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

[Solved] RAD Grid batch update with combo box

1 Answer 178 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rahul
Top achievements
Rank 1
Rahul asked on 18 Jun 2014, 12:56 PM
Hi,

I have taken a RAD Grid having 3 combo box columns, I wan to implement batch update and need to update the database on code behind, please let me know which event of grid should I handel for inserting new item (single row) & batch update modified items to database (only modified rows). also please let me know how can i get the values from grid to insert in database for insert new row or for bulk update.

below is my code

.aspx page
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
        CellSpacing="-1" GridLines="Both" onneeddatasource="RadGrid1_NeedDataSource"
        onprerender="RadGrid1_PreRender">
        <MasterTableView EditMode = "Batch" CommandItemDisplay ="TopAndBottom">
            <Columns>
                <telerik:GridBoundColumn DataField="trans"
                    FilterControlAltText="Filter column column" HeaderText="TransactionID"
                    UniqueName="column">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="EmployeeName"
                    FilterControlAltText="Filter EmployeeName column"
                    UniqueName="EmployeeName" HeaderText="Employee Name">
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="RCBEmployee" Runat="server"
                            EmptyMessage="Select Employee">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <%# Eval("EmployeeName") %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn DataField="Department"
                    FilterControlAltText="Filter Department column" UniqueName="Department"
                    HeaderText="Department">
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="rcbdept" Runat="server">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                         <%# Eval("Department") %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn DataField="Skill"
                    FilterControlAltText="Filter Skill column" UniqueName="Skill"
                    HeaderText="Skill">
                    <EditItemTemplate>
                        <telerik:RadComboBox ID="RCBSkill" Runat="server">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <%# Eval("Skill") %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        <BatchEditingSettings EditType ="Row" />
        </MasterTableView>
    </telerik:RadGrid>

ASPX.CS

 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = PopulateGrid();
    }
 protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
       //Populating Employee
        RadComboBox combo = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_EmployeeName").FindControl("RCBEmployee") as RadComboBox;
        combo.DataSource = PopulateEmoloyee();
        combo.DataTextField = "EmployeeName";
        combo.DataValueField = "EmployeeID";
        combo.DataBind();       
//populating Department
        RadComboBox combodept = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_Department").FindControl("rcbdept") as RadComboBox;
        combodept.DataSource = PopulateDepartment();
        combodept.DataTextField = "Department";
        combodept.DataValueField = "deptid";
        combodept.DataBind();   
}

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 23 Jun 2014, 08:57 AM
Hello Rahul,

You can extract both the values of the insert and update records by subscribing to the OnBatchEditCommand as illustrated here. Additionally you can check the command type thus allowing you to perform the respective CRUD operation.

C#:
protected void RadGrid1_BatchEditCommand(object sender, Telerik.Web.UI.GridBatchEditingEventArgs e)
    {
        foreach (GridBatchEditingCommand command in e.Commands)
        {
            Hashtable newValues = command.NewValues;
            Hashtable oldValues = command.OldValues;
            string productName;
            if (command.Type == GridBatchEditingCommandType.Insert)
            {
                productName = newValues["ProductName"].ToString();
            }
 
            if (command.Type == GridBatchEditingCommandType.Update)
            {
                productName = newValues["ProductName"].ToString();
            }
        }
    }

Alternatively you can use the OnItemCommand, OnUpdateCommand or OnInsertCommand event handlers for achieving the same but in such cases you should cast the command argument to GridBatchEditingEventArgument(as demonstrated for the OnItemCommand in the above linked article).

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Rahul
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or