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

Radgrid - Put certain rows in edit mode

7 Answers 844 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ramesh
Top achievements
Rank 1
Ramesh asked on 12 Apr 2012, 08:43 PM
I am trying to set certain rows on my grid in edit mode automatically on page load based on the content of one of the columns. I have seen some examples on this forum where either all rows are automatically put into edit mode during PreRender or by manually selecting multiple rows and firing a command event.

Please provide some examples on how certain rows can be automatically be put in edit mode during page load.

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Apr 2012, 05:35 AM
Hello Ramesh,

Try the following code to achieve your scenario.
C#:
protected void grid1_PreRender(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
   foreach (GridItem item in grid1.MasterTableView.Items)
   {
     if (item is GridEditableItem)
     {
       GridEditableItem editableItem = item as GridDataItem;
       TableCell cell = (TableCell)editableItem["UniqueName"];
       if (cell.Text == "Text")
       {
         editableItem.Edit = true;
       }
     }
   }
    grid1.Rebind();
 }
}

Thanks,
Princy.
0
Ray
Top achievements
Rank 1
answered on 28 Oct 2015, 12:58 PM

Hi Princy,

Is there some setting that must be in place for this to work?  It executes the "editableItem.Edit = true;" line for every row in my grid but only the last one stays in edit mode.

It is as if when it puts each row in edit mode, it takes the previous row out of edit mode, leaving the last row in edit mode.

Thanks,

Ray

0
Eyup
Telerik team
answered on 02 Nov 2015, 11:45 AM
Hello Ray,

You will also need to enable the AllowMultiRowEdit property in the main RadGrid tag.
Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ray
Top achievements
Rank 1
answered on 03 Nov 2015, 07:40 PM

Hi Eyup,

Yes, this worked.  Thank you!!

 Ray

0
Binu
Top achievements
Rank 1
answered on 23 Mar 2021, 04:39 AM

Hi, I am using the same code, now my textbox is in edit mode on page load. But after enter the value into the textbox, it is still in edit mode. So when I click "Save all" button, I can't able to get the value in "_BatchEditCommand". e.Commands.Count is 0 always because my text box is always in editable mode. Please someone help me  to solve this issue thanks. my code is as follows...

Protected Sub gvApproverRFMI_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
                    For Each item As GridItem In gvApproverRFMI.MasterTableView.Items
                        Dim strLabel As String = (TryCast(item.FindControl("lblReply"), Label)).Text
                        If TypeOf item Is GridEditableItem Then
                            Dim editableItem As GridEditableItem = TryCast(item, GridDataItem)
                            Dim cell As TableCell = CType(editableItem("Reply"), TableCell)
                            If strLabel = "" Then
                                editableItem.Edit = True
                            End If
                        End If
                    Next
                    gvApproverRFMI.Rebind()               
        End If
    End Sub

Protected Sub gvApproverRFMI_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs)
        If (e.Commands.Count > 0) Then
            For Each command As GridBatchEditingCommand In e.Commands
                If (command.Type = GridBatchEditingCommandType.Update) Then

                    Dim newValues As Hashtable = command.NewValues
                    Dim oldValues As Hashtable = command.OldValues
                    Dim RFMI_ID As String = newValues("RFMI_ID").ToString()
                    Dim Approver_ID As String = newValues("RFMI_EMP_ID").ToString()
                    Dim Reply As String = newValues("Reply").ToString()
                    Reply = Reply.Replace("'", "''")
                    'update master info
                    SaveData("Update")
                End If
            Next
        Else
            ScriptManager.RegisterStartupScript(Me, Me.GetType, "alert", "alert('Please enter your reply in Reply field at Approval Input section');", True)
        End If
        'Rebind grid
        Dim dt As DataTable = clsDoc.RetrieveApproverRFMI(ViewState("DocID"), intCurrentLevel)
        gvApproverRFMI.DataSource = dt
        gvApproverRFMI.Rebind()
    End Sub

