Solved Datagrid Click event??

Posted on 2003-11-11
Last Modified: 2008-02-01
Hi I am am trying to find the EASIEST way to have a user click on a populated datagrid (via a programmatic dataset) to populate text boxes and lists external to the datagrid.  Sort of a click the row you want to see detail on kind of functionality.   Is there a way to click any row in the datagrid and simply return the value of my hidden ID field into a variable to use for my control population on the rest of the page????

Thanks All,

Question by:TechQT
LVL 10

Expert Comment

ID: 9725313
In the property builder for the grid. Columns-->select the column that you want user to click that will raise the event, type in "Select" in the Command name: textbox.

Then you can code the SelectedIndexChanged event when users clicked an item in this column.

Expert Comment

ID: 9728839
Do something like this

In your page

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
                              <asp:TemplateColumn Visible="False" HeaderText="HiddenColumn">
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.HiddenField") %>'>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.HiddenField") %>'>
                              <asp:TemplateColumn HeaderText="FirstColumn">
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstfield") %>'>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstfield") %>'>
                              <asp:TemplateColumn HeaderText="SecondColumn">
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SecondColumn") %>'>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SecondColumn") %>'>
                              <asp:TemplateColumn HeaderText="Select">
                                          <asp:LinkButton runat="server" Text="Button" CommandName="SelectClick" CausesValidation="false"></asp:LinkButton>

and in your code behind
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
        If (e.CommandName = "Select") Then
            Dim hidfield As String
            hidfield = e.Item.Cells(0).Text()
        End If

    End Sub

Hope it helps

Accepted Solution

thomasdodds earned 250 total points
ID: 9734561
along with arvindb1's comment - you can set the DataKeyField for your data grid (instead of using the hidden field approach) in the databindings properties and then access it on the DataGrid_OnSelectedItemChanged Event by using

dim key as string = datagrid.datakeys(e.item.itemindex).tostring

both are acceptable ... just wanted to give you options ...

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
Memory Usage 2 50
How can I use SQL Stored Procedure to return FedEx Zone 17 41
SSL https .net web site force redirect 3 38
In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now