22850

In Gridview, I want to capture the Column Id's value for the selected Row

In Gridview, I want to capture the Column Id's value for the selected row. When I click "Select" and Click "Go" button, I receive this output:

Captured: MainContent_GridView1

How do I select the ID of that particular row?

protected void btnGo_Click(object sender, EventArgs e) { try { GridViewRow row = GridView1.SelectedRow; String RowID = row.ClientID; Response.Write("Captured: " + RowID); } catch (Exception ex) { Response.Write("Error:"+ ex.ToString()); } }

Answer1:

One way to get the field values in a row is to add the field names to the DataKeyNames of the GridView:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id,Phone" ... >

In code-behind, you can get the values this way:

int Id = (int)GridView1.DataKeys[row.RowIndex].Values["Id"]; string phone = (string)GridView1.DataKeys[row.RowIndex].Values["Phone"];

I included Phone in the list to show that several fields can be specified. In your case, DataKeyNames would only contain Id.

Answer2:

I think this is what you mean, you have a GridView that is showing the id in one of the columns. So, if it was the first column, you could do something like

GridViewRow row = GridView1.SelectedRow; string id = row.Cells[0].Text;

Is that what you mean?

Answer3:

String Id = GridView1.SelectedRow.Cells[0].Text

Recommend

  • Cannot apply indexing with [] to an expression of type 'ISet'
  • gridview highlighting current row
  • ASP.NET Dynamic Data Site: Force First Column to be Alphabetized
  • How to Delete a Row or Disable Delete Button for specific row in GridView?
  • Unchecking radiobutton list using jquery
  • How can I clear data in a GridView?
  • ASP.NET dataset getdataBy Failed to enable constraints. One or more rows contain values violating no
  • Deleting a row completely from a dataset
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • Exception HRESULT: 0x800455BC in speech recognition in Windows phone 8
  • How to access recipient on sent messages page with mailboxer
  • Click on button in another program - FindWindow, C#
  • DIV instruction jumping to random location?
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Can't delete or rename original file after resizing
  • c# open webrowser in many tab
  • Bypass multiple inheritance in Java
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Transactional Create with Validation in ServiceStack Redis Client
  • How to Cache Real-time Data?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Is there a way to do normal logging with EureakLog?
  • How to set ini file attributes during an Inno install
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?