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

Creating RadGrid dynamically with checkboxes

7 Answers 800 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Julia Shah
Top achievements
Rank 1
Julia Shah asked on 24 Apr 2009, 09:57 PM
Hi,

I have a CardView radgrid that shows accounts.

        <telerik:RadGrid ID="RadGridAccounts" DataSourceID="SqlDataSourceAccounts" runat="server" 
            OnItemDataBound="RadGridAccounts_OnItemDataBound" AllowPaging="True" PageSize="10" ShowHeader="false">  
           <PagerStyle Mode="NextPrevAndNumeric" /> 
           <MasterTableView><ItemTemplate>  
 
        <h4>Account # <%# DataBinder.Eval(Container.DataItem, "AccountNumber") %> </h4>     
 
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>   
        <br /> 
          
        <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder> 
        <br /> 
 
        <asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder> 
                  
    </ItemTemplate> 
</MasterTableView> 
   </telerik:RadGrid> 
 

 

For each account I show different account details in three radgrids that I create dynamically.

                RadGrid1 = new RadGrid();     
                RadGrid1.Skin = "Office2007";     
                RadGrid1.Width = Unit.Percentage(100);     
                RadGrid1.AutoGenerateColumns = false;     
    
                boundColumn = new GridBoundColumn();     
                boundColumn.DataField = "Asset";     
                boundColumn.HeaderText = "Asset";     
                RadGrid1.MasterTableView.Columns.Add(boundColumn);     
    
                boundColumn = new GridBoundColumn();     
                boundColumn.DataField = "Value";     
                boundColumn.HeaderText = "Value";     
                boundColumn.DataFormatString = "{0:C}";     
                RadGrid1.MasterTableView.Columns.Add(boundColumn);     
    
                RadGrid1.DataSource = dt;     
                RadGrid1.DataBind();     
    
                PlaceHolder1.Controls.Add(RadGrid1);     
 

What I need to do is dynamically add a checkbox to each row in the radgrid. When I try this:

                GridCheckBoxColumn checkboxColumn = new GridCheckBoxColumn();  
                checkboxColumn.DataField = "Problem";  
                checkboxColumn.HeaderText = "Problem?";  
                RadGrid1.MasterTableView.Columns.Add(checkboxColumn);  
 

then the checkboxes are disabled. I guess I need to put the grid in edit mode? How would I do that? I don't want the users to edit any columns besides checking the checkboxes.

Another question is - after a user checks some checkboxes and clicks on "Save", how do I refer to the checkboxes inside the grids inside the accounts grid to save the changes to the database?

Thanks,
Julia

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Apr 2009, 05:35 AM
Hi Julia,

Try using a GridTemplateColumn and add the checkbox in its ItemTemplate. Here is a link which explains how to add GridTemplateColumn dynamically.
Programmatic creation

Go through the Section entitled:
Creating template columns programmatically




Shinu
0
Julia Shah
Top achievements
Rank 1
answered on 27 Apr 2009, 03:02 PM
I've seen that example, but I am not sure I can use it in my case. I add my grids programmatically on itemdatabind event of the main grid:

public void RadGridAccounts_OnItemDataBound(Object src, GridItemEventArgs e)  
{  
      if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)  
      {  
          DataTable dt = GetAssets(dataitems["AccountNumber"].ToString());  
 
          string Filter = "Cusip = Cusip1";  
          dt.DefaultView.RowFilter = Filter;  
          dt.DefaultView.Sort = "Security";  
          CreateGrid(dt.DefaultView, ((PlaceHolder)e.Item.FindControl("PlaceHolder1")));  
      }  
}  
 

Example you gave me creates the grid on PageInit and it says that "Column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState." So how would I resolve that?

 

Thanks,

Julia


0
Sebastian
Telerik team
answered on 27 Apr 2009, 03:39 PM

Hello Julia,

The proper place to create the grids in rows of other grid dynamically is the ItemCreated hander and bind them to data inside the ItemDataBound handler as explained in this help topic.

To generate the columns for the inner grids programmatically (including the template column with the checkboxes), intercept their OnInit event and build the columns inside that handler.

Best regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Prakash
Top achievements
Rank 1
answered on 13 Jun 2009, 01:48 PM
Hi Julia,

    I am facing the same problem of what your facing. I have dynamically added a GridCheckBoxColumn in the Rad Grid and i am now unable to edit any rows , could you please help me if you have solved the problem 


With Regards,
Prakash K
 
0
Rajitha Reshmai
Top achievements
Rank 1
answered on 18 Jul 2009, 12:38 PM
Hi,


 I have created one checkbox in radgrid item template. I have inserted two records. when i edit one record, iam not getting checkbox
status in edit mode. please help me?
0
Rohan
Top achievements
Rank 1
answered on 21 Nov 2012, 02:29 PM
Hi all ,

I want to add check box to rad grid column to select the row using the GridTemplateColumn and Item template , i am using custom class to add item template -(because i want to generate rad grid dynamically).. i am able to fire the check box event and select row but not able to persistence the check state across the various operation or rad grid such as sorting, paging and...... can you please provide any sample of rad grid with grid template column with check box that is generated using template custom class ........
0
Pavlina
Telerik team
answered on 26 Nov 2012, 03:07 PM
Hi Rohan,

How to persists the selected Rows Server-side on Sorting/Paging/Filtering/Grouping is demonstrated in the article below:
http://www.telerik.com/help/aspnet-ajax/grid-persist-selected-rows-on-sorting.html

All the best,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Julia Shah
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Julia Shah
Top achievements
Rank 1
Sebastian
Telerik team
Prakash
Top achievements
Rank 1
Rajitha Reshmai
Top achievements
Rank 1
Rohan
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or