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

How to update multiple rows with the same value

2 Answers 363 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Blake
Top achievements
Rank 1
Blake asked on 04 Mar 2009, 09:55 PM
I need to create a way for users to 'bulk edit' data in a datagrid, for example changing the AssignedTo value of every row in the datagrid (or at least every row where a checkbox is checked) at once. Here is how I was hoping to do it but I am not sure how.

The user has a grid full of data.
The first column is checkboxes.
The user checks the rows he wants to update

In the first row there would be a field that would correspond to the data in that column. For example there is an AssignedTo column. At the top of that column would be a combobox which the user would choose a name from and then hit a button and it would update all of the rows that were checked. If I can't put the edit controls in the first row of the grid I would be happy to put them outside of the grid as well.

I haven't found any examples of this. Could someone let me know if this is feasible and how I might go about it.

Thanks,
Blake

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Mar 2009, 08:19 AM
Hello Blake,

You can place the combobox in the CommandItemTemplate of the grid. On clicking the update button, you can access the combobox in the CommandItemTemplate and get its value. Then loop through the rows and find out if the checkboxes are checked and update your database accordingly. To understand better check out the code below:
aspx:
<telerik:RadGrid  ID="RadGrid1" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" runat="server" > 
     <MasterTableView CommandItemDisplay="Top" DataSourceID="SqlDataSource1">        
     <CommandItemTemplate> 
     <telerik:RadComboBox ID="RadComboBox1" DataSourceID="SqlDataSource1" DataTextField="AssignedTo" DataValueField="AssignedTo" runat="server"
            <CollapseAnimation Duration="200" Type="OutQuint" /> 
        </telerik:RadComboBox> 
           
        <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" /> 
     </CommandItemTemplate> 
   <Columns> 
        <telerik:GridTemplateColumn UniqueName="TemplateColumnCheckBox"
        <ItemTemplate> 
            <asp:CheckBox ID="CheckBox1" runat="server" /> 
        </ItemTemplate> 
        </telerik:GridTemplateColumn> 
        <telerik:GridBoundColumn DataField="AssignedTo" HeaderText="AssignedTo" UniqueName="AssignedTo"></telerik:GridBoundColumn> 
         .... 

cs:
protected void Button1_Click(object sender, EventArgs e) 
    { 
        GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
        RadComboBox combo = (RadComboBox)cmdItem.FindControl("RadComboBox1");   
        string strtxt = combo.Text;       
        foreach (GridDataItem item in RadGrid1.Items) 
        { 
            if (((CheckBox)item["TemplateColumnCheckBox"].FindControl("CheckBox1")).Checked) 
            { 
                //update query  
            } 
        } 
    } 
Also if you want the rows to be put in EditMode, you can do it on the CheckChanged event of the checkbox.

Thanks
Princy.
0
Blake
Top achievements
Rank 1
answered on 09 Mar 2009, 02:12 PM
Thanks for the response. I got side tracked for a few days but I will try that out today.

bb
Tags
Grid
Asked by
Blake
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Blake
Top achievements
Rank 1
Share this question
or