DataGrid Key Field - Actual Value

P

Prince

Hi, could someone either please tell me or point me in
the right direction.

I have a DataGrid on a page with the DataKeyField="ID".
The grid displays checkboxes for each record. I have an
<asp:Button> on the page. In the handle for the button
click, when I determine if the checkbox is checked (by
interating thru all the rows of the DataGrid, how do I
get the actual value of ID so that I can pass the value
to my stored procedures?

code looks something like this:
DataGrid scDG;
// binding ....

int count = scDg.Items.Count;
int id;
for loop...
id = ????
end for loop


Thanks,
Prince
 
S

Scott Mitchell [MVP]

Prince said:
I have a DataGrid on a page with the DataKeyField="ID".
The grid displays checkboxes for each record. <snip> how do I
get the actual value of ID so that I can pass the value
to my stored procedures?

'VB.NET
Dim ID as Integer = Convert.ToInt32(DataGridID.DataKeys(index))

// C#
int ID = Convert.ToInt32(DataGridID.DataKeys[index]);


So, if you're looping through the Items collection, the index would be
the current loop index. If you're in a DataGrid *Command event handler
(like UpdateCommand, or DeleteCommand), you'd use the index of the row
that triggered the event (e.Item.ItemIndex).

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
P

Prince

Funny. That was exactly what I had but thought it wasn't
working. I realized that the records were being deleted
in the back but the page wasn't being refreshed properly.

Thanks,
Prince
-----Original Message-----
Prince said:
I have a DataGrid on a page with the DataKeyField="ID".
The grid displays checkboxes for each record. <snip> how do I
get the actual value of ID so that I can pass the value
to my stored procedures?

'VB.NET
Dim ID as Integer = Convert.ToInt32(DataGridID.DataKeys (index))

// C#
int ID = Convert.ToInt32(DataGridID.DataKeys[index]);


So, if you're looping through the Items collection, the index would be
the current loop index. If you're in a DataGrid *Command event handler
(like UpdateCommand, or DeleteCommand), you'd use the index of the row
that triggered the event (e.Item.ItemIndex).

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
.
 
S

Scott Mitchell [MVP]

Prince said:
Funny. That was exactly what I had but thought it wasn't
working. I realized that the records were being deleted
in the back but the page wasn't being refreshed properly.

Yeah, whenever deleting or updating data you need to make sure to rebind
the data to the DataGrid.

Glad to hear you got it working! :)

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPMessageboard.com
http://www.ASPFAQs.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top