473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the DataKey Value in a GridView

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?
Jun 27 '08 #1
3 7695
"Wannabe" <Wa*****@discus sions.microsoft .comwrote in message
news:60******** *************** ***********@mic rosoft.com...
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?
protected void MyCheckBox_OnCh eckedChanged(ob ject sender, EventArgs e)
{
int intDataKey =
Convert.ToInt32 (MyGridView.Dat aKeys[((GridViewRow)( (CheckBox)sende r).Parent.Paren t).RowIndex].Value);
// rest of code
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
Hi,

You can try the following code to implement the same.

Protected Sub GridView1_RowCr eated(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles GridView1.RowCr eated
If e.Row.RowType = DataControlRowT ype.DataRow Then
Dim chb As CheckBox = e.Row.Cells(2). FindControl("Ch eckBox1")
AddHandler chb.CheckedChan ged, AddressOf check_changed
End If
End Sub
Public Sub check_changed(B yVal sender As Object, ByVal e As EventArgs)
Response.Write( Me.GridView1.Da taKeys.Item(CTy pe(CType(sender ,
CheckBox).Paren t.Parent, GridViewRow).Ro wIndex).Value)
End Sub

Regards,
Manish
www.componentone.com

"Wannabe" wrote:
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?
Jun 27 '08 #3
Sorry for the long time in replying, but I was on vacation. I will check this
out and get back to you. Thanks.

"Manish" wrote:
Hi,

You can try the following code to implement the same.

Protected Sub GridView1_RowCr eated(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles GridView1.RowCr eated
If e.Row.RowType = DataControlRowT ype.DataRow Then
Dim chb As CheckBox = e.Row.Cells(2). FindControl("Ch eckBox1")
AddHandler chb.CheckedChan ged, AddressOf check_changed
End If
End Sub
Public Sub check_changed(B yVal sender As Object, ByVal e As EventArgs)
Response.Write( Me.GridView1.Da taKeys.Item(CTy pe(CType(sender ,
CheckBox).Paren t.Parent, GridViewRow).Ro wIndex).Value)
End Sub

Regards,
Manish
www.componentone.com

"Wannabe" wrote:
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?
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8880
by: Stu | last post by:
Hi, I am trying to return the data key field value from a dataset when I select a row in a datagrid after the datagrid has been bound to a dataset. When I an the Edit, Update and Cancel row I can return the DataKey using the sub listed below. However, the select button does not have 'DataGridCommandEventArgs' as one of it's parameters so I cannot use the same command.
7
3585
by: Joel Reinford | last post by:
I am looking for a way to set the selectedindex of a datagrid based on the datakey value. For example given this grid with a datakey of OrderID: OrderID Item 54 A 98 B 102 C 263 A
1
1822
by: mike | last post by:
i'd like to get datakey of GridView So i write code like below but it wasn't work well. how should i do? protected void BookGridView_RowDataBound(object sender, GridViewRowEventArgs e) { int tableId = int.Parse( BookGridView.DataKeys.ToString() );
0
6926
by: mike | last post by:
how can i get datakey value of GridView ??????? protected void BookGridView_RowDataBound(object sender, GridViewRowEventArgs e) { int tableId = int.Parse( BookGridView.DataKeys.ToString() ); .....
1
8317
by: Mark Richards | last post by:
I posted this earlier, and got no response. Basically I need to be able to grab a value from one of the fields in a gridView to populate a dropdown in another column of the gridview: I have a gridView that has 2 templated columns that contain drop-down lists. The first drop down list uses a value off of a formview on the same page for filtering. The 2nd drop-down is supposed to use the value off the first drop-down for it's...
3
17313
by: MasterChief | last post by:
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.
10
1689
by: Mike | last post by:
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an 'input string was not in a correct format.' Code: foreach (GridViewRow row in grid.Rows) { CheckBox chk = (CheckBox)gr.FindControl("checkbox"); if (chk.Checked) {
1
6237
by: JeffBuzz | last post by:
I have searched high and low and cannot figure this out. I want programmatically select a row in my GridView based on a primary key value (DataKey). You would think this is common, but I guess not. It is easy to do when the data key is on the current page, but doesn't work when the key is on another page. Any ideas? Thanks, Jeff
1
40180
by: jc | last post by:
Duh.. How do I do this? CreditId is my datakey, but I can't seem to get to it. Dim dgItem As GridViewRow For Each dgItem In gridEmployeeCredits.Rows If dgItem.DataItem("CreditId") = 0 Then Dim textboxCreditDate As TextBox = CType(dgItem.FindControl("textboxCreditDate"), TextBox)
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.