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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

707 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