This is a migrated thread and some comments may be shown as answers.

Get value of cell in Grid

11 Answers 213 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mgroesink
Top achievements
Rank 1
mgroesink asked on 16 Sep 2008, 09:28 PM
I have a RadGrid with cells that contain numeric values. If a user clicks a cell I want to decrease the value in that cell.
How do I do that? The main question is: how do I get the value that is in the cell that the user clicked on?

I hope that someone can help me with this problem. I have a feeling that the solution is very simple, but this did already cost me a whole evening without coming any closer to as solution.

11 Answers, 1 is accepted

Sort by
0
Scott R
Top achievements
Rank 1
answered on 17 Sep 2008, 03:42 AM
0
mgroesink
Top achievements
Rank 1
answered on 17 Sep 2008, 05:18 AM
In all those solutions a unique column name is used. But how do I get this name when the user clicks a cell? I do not know which column will be clicked.
0
Daniel
Telerik team
answered on 17 Sep 2008, 06:20 AM
Hello Marcel,

Please take a look at the following forum thread where you can find a working demo illustrating the needed approach.
CellIndex in onRowClick

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
mgroesink
Top achievements
Rank 1
answered on 17 Sep 2008, 07:17 PM
Thanks. I am able now to get the unique columnname now.

But how can I do that serverside? The user clicks a cell in the grid (again it is not preditable which cell) that contains a number and after clicking that cellthe value should increase by 1.
0
Daniel
Telerik team
answered on 22 Sep 2008, 01:16 PM
Hello Marcel,

There is no pure server-side solution but it's rather easy to implement it as shown below:

javascript
<script type="text/javascript" language="javascript"
    function RowClick(sender, args) 
    { 
        var cellIndex = args.get_domEvent().target.cellIndex; 
        sender.get_masterTableView().fireCommand("CellClick", args.get_tableView().get_columns()[cellIndex].get_uniqueName() + "|" + args.get_itemIndexHierarchical()); 
    } 
</script> 

code-behind
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == "CellClick"
    { 
        string columnName = e.CommandArgument.ToString().Split('|')[0]; 
        string rowNumber = e.CommandArgument.ToString().Split('|')[1]; 
        label1.Text = String.Format("You clicked on column {0}, row {1}: ", columnName, rowNumber); 
    } 

aspx
<asp:Label ID="label1" runat="server" Text=" "></asp:Label> 
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="RadGrid1_ItemCommand" 
    GridLines="None"

aspx
<ClientSettings Selecting-AllowRowSelect="true" ClientEvents-OnRowClick="RowClick"
</ClientSettings> 

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
mgroesink
Top achievements
Rank 1
answered on 22 Sep 2008, 08:20 PM
Thanks for the nice solution Daniel. This is what I was looking for.
0
Paul
Top achievements
Rank 1
answered on 04 Nov 2008, 07:06 PM
Hi Daniel,
Thanks for your solution you have provided it works great in Firefox but not in IE. The line found in javascript on the RowClick method:
 
sender.get_masterTableView().fireCommand("CellClick", args.get_tableView().get_columns()[cellIndex].get_uniqueName() + "|" + args.get_itemIndexHierarchical());  

This brings back the correct row number and column in Firefox. In IE it brings back the column one left to the column the cell is under.

Any help is greatly appreciated.
Cheers,
Paul



0
Daniel
Telerik team
answered on 05 Nov 2008, 09:35 AM
Hello Paul,

I attached a simple example using the depicted code. Could you please let me know how to modify it in order to reproduce this behavior. Thus I may be able to provide more to-the-point answer.

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Paul
Top achievements
Rank 1
answered on 05 Nov 2008, 10:06 AM
Hi Daniel,
I worked out what the problem was, I had a hidden column in my grid. So it appears that in Firefox, it doesn't count hidden columns when you use your function to return the column name. IE on the otherhand includes hidden columns in its column count and therefore returns the column one left to the column you actually want (due to my hidden column being at the start of the grid).
Surprised other people haven't seen this problem.
Cheers,
Paul
0
Paul
Top achievements
Rank 1
answered on 05 Nov 2008, 10:07 AM
In order to solve the problem, I just put the hidden column at the end of my grid so it now works in both IE and firefox.
0
Daniel
Telerik team
answered on 05 Nov 2008, 03:33 PM
Hello Paul,

I'm glad to realize that you managed to overcome the issue. Thank you for sharing your solution with us.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
mgroesink
Top achievements
Rank 1
Answers by
Scott R
Top achievements
Rank 1
mgroesink
Top achievements
Rank 1
Daniel
Telerik team
Paul
Top achievements
Rank 1
Share this question
or