<telerik:RadGrid ID="gvApproverRFMI" runat="server" AllowAutomaticUpdates="True" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
                                            OnNeedDataSource="gvApproverRFMI_NeedDataSource" OnItemCreated="gvApproverRFMI_ItemCreated" OnBatchEditCommand="gvApproverRFMI_BatchEditCommand" OnItemDataBound="gvApproverRFMI_ItemDataBound"
                                            EnableViewState="False" EnableLinqExpressions="false" OnPreRender="gvApproverRFMI_PreRender" AllowMultiRowEdit="true"
                                            EnableAJAX="True" GroupPanelPosition="Top" PageSize="15" Width="90%"
                                            FilterItemStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" AlternatingItemStyle-HorizontalAlign="Left">
                                            <GroupingSettings CaseSensitive="False" CollapseAllTooltip="Collapse all groups" />
                                            <ClientSettings AllowColumnHide="True" AllowColumnsReorder="True" AllowKeyboardNavigation="True" AllowRowHide="True" ReorderColumnsOnClient="True">
                                                <Resizing AllowColumnResize="True" ClipCellContentOnResize="False" EnableRealTimeResize="True" ResizeGridOnColumnResize="True" />
                                                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                                                <KeyboardNavigationSettings AllowSubmitOnEnter="True" EnableKeyboardShortcuts="False" />
                                                <Selecting AllowRowSelect="True" />
                                                <ClientEvents OnBatchEditCellValueChanged="gridCellChanged" OnUserAction="UserAction" OnBatchEditOpening="OnBatchEditOpening" />
                                            </ClientSettings>
                                            <MasterTableView CommandItemDisplay="TopAndBottom" DataKeyNames="RFMI_ID, RFMI_EMP_ID" EditMode="Batch" HorizontalAlign="Justify" 
                                                PageSize="15" Width="100%" CommandItemStyle-Font-Bold="true" CommandItemStyle-HorizontalAlign="Right" CommandItemStyle-Height="30px">
                                                <CommandItemSettings ShowAddNewRecordButton="False" ShowRefreshButton="False" ShowCancelChangesButton="false" ShowSaveChangesButton="false"/>
                                                <CommandItemTemplate>
                                                    <asp:Button ID="btnSaveAll" CssClass="button" runat="server" Text="Reply To Question" OnClientClick="initSaveAll(); return false;" />&nbsp;
                                                </CommandItemTemplate>
                                                <NoRecordsTemplate>
                                                    No Data Found.
                                                </NoRecordsTemplate>
                                                <Columns>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RFMI_ID" HeaderText="RFMI_ID" SortExpression="RFMI_ID" UniqueName="RFMI_ID" Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRFMIID" Text='<%# Eval("RFMI_ID")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RFMI_EMP_ID" HeaderText="RFMI_EMP_ID" SortExpression="RFMI_EMP_ID" UniqueName="RFMI_EMP_ID" Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRFMIEMPID" Text='<%# Eval("RFMI_EMP_ID")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="Approval_Status" FilterControlAltText="Filter WF Level" FilterDelay="1000" HeaderText="WF Level" SortExpression="Approval_Status" UniqueName="Approval_Status">
                                                        <FilterTemplate>
                                                            <telerik:RadComboBox ID="cmbWFLevel" DataTextField="Approval_Status" DataValueField="Approval_Status" UniqueName="cmbWFLevel"
                                                                AppendDataBoundItems="true" AutoPostBack="false" SelectedValue='<%# (TryCast(Container, GridItem)).OwnerTableView.GetColumn("Approval_Status").CurrentFilterValue%>'
                                                                runat="server" OnClientSelectedIndexChanged="WFLevel_Changed" Width="90%">
                                                                <Items>
                                                                    <telerik:RadComboBoxItem Text="-ALL-" Value="" />
                                                                </Items>
                                                            </telerik:RadComboBox>
                                                            <telerik:RadScriptBlock ID="sbWFLevel" runat="server">
                                                                <script type="text/javascript">
                                                                    function WFLevel_Changed(sender, args) {
                                                                        var tableView = $find("<%# (TryCast(Container, GridItem)).OwnerTableView.ClientID%>");
                                                                        tableView.filter("Approval_Status", args.get_item().get_value(), "EqualTo");
                                                                    }
                                                                </script>
                                                            </telerik:RadScriptBlock>
                                                        </FilterTemplate>
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblWFLevel" Text='<%# Eval("Approval_Status")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <HeaderStyle Width="13%" />
                                                        <ItemStyle Width="13%" />
                                                    </telerik:GridTemplateColumn>

                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RFMI_EMP_Name" FilterControlAltText="Filter Approver column" FilterDelay="1000" HeaderText="Approver" SortExpression="RFMI_EMP_Name" UniqueName="RFMI_EMP_Name">
                                                        <FilterTemplate>
                                                            <telerik:RadComboBox ID="cmbRFMIEmp" DataTextField="RFMI_EMP_Name" DataValueField="RFMI_EMP_Name" UniqueName="cmbRFMIEmp"
                                                                AppendDataBoundItems="true" AutoPostBack="false" SelectedValue='<%# (TryCast(Container, GridItem)).OwnerTableView.GetColumn("RFMI_EMP_Name").CurrentFilterValue%>'
                                                                runat="server" OnClientSelectedIndexChanged="RFMIEmp_Changed" Width="90%">
                                                                <Items>
                                                                    <telerik:RadComboBoxItem Text="-ALL-" Value="" />
                                                                </Items>
                                                            </telerik:RadComboBox>
                                                            <telerik:RadScriptBlock ID="sbRFMIEmp" runat="server">
                                                                <script type="text/javascript">
                                                                    function RFMIEmp_Changed(sender, args) {
                                                                        var tableView = $find("<%# (TryCast(Container, GridItem)).OwnerTableView.ClientID%>");
                                                                        tableView.filter("RFMI_EMP_Name", args.get_item().get_value(), "EqualTo");
                                                                    }
                                                                </script>
                                                            </telerik:RadScriptBlock>
                                                        </FilterTemplate>
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRFMIEmp" Text='<%# Eval("RFMI_EMP_Name")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <HeaderStyle Width="13%" />
                                                        <ItemStyle Width="13%" />
                                                    </telerik:GridTemplateColumn>

                                                    <telerik:GridTemplateColumn DataField="Created_Dt" HeaderText="RFMI Date" SortExpression="Created_Dt" UniqueName="Created_Dt">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblCreatedDt" Text='<%# Eval("Created_Dt")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <HeaderStyle Width="9%" />
                                                        <ItemStyle Width="9%" />
                                                    </telerik:GridTemplateColumn>

                                                    <telerik:GridTemplateColumn DataField="Question" AllowFiltering="true" HeaderText="Question" SortExpression="Question" UniqueName="Question" FilterControlWidth="50%">
                                                        <HeaderStyle Font-Size="12px" Width="24%" />
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblQuestion" Text='<%# Eval("Question")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <ItemStyle Width="24%" />
                                                    </telerik:GridTemplateColumn>

                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RTQ_EMP_Name" FilterControlAltText="Filter Reply Person column" FilterDelay="1000" HeaderText="Reply Person" SortExpression="RTQ_EMP_Name" UniqueName="RTQ_EMP_Name" Visible="false">
                                                        <FilterTemplate>
                                                            <telerik:RadComboBox ID="cmbRTQEmp" DataTextField="RTQ_EMP_Name" DataValueField="RTQ_EMP_Name" UniqueName="cmbRTQEmp"
                                                                AppendDataBoundItems="true" AutoPostBack="false" SelectedValue='<%# (TryCast(Container, GridItem)).OwnerTableView.GetColumn("RTQ_EMP_Name").CurrentFilterValue%>'
                                                                runat="server" OnClientSelectedIndexChanged="RTQEmp_Changed" Width="50%">
                                                                <Items>
                                                                    <telerik:RadComboBoxItem Text="-ALL-" Value="" />
                                                                </Items>
                                                            </telerik:RadComboBox>
                                                            <telerik:RadScriptBlock ID="sbRTQEmp" runat="server">
                                                                <script type="text/javascript">
                                                                    function RTQEmp_Changed(sender, args) {
                                                                        var tableView = $find("<%# (TryCast(Container, GridItem)).OwnerTableView.ClientID%>");
                                                                        tableView.filter("RTQ_EMP_Name", args.get_item().get_value(), "EqualTo");
                                                                    }
                                                                </script>
                                                            </telerik:RadScriptBlock>
                                                        </FilterTemplate>
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRTQEmp" Text='<%# Eval("RTQ_EMP_Name")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <HeaderStyle Width="8%" />
                                                        <ItemStyle Width="8%" />
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn DataField="Reply" AllowFiltering="true" HeaderText="Reply" SortExpression="Reply" UniqueName="Reply" FilterControlWidth="50%">
                                                        <HeaderStyle ForeColor="#0000ff" Font-Size="12px" Width="25%" />
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblReply" Text='<%# Eval("Reply")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <telerik:RadTextBox ID="txtReply" runat="server" Width="100%" TextMode="MultiLine" Rows="2"></telerik:RadTextBox>
                                                        </EditItemTemplate>
                                                        <ItemStyle Width="25%" />
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn DataField="Last_Modified_Dt" HeaderText="Reply Date" SortExpression="Last_Modified_Dt" UniqueName="Last_Modified_Dt">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblReplyDt" Text='<%# Eval("Last_Modified_Dt")%>'></asp:Label>
                                                        </ItemTemplate>
                                                        <HeaderStyle Width="8%" />
                                                        <ItemStyle Width="8%" />
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="STATUS" HeaderText="STATUS" SortExpression="STATUS" UniqueName="STATUS" Display="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblStatus" Text='<%# Eval("STATUS")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RTQ_EMP_ID" HeaderText="RTQ_EMP_ID" SortExpression="RTQ_EMP_ID" UniqueName="RTQ_EMP_ID" Display="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRTQ_EMP_ID" Text='<%# Eval("RTQ_EMP_ID")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>

                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="Level_ID" HeaderText="Level_ID" SortExpression="Level_ID" UniqueName="Level_ID" Display="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblLevel_ID" Text='<%# Eval("Level_ID")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" DataField="RMI_Status" HeaderText="RMI_Status" SortExpression="RMI_Status" UniqueName="RMI_Status" Display="false">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblRMI_Status" Text='<%# Eval("RMI_Status")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                </Columns>
                                            </MasterTableView>
                                        </telerik:RadGrid>

