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

Retreiving all the cell values of a selected row in radgrid in code- behind) - Need immediate help

10 Answers 1660 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Abdul Kadar
Top achievements
Rank 1
Abdul Kadar asked on 17 Oct 2008, 10:25 AM

I am using a radgrid with 2 fields.categoryid and category description.And I set category id as my datakeyname.How can I obtain the data of category description field when I select a particular row.I need all the appropriate cell values of the selected row.Pls help me in this issue ASAP.

 

 

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" GridLines="None" Style="left: 24px; position: absolute; top: 168px" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" Height="24px" PageSize="5" Skin="Sunset" Width="512px" AllowAutomaticUpdates="True" AutoGenerateEditColumn="True" AllowAutomaticDeletes="True">

 

 

 

<MasterTableView DataKeyNames="categoryid" DataSourceID="SqlDataSource3" EditMode="InPlace" ClientDataKeyNames="categoryid" >

 

 

 

<Columns>
<telerik:GridBoundColumn DataField="categoryid" DataType="System.Int64" EmptyDataText="&amp;nbsp;" HeaderText="Category ID" SortExpression="categoryid" UniqueName="categoryid" ReadOnly="True"></telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn DataField="categorydescription" EmptyDataText="&amp;nbsp;" HeaderText="Description" SortExpression="categorydescription" UniqueName="categorydescription"> </telerik:GridBoundColumn>

 

 

 

<telerik:GridButtonColumn UniqueName="SelectColumn" Text="Select" CommandName="Select"></telerik:GridButtonColumn>
</Columns>

 

 

 

<ExpandCollapseColumn><HeaderStyle Width="20px" /></ExpandCollapseColumn>
<RowIndicatorColumn><HeaderStyle Width="20px" /></RowIndicatorColumn></MasterTableView><FilterMenu EnableTheming="True" Skin="Sunset"><CollapseAnimation Duration="200" Type="OutQuint" /></FilterMenu><HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"Font-Underline="False" Wrap="True" /><PagerStyle Mode="NextPrevAndNumeric" /><ClientSettings><Selecting AllowRowSelect="True" /></ClientSettings><EditItemStyle Font-Names="Tahoma" Font-Size="9pt" /></telerik:RadGrid>

 

 

 

 

10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 17 Oct 2008, 10:53 AM
Hi Abdul,

Generally SelectedItems is a collection of GridDataItem and you can index this type by column UniqueName. Here is an example for SelectedIndexChanged event:

foreach(GridDataItem item in RadGrid1.SelectedItems)
{
    foreach(GridColumn column in item.OwnerTableView.RenderColumns)
    {
       Response.Write(String.Format("{0} : {1} <br />", column.UniqueName, item[column.UniqueName].Text));
    }
}

All the best,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Princy
Top achievements
Rank 2
answered on 17 Oct 2008, 10:54 AM
Hello Abdul,

Try accessing the cells in the selected row using the UniqueName property of columns as shown below.
cs:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach(GridDataItem dataItem in RadGrid1.Items) 
        { 
           if (dataItem.Selected) 
            { 
                string strtxt = dataItem["categorydescription"].Text; 
            } 
        } 

Thanks
Princy.
0
Abdul Kadar
Top achievements
Rank 1
answered on 17 Oct 2008, 01:02 PM
what is the purpose of GetCellbyColumnUniqueName ? Using this can we able to get the values of all the cell in a particular selected row ? If pls explain me in details for my above scenario in VB
Also pls send the above code in VB.net.
Thanks
0
Abdul Kadar
Top achievements
Rank 1
answered on 17 Oct 2008, 01:13 PM

Hi,
thank u for ur reply
I am new in telerik controls
I used the below code in my asp.net 2 controls
Is there any way of writing code like this in radcontrols?
I was bit confused of the data items ?
Pls help me on this issue ?
Whats the purpose of GetCellbycolumnunique name
Pls send the code in VB.net

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

Dim SesSupplierCode As String = System.Convert.ToString(Server.HtmlEncode(GridView1.SelectedDataKey("sno")))

Cn.Open()

Dim Cmd As New SqlCommand

Cmd.Connection = Cn

Cmd.CommandText = "select categoryid,description from categorymaster where sno='" & SesSupplierCode & "'"

Dim Rs As SqlDataReader = Cmd.ExecuteReader()

If Rs.Read Then

txtcategoryid.Text = Rs.GetValue(0)

Txtcategorydescription.Text = Rs.GetString(1)

End If

BtnSave.Enabled = False

TxtApplicationId.Enabled = False

BtnUpdate.Enabled = True

End Sub

0
Abdul Kadar
Top achievements
Rank 1
answered on 17 Oct 2008, 01:14 PM
 

Hi,
thank u for ur reply
I am new in telerik controls
I used the below code in my asp.net 2 controls
Is there any way of writing code like this in radcontrols?
I was bit confused of the data items ?
Pls help me on this issue ?
Whats the purpose of GetCellbycolumnunique name
Pls send the code in VB.net

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

Dim SesSupplierCode As String = System.Convert.ToString(Server.HtmlEncode(GridView1.SelectedDataKey("sno")))

Cn.Open()

Dim Cmd As New SqlCommand

Cmd.Connection = Cn

Cmd.CommandText = "select categoryid,description from categorymaster where sno='" & SesSupplierCode & "'"

