Looping through a datagrid

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

Brian Conway

Can anyone give me an example of how I can loop through a datagrid and have
it run a stored procedure each time it loops?
 
Brian,

Why would you loop through a data grid? If anything, I would think that
you want to create a data adapter, and then have it update the underlying
data source based on the changes that have occured.

Also, are you using Windows Forms or ASP.NET?
 
Well I am not sure that that is the right thing to be doing. This is my
first attempt at creating a working Datagrid and having it update to a
database. What I have right now to pull the data is the following code. I
think though that I saw somewhere that I don't need to open and close the
connections though since the dataadapter is supposed to do this. I have
made some fields in the grid editable and want those changes to hit the
database at the same time with an update button that I have at the bottom of
the grid. From here I have no idea how to get the updates submitted to the
database. This is a webform. I am so lost on this part. Form fields to
the database are easy, but updating from a datagrid I am getting confused.

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

conn.Open();

OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, conn);

cmd.SelectCommand.Parameters.Add("@cuid", OleDbType.VarChar, 20).Value =
Session["CUID"];

DataSet dsFleet = new DataSet();

cmd.Fill(dsFleet);


DataGrid1.DataSource = dsFleet;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

conn.Close();



Nicholas Paldino said:
Brian,

Why would you loop through a data grid? If anything, I would think that
you want to create a data adapter, and then have it update the underlying
data source based on the changes that have occured.

Also, are you using Windows Forms or ASP.NET?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Conway said:
Can anyone give me an example of how I can loop through a datagrid and have
it run a stored procedure each time it loops?
 
Brian,

The ASP.NET model is different from the Windows Forms model. In the
ASP.NET model, you have to set up event handlers that will perform the
updating of the data when the request is posted back to the server. There
is an example of how to do this in the .NET documentation titled "Updating
Data in a SQL Database", located at (watch for line wrap):

http://msdn.microsoft.com/library/d...guide/html/cpconupdatingdatainsqldatabase.asp


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Conway said:
Well I am not sure that that is the right thing to be doing. This is my
first attempt at creating a working Datagrid and having it update to a
database. What I have right now to pull the data is the following code. I
think though that I saw somewhere that I don't need to open and close the
connections though since the dataadapter is supposed to do this. I have
made some fields in the grid editable and want those changes to hit the
database at the same time with an update button that I have at the bottom of
the grid. From here I have no idea how to get the updates submitted to the
database. This is a webform. I am so lost on this part. Form fields to
the database are easy, but updating from a datagrid I am getting confused.

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

conn.Open();

OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, conn);

cmd.SelectCommand.Parameters.Add("@cuid", OleDbType.VarChar, 20).Value =
Session["CUID"];

DataSet dsFleet = new DataSet();

cmd.Fill(dsFleet);


DataGrid1.DataSource = dsFleet;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

conn.Close();



message news:[email protected]...
Brian,

Why would you loop through a data grid? If anything, I would think that
you want to create a data adapter, and then have it update the underlying
data source based on the changes that have occured.

Also, are you using Windows Forms or ASP.NET?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Conway said:
Can anyone give me an example of how I can loop through a datagrid and have
it run a stored procedure each time it loops?
 
The example that they are showing on the link is for doing invidual row
updates, instead of a mutiple row update though.


Nicholas Paldino said:
Brian,

The ASP.NET model is different from the Windows Forms model. In the
ASP.NET model, you have to set up event handlers that will perform the
updating of the data when the request is posted back to the server. There
is an example of how to do this in the .NET documentation titled "Updating
Data in a SQL Database", located at (watch for line wrap):

http://msdn.microsoft.com/library/d...guide/html/cpconupdatingdatainsqldatabase.asp


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Conway said:
Well I am not sure that that is the right thing to be doing. This is my
first attempt at creating a working Datagrid and having it update to a
database. What I have right now to pull the data is the following code. I
think though that I saw somewhere that I don't need to open and close the
connections though since the dataadapter is supposed to do this. I have
made some fields in the grid editable and want those changes to hit the
database at the same time with an update button that I have at the
bottom
of
the grid. From here I have no idea how to get the updates submitted to the
database. This is a webform. I am so lost on this part. Form fields to
the database are easy, but updating from a datagrid I am getting confused.

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

OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, conn);

cmd.SelectCommand.Parameters.Add("@cuid", OleDbType.VarChar, 20).Value =
Session["CUID"];

DataSet dsFleet = new DataSet();

cmd.Fill(dsFleet);


DataGrid1.DataSource = dsFleet;

DataGrid1.SelectedIndex = 0;

DataGrid1.DataBind();

conn.Close();



message news:[email protected]...
Brian,

Why would you loop through a data grid? If anything, I would
think
that
you want to create a data adapter, and then have it update the underlying
data source based on the changes that have occured.

Also, are you using Windows Forms or ASP.NET?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Can anyone give me an example of how I can loop through a datagrid and
have
it run a stored procedure each time it loops?
 
foreach(DataRow dr in YourDataSet.Tables("YourTable")) {
//runs through every row .. you can access variables for the row
dr["Column"]
}
 
Back
Top