This is a migrated thread and some comments may be shown as answers.
how to find row values of a selected row in a radgrid
1 Answer 45 Views
This is a migrated thread and some comments may be shown as answers.
Miriam
Top achievements
Rank 1
Miriam asked on 27 Sep 2018, 06:26 PM

Hi

how to find row values of a selected row in a radgrid? c#

I have a radbutton inside a radgrid.

 

<telerik:RadGrid ID="dg" runat="server" AllowSorting="true" Width="100%"
                        RenderMode="Auto" PageSize="10" AutoGenerateColumns="false" 
                        AllowPaging="true">
                        <PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" PageSizeControlType="RadComboBox"
                            AlwaysVisible="true"></PagerStyle>
                        <MasterTableView HierarchyDefaultExpanded="false">
                            <CommandItemSettings AddNewRecordText="Nuevo" CancelChangesText="Cancelar" RefreshText="Actualizar"
                                ShowRefreshButton="false" />
                            <Columns>                                
                                <telerik:GridTemplateColumn HeaderText="Id" Visible="false">
                                    <ItemTemplate>
                                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("Idtip") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="lblIDEd" runat="server" Text='<%# Eval("Idtip") %>' Visible="false"></asp:Label>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn HeaderText="Tipo">
                                    <ItemTemplate>
                                        <asp:Label ID="lblDescr" runat="server" Text='<%# Eval("Nombre") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtEditDescr" runat="server" Text='<%# Eval("Nombre") %>' Width="220px"></asp:TextBox>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn>
                                    <ItemTemplate>
                                        <telerik:RadButton ID="btnEditlist" runat="server" OnClick="btnEditlist_Click" Text="" RenderMode="Auto" CausesValidation="false">
                                            <Icon PrimaryIconCssClass="rbEdit" />
                                        </telerik:RadButton>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <%--<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                </telerik:GridEditCommandColumn>--%>
                                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" />
                            </Columns>
                        </MasterTableView>
                        <ClientSettings EnableRowHoverStyle="true">
                        </ClientSettings>
                    </telerik:RadGrid>

and the button event 

protected void btnEditlist_Click(object sender, EventArgs e)
        {
             GridItem reng = sender as RadButton.NamingContainer as GridItem;
             GridDataItem Info = sender as RadButton.NamingContainer as GridDataItem;
             string Id = reng.FindControl("lblID") as Label.Text;
        }

 

But I can´t find the row values.

 

 

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 01 Oct 2018, 05:08 PM
Hello Miriam,

I advise that you first examine the following two sections of the documentation on getting data from a grid and on accessing controls in a template column:

Here's an example that applies that to the provided snippet where I highlighted the key changes:

protected void dg_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = GetDummyData();
}
 
protected DataTable GetDummyData()
{
    DataTable tbl = new DataTable();
    tbl.Columns.Add(new DataColumn("Idtip", typeof(decimal)));
    tbl.Columns.Add(new DataColumn("Nombre", typeof(string)));
    tbl.Columns.Add(new DataColumn("otherField", typeof(string)));
    tbl.Columns.Add(new DataColumn("FourthField", typeof(string)));
    tbl.Rows.Add(new object[] { 1, "one", 11, "red" });
    tbl.Rows.Add(new object[] { 2, "two", 22, "green" });
    tbl.Rows.Add(new object[] { 3, "three", 33, "blue" });
    tbl.Rows.Add(new object[] { 4, "four", 44, "pink" });
 
    return tbl;
}
 
protected void btnEditlist_Click(object sender, EventArgs e)
{
    GridDataItem grdRow = (sender as Control).NamingContainer as GridDataItem;
    string data = grdRow.GetDataKeyValue("Idtip").ToString();
    Response.Write(data);
}
<telerik:RadGrid ID="dg" runat="server" AllowSorting="true" Width="100%"
    RenderMode="Auto" PageSize="10" AutoGenerateColumns="false"
    AllowPaging="true"
    OnNeedDataSource="dg_NeedDataSource">
    <PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" PageSizeControlType="RadComboBox"
        AlwaysVisible="true"></PagerStyle>
    <MasterTableView HierarchyDefaultExpanded="false" DataKeyNames="Idtip">
        <CommandItemSettings AddNewRecordText="Nuevo" CancelChangesText="Cancelar" RefreshText="Actualizar"
            ShowRefreshButton="false" />
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Id" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Eval("Idtip") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblIDEd" runat="server" Text='<%# Eval("Idtip") %>' Visible="false"></asp:Label>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Tipo">
                <ItemTemplate>
                    <asp:Label ID="lblDescr" runat="server" Text='<%# Eval("Nombre") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditDescr" runat="server" Text='<%# Eval("Nombre") %>' Width="220px"></asp:TextBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <telerik:RadButton ID="btnEditlist" runat="server" OnClick="btnEditlist_Click" Text="" RenderMode="Auto" CausesValidation="false">
                        <Icon PrimaryIconCssClass="rbEdit" />
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <%--<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                        </telerik:GridEditCommandColumn>--%>
            <telerik:GridButtonColumn Text="Delete" CommandName="Delete" />
        </Columns>
    </MasterTableView>
    <ClientSettings EnableRowHoverStyle="true">
    </ClientSettings>
</telerik:RadGrid>


Regards,
Marin Bratanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Miriam
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or