Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting the DataKey Value in a GridView

1 view
Skip to first unread message

Wannabe

unread,
May 8, 2008, 8:57:00 AM5/8/08
to
I have a gridview that has a label field and a checkbox field in it. I have
the checkbox set to autopost when changed. How can I get the datakey value
that is assigned to that row when a checkbox is either checked or unchecked?

Mark Rae [MVP]

unread,
May 8, 2008, 10:34:51 AM5/8/08
to
"Wannabe" <Wan...@discussions.microsoft.com> wrote in message
news:60CA5B12-682D-44E8...@microsoft.com...

protected void MyCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
int intDataKey =
Convert.ToInt32(MyGridView.DataKeys[((GridViewRow)((CheckBox)sender).Parent.Parent).RowIndex].Value);
// rest of code
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Manish

unread,
May 10, 2008, 4:05:00 AM5/10/08
to
Hi,

You can try the following code to implement the same.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chb As CheckBox = e.Row.Cells(2).FindControl("CheckBox1")
AddHandler chb.CheckedChanged, AddressOf check_changed
End If
End Sub
Public Sub check_changed(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(Me.GridView1.DataKeys.Item(CType(CType(sender,
CheckBox).Parent.Parent, GridViewRow).RowIndex).Value)
End Sub

Regards,
Manish
www.componentone.com

Wannabe

unread,
May 19, 2008, 9:06:02 AM5/19/08
to
Sorry for the long time in replying, but I was on vacation. I will check this
out and get back to you. Thanks.
0 new messages