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

Looping through a datagrid

P: n/a
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?
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?

Nov 16 '05 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uD**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?


Nov 16 '05 #3

P: n/a
it would probably be better to run through the datasource you have connected
to it.
"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?

Nov 16 '05 #4

P: n/a
How do I do this?
"Greg Young" <gr********@planetbeach.com> wrote in message
news:Ov**************@TK2MSFTNGP10.phx.gbl...
it would probably be better to run through the datasource you have connected to it.
"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?


Nov 16 '05 #5

P: n/a
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/de...qldatabase.asp
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:uD**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?



Nov 16 '05 #6

P: n/a
The example that they are showing on the link is for doing invidual row
updates, instead of a mutiple row update though.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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/de...qldatabase.asp

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

"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:uD**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
> 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?
>
>



Nov 16 '05 #7

P: n/a
foreach(DataRow dr in YourDataSet.Tables("YourTable")) {
//runs through every row .. you can access variables for the row
dr["Column"]
}
"Brian Conway" <Br**********@qwest.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
How do I do this?
"Greg Young" <gr********@planetbeach.com> wrote in message
news:Ov**************@TK2MSFTNGP10.phx.gbl...
it would probably be better to run through the datasource you have

connected
to it.
"Brian Conway" <Br**********@qwest.com> wrote in message
news:eI*************@TK2MSFTNGP10.phx.gbl...
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?



Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.