Dim Rs As SqlDataReader = Cmd.ExecuteReader()

If Rs.Read Then

txtcategoryid.Text = Rs.GetValue(0)

Txtcategorydescription.Text = Rs.GetString(1)

End If

BtnSave.Enabled = False

TxtApplicationId.Enabled = False

BtnUpdate.Enabled = True

End Sub

0
Princy
Top achievements
Rank 2
answered on 20 Oct 2008, 05:47 AM
Hello Abdul,

The rows in a Grid are accessed as GridDataItems. To access the cells as such in these rows you have to use the UniqueName property of the column.
aspx:
<telerik:GridBoundColumn DataField="categorydescription" EmptyDataText="&amp;nbsp;" HeaderText="Description" SortExpression="categorydescription" UniqueName="categorydescription"</telerik:GridBoundColumn> 

Now to access a cell in the row which was selected you can use the UniqueName property of  the column as shown.
vb:
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs) 
     ' to access each row in RadGrid1 
    For Each dataItem As GridDataItem In RadGrid1.Items 
         If dataItem.Selected Then 
           ' use the column UniqueName (categorydescription ) to access the cell and get its Text 
           Dim strtxt As String = dataItem("categorydescription").Text 
         End If 
    Next 
End Sub 
 

You can also use the DataKeyName property of the grid to retrieve the cell text.
aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" >         
   <MasterTableView DataKeyNames="FirstName"
      .... 

vb:
Protected Sub RadGrid1_SelectedIndexChanged(sender As Object, e As EventArgs) 
    For Each dataItem As GridDataItem In RadGrid1.Items 
        ' To get the DataKey(GetDataKeyValue for RadGrid is similar to SelectedDataKey for Asp.Net GridView) 
               Dim strtxt As String = dataItem.GetDataKeyValue("Name").ToString() 
 
                 ' custom data binding code goes here    
    Next 
End Sub 

Thanks
Princy.
0
Jonathan
Top achievements
Rank 1
answered on 31 Oct 2008, 06:04 AM

Hi,  following your post about getting the cell values, I have a grid where some of the columns are data bound to a list, some are itemtemplate columns that have a dropdownlist in them and I would like to retrieve the value of the selected item in the dropdownlist for each selected row.  I am using a GridClientSelectColumn to select the rows and for each selected row I choose a value in the dropdownlist.  The grid never goes into edit mode although the dropdownlists are editable at all times.

My question is can I access the dropdownlist for the selected row through the unique name for that colum and the SelectedItems property of the RadGrid as in:

 
foreach (GridDataItem dataitem in RadGrid1.SelectedItems)
{  
DropDownList ddlRelation1 = (DropDownList)dataitem["RelationtoClientColumn"].Controls[0];   

 

 

 

If not how can I access the dropdownlist values for each selected row?

 

 

0
Shinu
Top achievements
Rank 2
answered on 31 Oct 2008, 06:30 AM
Hi Jonathan,

Yes it is possible to access the DropDownList as you have mentioned above. For that you need to set the AutoPostBack property of the DropDownList to true. You can access the DropDownList in the ItemTemplate of a GridTemplateColumn using its ControlID. Here is the code I tried on my end.

ASPX:
<telerik:GridTemplateColumn    HeaderText="TempCol"  DataField="EndDate"  UniqueName="TempCol" > 
  
  <ItemTemplate> 
      <asp:DropDownList ID="DropDownList1"  AutoPostBack="true" runat="server"
       <asp:ListItem Text="Asia" ></asp:ListItem> 
        <asp:ListItem Text="Europe" ></asp:ListItem> 
         <asp:ListItem Text="Africa" ></asp:ListItem> 
      </asp:DropDownList> 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 


CS:
   protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
       foreach (GridDataItem dataitem in RadGrid1.SelectedItems) 
        { 
 
            DropDownList ddl = (DropDownList)dataitem["TempCol"].FindControl("DropDownList1"); 
                string selectedText = ddl.SelectedItem.Text; 
             
        } 
    } 
 


Thanks
Shinu
0
Ramachandran Panduranga
Top achievements
Rank 1
answered on 11 Dec 2008, 05:06 AM
Hi Princy,

I tried the same piece of code for retrieving data from the selected row of the RadGrid but Its not working, I doubt on the grid_PreRender event because after selecting the row on the grid there seems no action.

Is there any other event that I try for?

Please help me ASAP.


Thanks,
Ram
0
Princy
Top achievements
Rank 2
answered on 11 Dec 2008, 07:04 AM
Hello Ramachandran,

The code given above to retrieve data from selected rows will work only if the rows are initially selected or on normal postbacks but if you want to retrieve data from a row on selecting it, then you have to set the EnablePostBackOnRowClick property to true for the grid as shown below which will help fire the PreRender or SelectedIndexChanged events of the grid on selecting a row.
aspx:
<telerik:RadGrid  ID="RadGrid1" runat="server" OnPreRender="RadGrid1_PreRender"  OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged" >          
   <MasterTableView>        
   </MasterTableView>         
      <ClientSettings  EnablePostBackOnRowClick="true">          
      </ClientSettings> 
</telerik:RadGrid> 

Thanks
Princy.
Tags
Grid
Asked by
Abdul Kadar
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Princy
Top achievements
Rank 2
Abdul Kadar
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Ramachandran Panduranga
Top achievements
Rank 1
Share this question
or