By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

Looping through a Datagrid

P: n/a
I desperately need some help in coding how to have a completely ediable
datagrid be able to loop through each row and insert into a database table.
I am using a stored procedure in the database to do the insert, so I think I
just need to figure out how to get it to loop through all the rows on the
grid, and update from there.
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Brian,

If the data grid is connected to a DataSet as a source, then you should
be able to pass that data set to a DataAdapter, which will cycle through the
changed rows and update the underlying database. You will need to set the
appropriate properties (SelectCommand, InsertCommand, DeleteCommand and
UpdateCommand) to command objects which will call your stored procedure
instead of just issuing an insert, delete, or update.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I desperately need some help in coding how to have a completely ediable
datagrid be able to loop through each row and insert into a database table. I am using a stored procedure in the database to do the insert, so I think I just need to figure out how to get it to loop through all the rows on the
grid, and update from there.

Nov 16 '05 #2

P: n/a
Currently I do not have it attached to a dataset. How can I modify it to do
that? I have seen a couple references to the same thing that you mentioned
about it being connected to a dataset. Here is what I have so far on the
datagrid.

OleDbConnection conn = new
OleDbConnection(ConfigurationSettings.AppSettings["FleetConnectionString"]);

conn.Open();

OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);

cmd.Parameters.Add("@rcInfo", OleDbType.VarChar, 20).Value = lblRCInfo.Text;

OleDbDataReader dreader = cmd.ExecuteReader();

DataGrid1.DataSource = dreader;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

dreader.Close();

conn.Close();

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O4**************@TK2MSFTNGP11.phx.gbl...
Brian,

If the data grid is connected to a DataSet as a source, then you should be able to pass that data set to a DataAdapter, which will cycle through the changed rows and update the underlying database. You will need to set the
appropriate properties (SelectCommand, InsertCommand, DeleteCommand and
UpdateCommand) to command objects which will call your stored procedure
instead of just issuing an insert, delete, or update.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I desperately need some help in coding how to have a completely ediable
datagrid be able to loop through each row and insert into a database table.
I am using a stored procedure in the database to do the insert, so I think I
just need to figure out how to get it to loop through all the rows on

the grid, and update from there.


Nov 16 '05 #3

P: n/a
Brian,

What you can do is instead of attaching to the reader, you can attach
the command to an OleDbDataAdapter, and then set the UpdateCommand property
to an OleDbCommand representing your stored procedure. Then, you would fill
a dataset and bind the grid to the dataset. This has its disadvantages (it
wont be as fast, and could be an issue, for example). On return, you can
see the changes in the dataset (or should be able to, through viewstate, or
some other mechanism). Then, just pass the dataset back to the adapter, and
call Update.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Conway" <Br**********@qwest.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Currently I do not have it attached to a dataset. How can I modify it to do that? I have seen a couple references to the same thing that you mentioned
about it being connected to a dataset. Here is what I have so far on the
datagrid.

OleDbConnection conn = new
OleDbConnection(ConfigurationSettings.AppSettings["FleetConnectionString"]);
conn.Open();

OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);

cmd.Parameters.Add("@rcInfo", OleDbType.VarChar, 20).Value = lblRCInfo.Text;
OleDbDataReader dreader = cmd.ExecuteReader();

DataGrid1.DataSource = dreader;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

dreader.Close();

conn.Close();

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:O4**************@TK2MSFTNGP11.phx.gbl...
Brian,

If the data grid is connected to a DataSet as a source, then you

should
be able to pass that data set to a DataAdapter, which will cycle through

the
changed rows and update the underlying database. You will need to set the
appropriate properties (SelectCommand, InsertCommand, DeleteCommand and
UpdateCommand) to command objects which will call your stored procedure
instead of just issuing an insert, delete, or update.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I desperately need some help in coding how to have a completely ediable datagrid be able to loop through each row and insert into a database

table.
I am using a stored procedure in the database to do the insert, so I

think
I
just need to figure out how to get it to loop through all the rows on

the grid, and update from there.



Nov 16 '05 #4

