Access checkbox for inline edit of a radgrid

1 Answer 16 Views
Grid
Joel R
Top achievements
Rank 1
Joel R asked on 14 Nov 2022, 01:24 PM | edited on 14 Nov 2022, 01:25 PM

I need to enable/disable a checkbox that is in edit mode.  I have tried in PreRender and ItemDatabound to FindControl but have not found a way. I do have access to the data for the decision of enable/disable.

 

Any help is appreciated

 


<telerik:RadGrid ID="RadGridUserRole" DataSourceID="ObjectDataSourceUserRole" AllowMultiRowEdit="True"
    OnPreRender="RadGridUserRole_PreRender" runat="server" Width="205px" AutoGenerateColumns="False"
    GridLines="None">
    <MasterTableView CommandItemDisplay="none" EditMode="InPlace" AutoGenerateColumns="false"
        Width='100%' DataKeyNames="RoleName">
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Role" HeaderStyle-Width='65px' UniqueName="Role">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBoxRole" Checked='<%# Eval("UserInRole") %>' Text='<%# Eval("RoleName") %>' runat="server" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 


public void RadGridUserRole_PreRender(object sender, System.EventArgs e)
    {
            foreach (GridItem item in ((RadGrid)sender).MasterTableView.Items)
            {
                if (item is GridEditableItem)
                {
                    GridEditableItem editableItem = (GridDataItem)item;
                    editableItem.Edit = true;
                }
            }
            ((RadGrid)sender).Rebind();
    }

1 Answer, 1 is accepted

Sort by
1
Accepted
Doncho
Telerik team
answered on 17 Nov 2022, 09:39 AM

Hello Joel,

The ItemDataBound event is usually the most suitable one for this purpose.

I would suggest you check out the following resources to get a better idea of how to access the CheckBox when the GridItem turns into Edit mode:

Here is a basic sample you can test:

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateEditColumn="true" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID" EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" DataType="System.Int32"
                FilterControlAltText="Filter ID column" HeaderText="ID"
                ReadOnly="True" SortExpression="ID" UniqueName="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name"
                FilterControlAltText="Filter Name column" HeaderText="Name"
                SortExpression="Name" UniqueName="Name">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Role" HeaderStyle-Width='65px' UniqueName="Role">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBoxRole" runat="server" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
C#
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = Enumerable.Range(1, 20).Select(x => new { ID = x, Name = "Name " + x });
}

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
        {
            // insert item
        }
        else
        {
            // edit item
            var gridEditableItem = e.Item as GridEditableItem;
            var checkBox = gridEditableItem["Role"].FindControl("CheckBoxRole") as CheckBox;
            int id = Int32.Parse(gridEditableItem.GetDataKeyValue("ID").ToString());
            if (id % 2 == 0)
                checkBox.Enabled = true;
            else
                checkBox.Enabled = false;
        }
    }
}
I hope this helps.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Joel R
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or