0
votes

I have a very simple Telerik RadGrid I am not using auto generate columns. I am using gridbound columns and creating my own. I have the edit mode set to Inplace. Here is the ASPX for the RAdGid

<div id="Grid">
                <telerik:RadGrid RenderMode="Lightweight" runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true"
                    OnNeedDataSource="UserGrid_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
                    OnItemCreated="RadGrid1_ItemCreated" OnDeleteCommand="RadGrid1_DeleteCommand"
                    OnInsertCommand="RadGrid1_InsertCommand">
                    <MasterTableView DataKeyNames="UserID" CommandItemDisplay="Top" EditMode="InPlace">
                        <Columns>
                            <telerik:GridEditCommandColumn />
                            <telerik:GridBoundColumn DataField="UserID" HeaderText="User ID" ReadOnly="true"
                                ForceExtractValue="Always" ConvertEmptyStringToNull="true" />
                            <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" SortExpression="Active" UniqueName="chkActive"></telerik:GridCheckBoxColumn>
                            <telerik:GridTemplateColumn HeaderText="Role">
                                <ItemTemplate>
                                    <%#DataBinder.Eval(Container.DataItem, "Role")%>
                                </ItemTemplate>
                                <EditItemTemplate>
                              <asp:DropDownList runat="server" ID="ddlRoles">
                                  <asp:ListItem Text ="Admin" Value ="Administrator"></asp:ListItem>
                                  <asp:ListItem Text ="User" Value ="User"></asp:ListItem>
                              </asp:DropDownList>
                          </EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" UniqueName="FirstName" />
                            <telerik:GridButtonColumn ConfirmText="Delete this User?" ConfirmDialogType="RadWindow"
                                ConfirmTitle="Delete" ButtonType="FontIconButton" CommandName="Delete" />
                        </Columns>
                        <EditFormSettings InsertCaption="Add new item" CaptionFormatString="Edit User: {0}" CaptionDataField="FirstName">
                             <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
                    <PagerStyle Mode="NextPrevAndNumeric" />
                </telerik:RadGrid>
            </div>

I have removed several of the columns to shorten the code example. Below is a shortened e C# code

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
        string strUserID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserID"].ToString();
        int intUserId = Convert.ToUInt16(strUserID);

        using (ExpungeEntities db = new ExpungeEntities())
        {

            var Results = db.USERS_T_DATA.SingleOrDefault(i => i.UserID == intUserId);
            if (Results == null)
            {
                RadGrid1.Controls.Add(new LiteralControl("Unable to locate that user for updating"));
                e.Canceled = true;
                return;
            }
            Results.Role = (userControl.FindControl("ddlRoles") as DropDownList).SelectedValue;
            Results.FirstName = (userControl.FindControl("FirstName") as TextBox).Text;

        }
    }

When I run the code I get this error message: {"Object reference not set to an instance of an object."}

It looks like I am not getting a reference to the control. Can anyone tell me what is happening here? I have used this method to get values from a RadGrid using a USerControl to edit or add records, this is the first time I have attempted to use the Inplace editing.

I was able to access the controls but had to eliminate the The Telerik GridBoundColumn and instead use the GridTemplateColumn, ItemTemplat and EidtItemTemplate. In the EditdItemTemplate I use an asp textbox.

I still have an issue, when I click the edit button the pop up form appears but none of the data is populated. Below is an example of my RadGrid Rows.

                                    <ItemTemplate>
                                    <asp:Label ID="lblFirstName" runat="server"
                                        Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                            </telerik:GridTemplateColumn>

And I access the controls with this C# code:

DropDownList ddl = (DropDownList)editableItem.FindControl("ddlRole") as 
DropDownList;
            d.Role = ddl.SelectedValue;
            CheckBox CheckBox1 = editableItem.FindControl("chkActive") as 
CheckBox;
            d.Active = CheckBox1.Checked;
            d.FirstName = (editableItem.FindControl("txtFirstName") as 
TextBox).Text.Trim();
2
On which line do you get that error? - Alexandru Popa
on the Results.Role = (userControl.FindControl("ddlRoles") as DropDownList).SelectedValue; line - Perry

2 Answers

0
votes

From my experience with InPlace edit, you don't need any UserControl to access your edit controls. You can access them directly from GridEditableItem. Try to access your controls like this:

var ddlRoles = editedItem.FindControl("ddlRoles") as DropDownList;
if(ddlRoles != null)
{
    // use your drop down list
}

I tend to use inline temporary variables for safe casting. Here is how I would rewrite your code (if you use C# 7).

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridEditableItem editedItem)
    {
        var strUserID = Convert.ToString(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserID"]);
        var intUserId = Convert.ToUInt16(strUserID);

        using (ExpungeEntities db = new ExpungeEntities())
        {
            var Results = db.USERS_T_DATA.SingleOrDefault(i => i.UserID == intUserId);
            if (Results == null)
            {
                RadGrid1.Controls.Add(new LiteralControl("Unable to locate that user for updating"));
                e.Canceled = true;
                return;
            }

            if (editedItem.FindControl("ddlRoles") is DropDownList ddlRoles)
                Results.Role = ddlRoles.SelectedValue;

            if (editedItem.FindControl("FirstName") is TextBox txtFirstName)
                Results.FirstName = txtFirstName.Text;    
        }
    }
}
0
votes

I was able to solve my problem please take a look at the original post. I added the explanation and the asp and C# code I used to solve the problem.