Get DataKey on RowDataBound

M

MasterChief

I am trying to add an attribute to a gridview row so when you click on
it, it will pop up an alert saying the current datakey for that row.
How is it possible to get a datakey when using the rowdatabound I want
to do something like

If (e.Row.RowType = DataControlRowType.DataRow) Then
e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
End If

but I don't know how to get the KeyID? Any help would be appreciated.
 
T

tdavisjr

MasterChief said:
I am trying to add an attribute to a gridview row so when you click on
it, it will pop up an alert saying the current datakey for that row.
How is it possible to get a datakey when using the rowdatabound I want
to do something like

If (e.Row.RowType = DataControlRowType.DataRow) Then
e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
End If

but I don't know how to get the KeyID? Any help would be appreciated.

You need to hande the RowCommand Event.

Then retreive the datakey

DataKey key = GridView1.DataKeys[e.CommandArgument]

Then the the key should have a Value property which is the datakey.
Now, I'm doing this totally from memory; but this should point you in
the right diretion.
 
T

tdavisjr

Oops. The index to the DataKeys is probably an integer so please cast
c.CommandArgument appriately.
 
M

MasterChief

I can get the DataKey using the RowCommand Event but I want to be able
to put the OnClick attribute into the Row and have it alert the KeyID.
When I try to get the value of KeyID that I got in RowCommand it comes
back with a null value. The only way I can find to add an attribute to
a row is during the RowDataBound Event.

Public Class _MasterTestBed
Inherits System.Web.UI.Page
Dim KeyID As String
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
KeyID = GridView1.DataKeys(e.CommandArgument).Value.ToString
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
e.Row.Attributes.Add("onMouseOver",
"this.style.backgroundColor='lightgrey'")
e.Row.Attributes.Add("onMouseOut",
"this.style.backgroundColor='Transparent'")
e.Row.Attributes.Add("onClick", "alert('" + KeyID + "')'")
End If
End Sub
End Class
 
Joined
Dec 29, 2007
Messages
1
Reaction score
0
I realize this thread is almost 2 years old, but I had the same question today. I searched extensively and never found an answer. When I searched for "gridview rowdatabound datakey" on Google, this is the first link given. I finally figured out how to do it, so I thought I'd post what I figured out here in case somebody else is having the same problem I was.

The task I was trying to perform was to fill another gridview in each row based on the datakey of that row. To use the datakey for something else, you'll have to change the code a little. Also, I am a C# guy. I used a converter (http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx) to get the vb code, so I'm not positive the syntax is correct, but it should be pretty close. When I use the same converter to go from VB to C#, it usually works.

The code below works when there is one datakey in the GridView. I didn't test to see what would happen if more datakeys were set in the gridview. I also didn't figure out how to get other datakeys if there are more than one. This is for if you have one datakey, and you want to get the datakey of the current row during rowdatabound:

VB:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
DirectCast(e.Row.FindControl("SqlDataSource1"), SqlDataSource).SelectParameters("primaryKey").DefaultValue = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString()
End If
End Sub

C#:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((SqlDataSource)e.Row.FindControl("SqlDataSource1")).SelectParameters["primaryKey"].DefaultValue = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
}
}
 
Joined
Nov 19, 2008
Messages
1
Reaction score
0
Accessing Multiple Datakeys in RowDataBound method

protected void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
{
string local_idcode="";
string local_year="";
if (e.Row.RowType == DataControlRowType.DataRow)
{


local_idcode = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());
local_year = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

}

Following are the statement of interest
Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());

Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

In Values[1] and Values[2] 1 & 2 shows the position of the DataKeys in you Gridview. Values Index start from [0].

Also don't forget to add if condition
if (e.Row.RowType == DataControlRowType.DataRow)
without that you may get an index error.

Mobeen.
 
Joined
Dec 29, 2008
Messages
1
Reaction score
0
Get datakey at gridview rowdatabound event

Dim s As String = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString()
 
Joined
Aug 9, 2011
Messages
1
Reaction score
0
I know this an old post but!!

protected void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
{
string local_idcode="";
string local_year="";
if (e.Row.RowType == DataControlRowType.DataRow)
{


local_idcode = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());
local_year = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

}

Following are the statement of interest
Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());

Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

In Values[1] and Values[2] 1 & 2 shows the position of the DataKeys in you Gridview. Values Index start from [0].

Also don't forget to add if condition
if (e.Row.RowType == DataControlRowType.DataRow)
without that you may get an index error.

Mobeen.

This helped me out of a pickle :congrats:
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top