Cannot Get Value from A GridBoundCoulmn

1 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 19 Apr 2018 Link to this post

    I have a RadGrid setup like this

    <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="EditForms">
                                <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:GridBoundColumn DataField="LastName" HeaderText="Last Name"  UniqueName ="LastName"/>
                                    <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" UniqueName="UserName" />
                                    <telerik:GridBoundColumn DataField="EmailAddress" HeaderText="Email Address" UniqueName="Email" />
                                    <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 want to udpade or add new records using C# code behind. However I cannot get access to any of the controls.  When thew code attemps to read the value of the value of the control, in this case a textbox, I get an error message stating {"Object reference not set to an instance of an object."}  here is the 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.FirstName = (userControl.FindControl("FirstName") as TextBox).Text;
     
                }
            }

    Can you please tell me what the problem is?

     

Back to Top