how to find row values of a selected row in a radgrid

2 posts, 0 answers
  1. Miriam
    Miriam avatar
    12 posts
    Member since:
    Mar 2017

    Posted 27 Sep 2018 Link to this post

    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.

     

     

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4492 posts

    Posted 01 Oct 2018 Link to this post

    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.
Back to Top