How to get a cell value from datagrid by onClick Event?

Posted on 2006-05-15
Last Modified: 2007-12-19
Hi, i have a datagrid with many rows and 12 columns (months of year).

        Jan    Feb .... Dic
John  10        5       7
Greg   9        6       9
Mary  10       4       8

label1.text = "6"

So, i need select the value from Greg in Feb with a click in my datagrid,  and i´d like to display the value, in this case is "6" in a label for example, how can i do it?

I tried using e.Item.Attributes.Add("onclick") in Datagrid.ItemCreated but it´s not working.

Could you help me?
Question by:flavio2000
    LVL 6

    Accepted Solution

    Replace BoundColumns by ButtonColumns in your datagrid, e.g.:
                <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" OnItemCommand="DataGrid1_ItemCommand">
                        <asp:ButtonColumn DataTextField="Jan" HeaderText="Jan" CommandName="1"></asp:ButtonColumn>
                        <asp:ButtonColumn DataTextField="Feb" HeaderText="Feb" CommandName="2"></asp:ButtonColumn>

    ...into CommandName proprety put index of column in datagrid.

    Then use event handler of ItemCommand event of the datagrid like this:
    Sub DataGrid1_ItemCommand(sender As Object, e As DataGridCommandEventArgs)
      label1.text = CType(e.Item.Cells(e.commandname).Controls(0),LinkButton).Text
    End Sub
    LVL 61

    Expert Comment

    You could also use a single select column for this.  It could be a single column.  You can use "Select" as it's text like this:

    <asp:ButtonColumn Text="Select"  CommandName="Select"></asp:ButtonColumn>

    Or the Name as the text (John, Greg etc will be the clickable ltext) like this:

    <asp:ButtonColumn DataTextField ="Select"  CommandName="Select"  HeaderText="Name"></asp:ButtonColumn>

    Then in the SelectedIndexChanged event (no ItemCreated code needed):

    SomeTargetVariable1 = me.dgYourDatagrid.SelectedItem.Cells(1)
    SomeTargetVariable2 = me.dgYourDatagrid.SelectedItem.Cells(2)
                                                                                                ^--- Zero based column count.  

    (Visual Basic)


    Author Comment

    Thanks! Paab

    Author Comment

    One more question...

    The last column is a sum of 12 months (a total column).. how can i sum the values in this column?


            Jan    Feb .... Dic  Total
    John  10        5       7    22
    Greg   9        6       9     24
    Mary  10       4       8     22

    LVL 6

    Expert Comment

    You can add empty column to your sql query: select jan ...dic, null as total from table.
    Then in databound event handler of the datagrid you can sum the month values:
        Sub DataGrid1_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
              If e.Item.ItemType = ListItemType.Item or _
                 e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim i as Integer
                Dim sum as Integer
                for i = 1 to 12
                   sum += CInt(CType(e.Item.Cells(i).Controls(0),LinkButton).Text)
                'If total is Bound Column
                e.Item.Cells(13).Text = sum              
              End If
        End Sub


    Author Comment

    Very Good! Many Thanks!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 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