0
Binu
Top achievements
Rank 1
answered on 24 Mar 2021, 10:32 AM
Hi, I am using the same code, now my textbox is in edit mode on page load. But after enter the value into the textbox, it is still in edit mode. So when I click "Save all" button, I can't able to get the value in "_BatchEditCommand". e.Commands.Count is 0 always because my text box is always in editable mode. Can you please help, how to get the textbox values in the code behind. 
0
Doncho
Telerik team
answered on 25 Mar 2021, 02:08 PM

Hi Binu,

Thank you for sharing the code!

Please note that the BatchEditing mode operates on the client-side which makes it a lot different than the other edit modes exposed by the RadGrid.

In this case, to achieve the desired and initially open a cell for editing depending on the content in another cell you will need to use the client-side API of the Grid and especially the one for BatchEditing, see Batch Editing Client-side API.

To reach a desired cell on the client you can follow the instructions in Accessing Grid Cells, Cell Values and Raw DataKey Values Client-Side article.

Here is a sample code following logic similar to server-side code in the provided snippet:

Wire the OnGridCreated event:

<ClientSettings>
    <ClientEvents OnGridCreated="onGridCreated"/>
</ClientSettings>

JavaScript:

function onGridCreated(sender, args) {
    setTimeout(function () {
        openForEditing(sender);
    })
}
function openForEditing(grid) {
    var items = grid.MasterTableView.get_dataItems();
    for (var i = 0; i < items.length; i++) {
        //get the cell containig the label
        $cell = $(items[i].get_cell("Reply"))
        //get the element representing the label by its ID
        var element = $cell.find('[id$="lblReply"]')[0];
        if (element) {
            //check if the lable text satisfies condition
            if ($(element).text() == "") {
                var batchManager = grid.get_batchEditingManager();
                //open desired cell of the item for editing
                batchManager.openCellForEdit(items[i].get_cell("Reply"));
                return;
            }
        }
    }
}
I hope this will help you achieve the desired.

Have in mind that Batch has its own specifics, see RadGrid Batch Editing Templates and Specifics. Also, note that using complex templates with Batch edit mode goes beyond the capabilities of the RadGrid in Batch edit mode, see Working With Templates.

Please let me know if any questions come up.

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
Ramesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ray
Top achievements
Rank 1
Eyup
Telerik team
Binu
Top achievements
Rank 1
Doncho
Telerik team
Share this question
or