This is a migrated thread and some comments may be shown as answers.

How can I get the Correct Row in automatic edit mode

0 Answers 10 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 08 May 2018, 12:03 PM

I have a simple RadGrid with this design:

<div id="Grid">
                    <telerik:RadGrid RenderMode="Lightweight"
                         runat="server"
                         AllowPaging="true"
                         ID="RadGrid1"
                         AutoGenerateColumns="false"
                         OnItemCommand="RadGrid1_ItemCommand"
                         OnItemCreated="RadGrid1_ItemCreated"
                         OnPreRender="RadGrid1_PreRender1"
                         OnItemDataBound="RadGrid1_ItemDataBound"
                         OnNeedDataSource="RadGrid1_NeedDataSource"
                         AllowAutomaticDeletes="false"
                         AllowAutomaticInserts="true"
                         AllowAutomaticUpdates="true"
                         >
                        <MasterTableView DataKeyNames="UserID"
                             CommandItemDisplay="Top"
                             InsertItemPageIndexAction="ShowItemOnCurrentPage">
                            <Columns>
 
                                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="UserID" HeaderText="User ID" ReadOnly="true"
                                    ForceExtractValue="Always" ConvertEmptyStringToNull="true" Visible="false"/>
                                <telerik:GridTemplateColumn UniqueName="ActiveColumn" HeaderText="Active">
                                    <ItemTemplate>
                                        <asp:Label ID="lblActive" runat="server" Text='<%# Convert.ToBoolean(Eval("Active")) == true ? "Yes" : "No" %>'></asp:Label>
                                    </ItemTemplate>
                                    <InsertItemTemplate>
                                        <asp:CheckBox runat ="server" ID="chkInsertActive" />
                                    </InsertItemTemplate>
                                    <EditItemTemplate>
                                        <asp:CheckBox runat="server" ID ="chkActive" Checked='<%# DataBinder.Eval(Container.DataItem,"Active") %>'  />
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn HeaderText="Role">
                                    <ItemTemplate>
                                            <%#DataBinder.Eval(Container.DataItem,"Role") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlRole" runat="server" ></asp:DropDownList>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn UniqueName="FirstNameColumn" HeaderText="First Name">
                                    <ItemTemplate>
                                            <%# DataBinder.Eval(Container.DataItem,"FirstName") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* First Name Is Required" ForeColor="Red"
                                             ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn HeaderText="Last Name">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Last Name Is Required" ForeColor="Red"
                                             ControlToValidate="txtLastName"></asp:RequiredFieldValidator>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn HeaderText="User Name">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "UserName") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* User Name Is Required" ForeColor="Red"
                                             ControlToValidate="txtUserName"></asp:RequiredFieldValidator>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn HeaderText="Email Address">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "EmailAddress") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("EmailAddress") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="* Email Is Required" ForeColor="Red"
                                             ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridTemplateColumn>
                                    <ItemTemplate>
                                        <asp:Button ID="btnDelete" CssClass="btn btn-xs btn-danger" Text="Delete" runat="server" OnClick="BtnDelete_Click" OnClientClick="confirmAspButton(this); return false;">
                                        </asp:Button>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
 
                        </MasterTableView>
                        <PagerStyle Mode="NextPrevAndNumeric" />
                    </telerik:RadGrid>
                </div>

 

 Then I created C# code to filter the Grid by user and or Role with this code.  I am not showing all of the code just enough to give a picture of what I am working with:

if (txtName.Text.Length > 0 && ddlRole.SelectedIndex == 0)
{
    string a = txtName.Text;
    using (ExpungeEntities db = new ExpungeEntities())
    {
        RadGrid1.DataSource = db.USERS_T_DATA.Where(c => c.FirstName.Contains(a)
            || c.LastName.Contains(a)).ToList();
        {
            RadGrid1.Rebind();
            int intGridCount = RadGrid1.Items.Count;
            if (intGridCount == 0)
            {
                DisplayMessage("No records matched your search");
                txtName.Text = string.Empty;
            }
        }
         
         
    }
}

 

The code works as expected and I endup with a filtered grid. but here is the issue I am trying to solve.  When I click the "edit" icon that was created by my use of the AllowAutomaticUpdates I am presented with the first row in the grid for updating instead of the intended row.  I have taken a screen shot that shows the issue to help explain.

How can I get the correct row to display for edit?

 

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Share this question
or