Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Get DataKey on RowDataBound

Reply
Thread Tools

Get DataKey on RowDataBound

 
 
MasterChief
Guest
Posts: n/a
 
      02-15-2006
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.

 
Reply With Quote
 
 
 
 
tdavisjr
Guest
Posts: n/a
 
      02-15-2006

MasterChief wrote:
> 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.

 
Reply With Quote
 
 
 
 
tdavisjr
Guest
Posts: n/a
 
      02-15-2006
Oops. The index to the DataKeys is probably an integer so please cast
c.CommandArgument appriately.

 
Reply With Quote
 
MasterChief
Guest
Posts: n/a
 
      02-15-2006
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.ToStri ng
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

 
Reply With Quote
 
shuameston shuameston is offline
Junior Member
Join Date: Dec 2007
Posts: 1
 
      12-29-2007
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/co...arp-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").Defa ultValue = 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();
}
}
 
Reply With Quote
 
Mobeen Mobeen is offline
Junior Member
Join Date: Nov 2008
Posts: 1
 
      11-19-2008
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.
 
Reply With Quote
 
fragiletruce fragiletruce is offline
Junior Member
Join Date: Dec 2008
Posts: 1
 
      12-29-2008
Dim s As String = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString( )
 
Reply With Quote
 
siddersscfc siddersscfc is offline
Junior Member
Join Date: Aug 2011
Posts: 1
 
      08-09-2011
Quote:
Originally Posted by Mobeen View Post
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I get the GridView object in the RowDataBound event? mark4asp ASP .Net 2 10-28-2010 09:42 AM
Get DataKey value in GridView David C ASP .Net 2 12-02-2008 06:10 PM
How to get Datakey from gridview row? jc ASP .Net 1 03-06-2008 01:18 AM
how can i get datakey value of GridView ??????? mike ASP .Net 0 11-25-2005 12:06 PM
get datakey from datalist Andy Sutorius ASP .Net 7 03-04-2005 04:43 PM



Advertisments