P: n/a
Will this enable me to do a mass update in one shot? Or would it be a per
line basis?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ey**************@TK2MSFTNGP10.phx.gbl...
Brian,

What you can do is instead of attaching to the reader, you can attach
the command to an OleDbDataAdapter, and then set the UpdateCommand property to an OleDbCommand representing your stored procedure. Then, you would fill a dataset and bind the grid to the dataset. This has its disadvantages (it wont be as fast, and could be an issue, for example). On return, you can
see the changes in the dataset (or should be able to, through viewstate, or some other mechanism). Then, just pass the dataset back to the adapter, and call Update.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Conway" <Br**********@qwest.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Currently I do not have it attached to a dataset. How can I modify it to

do
that? I have seen a couple references to the same thing that you mentioned
about it being connected to a dataset. Here is what I have so far on the
datagrid.

OleDbConnection conn = new

OleDbConnection(ConfigurationSettings.AppSettings["FleetConnectionString"]);

conn.Open();

OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);

cmd.Parameters.Add("@rcInfo", OleDbType.VarChar, 20).Value =

lblRCInfo.Text;

OleDbDataReader dreader = cmd.ExecuteReader();

DataGrid1.DataSource = dreader;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

dreader.Close();

conn.Close();

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:O4**************@TK2MSFTNGP11.phx.gbl...
Brian,

If the data grid is connected to a DataSet as a source, then you

should
be able to pass that data set to a DataAdapter, which will cycle through
the
changed rows and update the underlying database. You will need to set the appropriate properties (SelectCommand, InsertCommand, DeleteCommand
and UpdateCommand) to command objects which will call your stored procedure instead of just issuing an insert, delete, or update.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> I desperately need some help in coding how to have a completely

ediable > datagrid be able to loop through each row and insert into a database
table.
> I am using a stored procedure in the database to do the insert, so I

think
I
> just need to figure out how to get it to loop through all the rows

on the
> grid, and update from there.
>
>



Nov 16 '05 #5

P: n/a
Brian,

The DataAdapter will cycle through all the rows, and then depending on
whether or not the row changed, it will update the row. It is not a batch
operation.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Will this enable me to do a mass update in one shot? Or would it be a per
line basis?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ey**************@TK2MSFTNGP10.phx.gbl...
Brian,

What you can do is instead of attaching to the reader, you can attach
the command to an OleDbDataAdapter, and then set the UpdateCommand

property
to an OleDbCommand representing your stored procedure. Then, you would

fill
a dataset and bind the grid to the dataset. This has its disadvantages

(it
wont be as fast, and could be an issue, for example). On return, you can see the changes in the dataset (or should be able to, through viewstate,

or
some other mechanism). Then, just pass the dataset back to the adapter,

and
call Update.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Conway" <Br**********@qwest.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Currently I do not have it attached to a dataset. How can I modify it to
do
that? I have seen a couple references to the same thing that you mentioned about it being connected to a dataset. Here is what I have so far on
the datagrid.

OleDbConnection conn = new

OleDbConnection(ConfigurationSettings.AppSettings["FleetConnectionString"]);

conn.Open();

OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);

cmd.Parameters.Add("@rcInfo", OleDbType.VarChar, 20).Value =

lblRCInfo.Text;

OleDbDataReader dreader = cmd.ExecuteReader();

DataGrid1.DataSource = dreader;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

dreader.Close();

conn.Close();

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O4**************@TK2MSFTNGP11.phx.gbl...
> Brian,
>
> If the data grid is connected to a DataSet as a source, then you
should
> be able to pass that data set to a DataAdapter, which will cycle through the
> changed rows and update the underlying database. You will need to
set
the
> appropriate properties (SelectCommand, InsertCommand, DeleteCommand

and > UpdateCommand) to command objects which will call your stored procedure > instead of just issuing an insert, delete, or update.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "Brian Conway" <Br**********@qwest.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > I desperately need some help in coding how to have a completely

ediable
> > datagrid be able to loop through each row and insert into a
database > table.
> > I am using a stored procedure in the database to do the insert, so I think
> I
> > just need to figure out how to get it to loop through all the rows

on the
> > grid, and update from there.
> >
> >
>
>



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.