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

Posted on 2006-05-15
Medium Priority
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
  • 3
  • 2

Accepted Solution

the_paab earned 2000 total points
ID: 16683605
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

ID: 16683914
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

ID: 16684840
Thanks! Paab
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16685405
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


Expert Comment

ID: 16687862
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

ID: 16689709
Very Good! Many Thanks!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 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