Get DataKey Values In GridView RowCommand?

May 12, 2010

I have a GridView with an associated DataKey, which is the item ID. How do I retrieve that value inside the RowCommand event?

This seems to work, but I don't like the cast to LinkButton (what if some other command is firing the event?), and I'm not too confident about the NamingContainer bit.

LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
int id = (int)grid.DataKeys[gvr.RowIndex].Value;

I'm aware that I could instead pass that ID as the CommandArgument, but I chose to use DataKey to give me more flexibility.

I'm also aware that it's possible to use a hidden field for the ID, but I consider that a hack that I don't want to use.

View 1 Replies

Similar Messages:

Data Controls :: How To Access DataKey Property Of Parent GridView From RowCommand Of Child GridView

Nov 8, 2013

I have followed your Nested level gridview and found it working, but i want add a button in the child element(from your example  i will have a btn  in Product grid and it should get contact_id (parent datakey value as well) ),as i  need to update in database.

View 1 Replies

Data Controls :: Get Value Of DataKey (DataKeyNames) Of Child (Nested) GridView On RowCommand

May 7, 2015

How to get grid view datakey value in nested grid view in gridview_RowCommand

I have used this code but here it will not detecting grid view name

protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e) {
string tempid = "";
switch (e.CommandName) {
case "cmdbtn":
int currentRowIndex = Convert.ToInt32(e.CommandArgument);

[Code] ....

I have used this code but here it will not detecting grid view name...

View 1 Replies

GridView Row Update With Datakey Values?

Jan 15, 2010

What seems to happen is 'GridView1_RowUpdating' gets fired, at this point newEnglish, newFrench, and newGerman all have the new values as they are passed into 'Logic.Update'. Then the update functions updates correctly, but once the 'GridView1_RowUpdating' sub ends, it seems to call Logic.Update another time, and this is where the new values get lost for French and German (datakeys) but not English.

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim newEnglish As String
Dim newFrench As String


View 2 Replies

Getting Values Of Cells In Rowcommand In Gridview?

Feb 23, 2010

I am working with gridview in my application. I want to get values of a particular cell in my rowcommand event on the click of a link button in a button field. Following is my code from aspx:


View 3 Replies

Forms Data Controls :: Get Values From Gridview Using Rowcommand Event?

Dec 17, 2010

i am using a grid view . i have added a linkbutton in cell[0]. when clicking on this butoon the below event is working.. actually i tried to get the cell[1].text... but not getting any cell values except cell[0].text... get the cell[1].text value.

protected void grvSchedule_RowCommand(object sender,
GridViewCommandEventArgs e)
{ if (Convert.ToInt32(e.CommandArgument) == 1)
{LinkButton lbt = (LinkButton)e.CommandSource;
GridViewRow grvRow = (GridViewRow)((DataControlFieldCell)lbt.Parent).Parent;
txtTime.Text = grvRow.Cells[0].Text;
//txtTime.Text = grvRow.Cells[1].Text;
mvwApp.ActiveViewIndex = 1;
hfdConsId.Value = ddlConsultant.SelectedValue.Trim();

View 6 Replies

Forms Data Controls :: Gridview RowCommand Event Returning Null Values?

Sep 2, 2010

I'm trying to retreive values entered by users in the textboxes in the footer of the gridview and insert these values into another sql table.My code is as follows :-

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
TextBox t1 = (TextBox)GridView2.FooterRow.FindControl("tfrm");
string frm = t1.Text;
TextBox t2 = (TextBox)GridView2.FooterRow.FindControl("tsub");
string sub = t2.Text;
string cmd = "insert into suggestions values('" + frm + "','" + sub + "','c','c')";
SqlCommand com = new SqlCommand(cmd,con);
int a = com.ExecuteNonQuery();

But the strings 'frm' and 'sub' returns null values.

View 8 Replies

Data Controls :: How To Get ID (DataKey) From GridView

Mar 26, 2016

This is Film table in database

ID Daste Preference

1 film 5
2 serial 4
3 serial 1
4 film 2

And bind gridview from this table and ordred by Preference Column I put Linknbutton in gridview that code is:

protected void LinkButton11_Click(object sender, EventArgs e) {
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
int id = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value);
using (SqlConnection conn = General.GetConnection()) {

[Code] .....

Oroblem is in this line:

int id = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value);

I want when I click on linkbutton it use Id but here it use Preference data How I can define that according to Id column it go to

Response.Redirect("EditFilm.aspx? &id=" + id);

View 1 Replies

AJAX :: Gridview Rowcommand Doesn't Return Updated Gridview

Jun 2, 2010

have a Gridview which repopulates itself when I click on a button outside the grid.This works fine, except that when I click on a button on the grid after repopulation, it gives the old values rather than the updated ones. I notice that querying GridView1 gives the grid when it was first populated.Anywhere I could have gone wrong?is already true.

View 5 Replies

Update Gridview - Use SelectedDataKey.Value.ToString() To Get Datakey?

Sep 2, 2010

after my grid view updates a record I would like to run some code, I have tried to use SelectedDataKey.Value.ToString() to get the datakey. However it is null, the record updates so the value must be stored somehow?

View 1 Replies

Unable To Get The Datakey Value Of A Selected Row In GridView For Use In FormView

Sep 15, 2010

I have a gridview like this :


I have done this before "n" number of times but I am not able to get it working this time. The trouble is that when the call for the Select Method of the formview goes to the relevant function - "GetCustomerByCusnum" I have a null value in its parameter "cusnum".

I know that I can write a selecting event and using CommandArgument, parse the value of the selected row and pass it into the Select method as a value but I dont want that solution. I know it works without the "Selecting" method but I cant recall how.

View 1 Replies

Forms Data Controls :: Get The Gridview Datakey Value?

Jan 5, 2011

I have a gridview with datakey value

in save button click i want to get the gridview datakey value

Dim _id As Integer For Each row As GridViewRow In Me.gridviewi.Rows Next

View 2 Replies

GridView Paging - Unable To Get The Correct Datakey Value?

Aug 5, 2010


<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" PagerSettings-Position="TopAndBottom"
PagerSettings-Mode="Numeric" PageSize="40" CellPadding="4" DataSourceID="dsEquipmentGridView"
ForeColor="#333333" GridLines="Horizontal" Style="font-size: x-small" AutoGenerateColumns="False"


For the first page loaded, it shows the correct DataKey value if I click on the command field.

However if I change to the next page of results, upon selecting a record the corresponding value is incorrect. It seems to keep the previous page information. How do I overcome this?


In the GridView1_SelectedIndexChanged method, I used the following:


I am unable to get the correct value after paging.

View 1 Replies

VS 2008 Datakey - Adding Row Dynamically To Gridview

Jul 27, 2011

I have a situation where I needed to add a row to my gridview. So say I have rows 3, 4 and 5.

My Datakeys are defined as "ID, CostingID". I did that so I can reference the two ID values I need when saving records.

here is the code in my RowDataBound event.

Basically if it is a datarow, I'm adding the value dataitems so I can get a sub total in the footer row.

Then, I have a boolean field called isDisbursement. So I sort my data by that field. So all records with the disbursement flag as true will display first.

Then as soon as the disbursement flag hits false I add a row to display the Total of the disbursement records value field.

All this works. My problem is, because I added a row at index 4 it inherits the datakey of the row that was at index 4 before. So when I'm looping through the grid rows to save the values I'm having a problem. I've attached an image so you will understand better what I'm trying to achieve. The "Total Disbursements" is the row I added. But it gets the following rows datakey because it is now at index 4.

when I do this, as soon as I reach row 4, although it doesn't have a CostingID it gets the CostingID of row 5 that used to be row 4:

Dim CostingID As Long = Convert.ToInt64(Me.grvCostings.DataKeys(row.RowIndex).Values("CostingID").ToString)
Dim strValue As String = CType(row.FindControl("txtValue"), TextBox).Text

If e.Row.RowType = DataControlRowType.DataRow Then
If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "value")) Then
_SubTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
End If

[Code] ....

View 1 Replies

Forms Data Controls :: Retrieving Datakey Value In Gridview?

Mar 19, 2010

Tried this but it doesn't work.


Retrieving datakey value in gridview?

View 5 Replies

Forms Data Controls :: Is It Possible To Have The BookId Value As A DataKey Of The GridView

Aug 14, 2010

Lets say for example that I have a book object that looks like this:


And a Page_Load method that bounds a GridView called "gvBooks" from a generic list of books:


Now as you can see, I want to have the BookId value that is part of the Dictionary as a DataKey of the gvBooks GridView.

That piece of code of curse returns an Exception:

"DataBinding: 'GridViewTest.Book' does not contain a property with the name 'BookId'."

My question is: Is it possible to have the BookId value as a DataKey of the GridView?

View 4 Replies

Data Controls :: How To Get Datakey Value Of Nested Child GridView

May 7, 2015

How can get datakey value of Nested(child) GridView on click on delete button of child grid

View 1 Replies

C# - Gridview Rowcommand After Refreshing?

May 31, 2010

I have a gridview inside an updatepanel.After updating the gridview, accessing the individual rows does not seem to give the right row. For example:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);

Row from the above code gives the values from the gridview before the gridview is refreshed/updated.

View 1 Replies

Forms Data Controls :: TemplateField In Gridview Not Passing DataKey Value?

Feb 4, 2010

I'm relatively new to the VS environment. I'm using VS2010 Beta 2 with VB. I have a Gridview setup to pull a list of employees. Next to their name I have edit and delete icons. When I leave the icons as ButtonFields, they pass the DataKey value to my code behind perfectly. When I convert the ButtonFields to TemplateFields, they no longer pass the DataKey.




View 2 Replies

Forms Data Controls :: Highlighting A Row In A Gridview Control Using A DataKey In C#

Apr 11, 2010

My gridivew currently has 4 colums with the third being a subject column. I want this colum to have bold text if the datakey 'ReadStatus' is equal to false. I have had success if the column is bound to the gridview but 'ReadStatus' is only a datakey and not bound to the gridiview. My code is below:



View 1 Replies

Forms Data Controls :: Programmatically Set GridView.SelectedIndex From Known DataKey

Aug 4, 2010

I want to select a row in my GridView based on a known data key value at runtime.

In other words, I have a data key value, 2 for ex., and the primary key value of one of the rows in my GridView is 2. How can select that row? (Without having to go through each row to find which one matches my key, then setting the SelectedIndex.

View 6 Replies

Forms Data Controls :: Retrieving Datakey, (an ID Value) From GridView Via Check Box?

Apr 13, 2010

I have a GridView with a checkbox for the user to select different categories. I am trying to retrieve the CategoryID associated with each row when the user clicks next, however I keep getting an error that tells me the selected index cannot be negative or out of range.

Here is my function


How do I retrieve the CategoryID of the row as an Integer value?

View 7 Replies

Forms Data Controls :: Determine DataKey For The Row To Be Deleted In GridView?

Jun 14, 2010

I have wired an event to handle delete command for my GridView. But I cannot determine which the record's unique key, in my case stored in DOCIDNO field. All I get is a row index, such as 0, 1, 2 which obviously cannot be used to accurately delete a record from database.



View 2 Replies

Forms Data Controls :: Get The DataKey Value In RowDataBound Event For GridView?

Feb 22, 2010

How to get the DataKey value in RowDataBound event for GridView

View 3 Replies

Forms Data Controls :: Get Datakey From Templatefield (dropdownlist) In Gridview

May 21, 2010

I have a gridview with a DropDownList (contained in a TemplateField). I have set the OnSelectedIndexChanged event to a function called "ddlAgent_SelectedIndexChanged" This allows me to aquire when a user has changed the value for the dropdownlist. What I need to do is persist this newly selected value to the database for the gridview record. The problem I'm having is how to I get the datakeyNames value for that row? See code below!

TemplateField source
<asp:TemplateField HeaderText="Agent">
<asp:DropDownList ID="ddlAgent" runat="server" AutoPostBack="False" OnSelectedIndexChanged="ddlAgent_SelectedIndexChanged">
<ItemStyle HorizontalAlign="Center"></ItemStyle>

Protected Sub ddlAgent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gvItems As DropDownList = CType(sender, DropDownList)
' Write the selected Agent ID to the Inquiry record.
'Dim ID As Integer = gvMaint.DataKeyNames(e.SelectedIndex).Value ' THIS FAILS BECAUSE
e.SelectedIndex is not part of System.EventArgs
End Sub

View 3 Replies

Copyrights 2005-15, All rights reserved