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

[Solved] When clicking in a cell I need the cell value server side

1 Answer 323 Views
Grid
This is a migrated thread and some comments may be shown as answers.
chom
Top achievements
Rank 1
chom asked on 17 Dec 2009, 06:03 PM
I am new to these controls and am missing the boat somewhere. I am trying to get the value of a specific cell when youclick the cell. I have just dropped a radgrid onto the page and configured it to a sqldatasource. I made one of the columns a button column using a link. I read through the Accessing Cells and Rows docs and added the following code to try to use the UniqueName to extract the value.

Using RadGrid
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
 
'e is the event argument object  
 
If TypeOf e.Item Is GridDataItem Then 
 
Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)  
 
Dim cell As TableCell = dataItem("UserName")  
 
Dim celltxt As String = cell.Text  
 
lblCell.Text = celltxt  
 
End If 
 
End Sub 
 
 

I am not sure if I have set up the radgrid correctly or if I am using the correct event. I want to convert a .net grid to a radgrid and used the RowCommand event in the .net grid to extract the values which is something like this.

Using ASP.Net Grid

 

Protected Sub gvManageSales_RowCommand(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvManageSales.RowCommand  
' CommandName property to determine which button was clicked.  
If e.CommandName = "SalesID" Then 
 
' Convert the row index stored in the CommandArgument  
' property to an Integer.  
 
Dim index As Integer = Convert.ToInt32(e.CommandArgument)  
' Retrieve the row that contains the button clicked   
 
' by the user from the Rows collection.  
 
Dim row As GridViewRow = gvManageSales.Rows(index)  
 
' Create a new ListItem object for the contact in the row.   
 
Dim UName As New ListItem()  
 
UName.Text = Server.HtmlDecode(row.Cells(1).Text)  
 
'Display name etc.  
lblName.text = UName  
 
End If 
 
End Sub 
 

 

Any ideas on how to acheive this in the radgrid.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Dec 2009, 06:01 AM
Hi Chom,

The ItemDataBound event fires on the server when an item(row) in the RadGrid control is bound with data. You can attach the ItemCommand event instead and obtain a GridDataItem (row) for the clicked row from the event arguments e.Item. Then access the value using the ColumnUniqueName as shown below.

ASPX:
 
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None" 
         AllowAutomaticUpdates="True"  onitemcommand="RadGrid1_ItemCommand"
        <MasterTableView AutoGenerateColumns="False" EditMode="InPlace" CommandItemDisplay="Top" 
            DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"
            <Columns>               
                <telerik:GridBoundColumn DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" 
                    UniqueName="CompanyName"
                </telerik:GridBoundColumn> 
               . . . 
            </Columns> 
        </MasterTableView> 
        <ClientSettings EnablePostBackOnRowClick="true" > 
        </ClientSettings> 
</telerik:RadGrid> 

CS:
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick"
        { 
            // Access the row 
            GridDataItem row = (GridDataItem)e.Item; //.Rows.(index); 
            // Set the value of label 
            lblName.Text = row["CompanyName"].Text; // CompanyName is the ColumnUniqueName 
        } 
    } 
Note: Also set the AutoPostBackOnRowClick property to True in order to fire the ItemCommand event when clicking the row.

-Shinu.
Tags
Grid
Asked by
chom
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or