Looping through a Datagrid

  • Thread starter Thread starter Brian Conway
  • Start date Start date
B

Brian Conway

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.
 
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.
 
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 said:
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]
- (e-mail address removed)

Brian Conway said:
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.
 
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]
- (e-mail address removed)


Brian Conway said:
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();





message news:[email protected]...
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]
- (e-mail address removed)

Brian Conway said:
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.
 
Will this enable me to do a mass update in one shot? Or would it be a per
line basis?


Nicholas Paldino said:
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]
- (e-mail address removed)


Brian Conway said:
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();





message news:[email protected]...
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]
- (e-mail address removed)

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.
 
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]
- (e-mail address removed)

Brian Conway said:
Will this enable me to do a mass update in one shot? Or would it be a per
line basis?


message news:[email protected]...
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]
- (e-mail address removed)


Brian Conway said:
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]" <[email protected]>
wrote
in
message 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]
- (e-mail address removed)

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.
 
Back
Top