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

Adding different columns programatically

18 Answers 455 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gonzalo
Top achievements
Rank 2
Gonzalo asked on 13 Feb 2012, 08:45 PM
Me.RadGridView1.Rows.Clear()
Me.RadGridView1.ColumnCount = 5
 
RadGridView1.Columns(0).HeaderText = "Id."
RadGridView1.Columns(1).HeaderText = "Name"
RadGridView1.Columns(2).HeaderText = "Under Appeal"
RadGridView1.Columns(3).HeaderText = "Terminated"
RadGridView1.Columns(4).HeaderText = "#"
 
RadGridView1.Columns(1).Width = 100
 
 
RadGridView1.Rows.Add ( 1, "John", True, True )
RadGridView1.Rows.Add ( 2, "Mary", True, False )
RadGridView1.Rows.Add ( 3, "Peter", False, True )


How do I get the radgridview to display checkboxes for boolean values for the last two columns? The columns get manually loaded.

Best regards

18 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 2
answered on 14 Feb 2012, 10:04 AM
Where in you code do you add the Columns?

Do you do this in the designer?

If it is then when you add a Column to the Gird there should be a drop down to select what column type you would like

if you are adding them in code you would need to use the correct Column Class 

http://www.telerik.com/help/winforms/gridview-columns-gridviewcheckboxcolumn.html 


0
Gonzalo
Top achievements
Rank 2
answered on 14 Feb 2012, 03:57 PM
Thanks David for your answer. In the code below where would you set the checkbox controls to handle the columns with boolean values ? programatically speaking. Best regards, G

Me.RadGridView1.Rows.Clear()
Me.RadGridView1.ColumnCount = 5
  
RadGridView1.Columns(0).HeaderText = "Id."
RadGridView1.Columns(1).HeaderText = "Name"
RadGridView1.Columns(2).HeaderText = "Under Appeal"
RadGridView1.Columns(3).HeaderText = "Terminated"
RadGridView1.Columns(4).HeaderText = "#"
  
RadGridView1.Columns(1).Width = 100
  
  
RadGridView1.Rows.Add ( 1, "John", True, True )
RadGridView1.Rows.Add ( 2, "Mary", True, False )
RadGridView1.Rows.Add ( 3, "Peter", False, True )
0
David
Top achievements
Rank 2
answered on 14 Feb 2012, 04:57 PM
Ah just had a look, The line in you code "Me.RadGridView1.ColumnCount = 5" This is what is adding the column rather then using this you can do the following,  I'm not sure if there is a way to convert a column once it has been created.
Me.RadGridView1.Rows.Clear()
  
Dim ID As New GridViewDecimalColumn()
ID.Name = "ID"
ID.HeaderText = "Id"
RadGridView1.MasterTemplate.Columns.Add(ID)
 
Dim Name As New GridViewTextBoxColumn()
Name.Name = "Name"
Name.HeaderText = "Name"
Name.Width = 100
RadGridView1.MasterTemplate.Columns.Add(Name)
 
Dim Under_Appeal as New GridViewCheckBoxColumn()
Under_Appeal.DataType = GetType(Boolean)
Under_Appeal.Name = "Under_Appeal"
Under_Appeal.HeaderText = "Under Appeal"
RadGridView1.MasterTemplate.Columns.Add(Under_Appeal)
 
Dim Terminated as New GridViewCheckBoxColumn()
Terminated.DataType = GetType(Boolean)
Terminated.Name = "Terminated"
Terminated.HeaderText = "Terminated"
RadGridView1.MasterTemplate.Columns.Add(Terminated)
   
Dim Hash As New GridViewDecimalColumn()
Hash.Name = "Hash"
Hash.HeaderText = "#"
RadGridView1.MasterTemplate.Columns.Add(Hash)
  
RadGridView1.Rows.Add ( 1, "John", True, True )
  
RadGridView1.Rows.Add ( 2, "Mary", True, False )
  
RadGridView1.Rows.Add ( 3, "Peter", False, True )
0
Gonzalo
Top achievements
Rank 2
answered on 14 Feb 2012, 06:44 PM
Thanks David ;-). Just what the doctor ordered :)
0
Stefan
Telerik team
answered on 16 Feb 2012, 03:54 PM
Hi Gonzalo,

Thank you for writing.

As David mentioned, the code that you have provided does not show how you are adding the columns to your grid. If your column is added as GridViewCheckBoxColumn, you cannot change it afterwards. You need to either add the correct column initially or remove the undesired columns and add GridViewCheckBoxColumn instead.

I hope this helps. 

Greetings,
Stefan
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Gonzalo
Top achievements
Rank 2
answered on 16 Feb 2012, 04:53 PM
Stefan,

Thanks for your kind reply. As I stated in my previous message David's message help me clarify the issue and I am happy moving to a different topic ... RadCharts :) . Your message is appreciated and acknowledged. Thanks for all your support. I am not usually developing win forms apps but I had this request so I never say "no" :-).

Best regards,

G

PS. By the way, I am already exploring Q1 2012 :-) Cheers.
0
Stefan
Telerik team
answered on 21 Feb 2012, 12:06 PM
Hi Gonzalo,

Thank you for the nice words. I am glad that everything is OK.

Enjoy the new bits of Q1 2012 and let us know if you have any questions there.
 
Regards,
Stefan
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Gonzalo
Top achievements
Rank 2
answered on 23 Feb 2012, 11:14 PM
Continuing with this great topic based on the previous example how would you add child row programmatically. Assuming...

RadGridView1.Rows.Add ( 1, "John", True, True )
RadGridView1.Rows.Add ( 2, "Mary", True, False )
RadGridView1.Rows.Add ( 3, "Peter", False, True )
 
something like ...
 
RadGridView1.ChildRows.Add ( 1, "sugar", 2.00 )
RadGridView1.ChildRows.Add ( 1, "cake", 12.00 )
RadGridView1.ChildRows.Add ( 1, "butter", 3.00)
RadGridView1.ChildRows.Add ( 2, "cake", 12.00)
Thank you so much.

G
0
Stefan
Telerik team
answered on 24 Feb 2012, 04:14 PM
Hi Gonzalo,

Thank you for your question.

In general, I would suggest to separate the different subjects into different threads, so they will be easier to find when looking for them.

In regards to your inquiry, first I would suggest that you get acquainted with our documentation concerning the different methods for Populating RadGridView with data and Binding to Hierarchical RadGridView. Both of these help sections will help you understand how RadGridView works, thus will help you choose the best approach for populating with data for your application.

Attached you can find a sample project demonstrating how to create a child template for your RadGridView, relate it with the MasterTemplate and add some row in it. I hope that you find this information helpful.
 
Regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Gonzalo
Top achievements
Rank 2
answered on 24 Feb 2012, 05:11 PM
Thanks Stefan and my apologies for not having created a different thread, I will follow your advise from now on ;-) I will look into your response and get back to you as needed. Thanks again for all your patience. G
0
Gonzalo
Top achievements
Rank 2
answered on 24 Feb 2012, 05:27 PM
Thanks Stefan for the example you rocked ;-).

G
0
Stefan
Telerik team
answered on 29 Feb 2012, 12:16 PM
I am glad I could help. Do not hesitate to open a new thread when you have any other questions.

Regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 29 Apr 2021, 07:33 AM | edited on 29 Apr 2021, 07:40 AM
hiii Telerik Team....how to add new Row In RadGridView Template Grid  CellEndEdit event .... i have used this code in CellEndEdit event
dgvSalaryReport.MasterTemplate.Templates[0].Rows.AddNew();
gvSalaryReport.MasterTemplate.Refresh();
dgvSalaryReport.MasterTemplate.Templates[0].Refresh();

but its not working for me.....the row count is increasing but rows not showing in  the design .....please check the attached Picture 
0
Nadya
Telerik team
answered on 29 Apr 2021, 10:25 AM

Hello, Ras Ran,

According to the provided information, I suppose that you have a hierarchy grid and want to add a new row to the first template (Templates[0]) collection. However, it is not clear enough whether you are using RadGridView in bound or unbound mode and how exactly the hierarchy is set up. Could you please elaborate?

Without knowing the exact setup, I can give you only general suggestions on how to add a new row to the child level. 

If you set the DataSource property (bound mode) for each GridViewTemplate (master level and child level), in order to add a new record for the child template, it is necessary to add a new record to the DataSource collection that is being applied to the child GridViewTemplate. Since there is a relation (GridViewRelation) between the two GridViewTemplates indicating the child ID and the parent ID, it is important to specify the proper parent ID for the new record. Thus, the hierarchy row will be added to the respective parent row. If you have a bound grid and you try to add new row directly to the Rows collection you wouldn't be able to see the new row. A sample approach how the hierarchy is setup in this case is available in the "Setting the hierarchical grid in bound mode" section in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/hierarchical-grid/binding-to-hierarchical-data-programmatically

If you use unbound mode and you add the rows directly to the Rows collection of the respective GridViewTemplate, it is just necessary to add a new row which cell's value for the parent ID points to the desired parent row. A sample approach for this approach is demonstrated in the "Hierarchical Grid in Unbound mode" section in the above-referred article.

I hope this information is useful. Should you have further questions please let me know.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 29 Apr 2021, 11:46 AM | edited on 29 Apr 2021, 11:48 AM

hiii  Nadya, please check the attached image.... i just enabled the property AllowAddNewRow ..... i just want to do this ( Add New Row )  as programmatically for Template Grid,

is there any solution for this ?

thanks and regards ,

RANEES

This My Code :
GridViewTemplate template = new GridViewTemplate();
dgvSalaryReport.DataSource = null;
dgvSalaryReport.DataSource = ds.Tables[0]; //DataTable
template.DataSource = null;
template.DataSource = dtForPaySlip;  //DataTable
template.EnableSorting = false;
template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
template.ShowRowHeaderColumn = false;
template.AllowAddNewRow = true;
template.EnableAlternatingRowColor = true;
template.HorizontalScrollState = ScrollState.AlwaysShow;
template.VerticalScrollState = ScrollState.AlwaysShow;
dgvSalaryReport.MasterTemplate.Templates.Add(template);
GridViewRelation relation = new GridViewRelation(dgvSalaryReport.MasterTemplate);
relation.ChildTemplate = template;

0
Nadya
Telerik team
answered on 29 Apr 2021, 01:58 PM

Hello, RANEES,

I can not see the attached image. However, according to the provided code snippet, it seems that you have a bound RadGridView and you use DataTable for the child template. In order to add a new row programmatically to the child template (similarly as AllowAddNewRow works), it is necessary to add a new record to the DataSource collection that is being applied to the child GridViewTemplate, in your case add a new row to the DataTable. It is important to specify the proper parent ID for the new record. Thus, the hierarchy row will be added to the respective parent row. 

I hope this helps. Please let me know if you need further assistance.

Regards,
Nadya
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 30 Apr 2021, 05:27 AM

hiii,Nadya its worked Perfectly ,u made my day...;)
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 30 Apr 2021, 11:23 AM

hii Nadya.....How do get Row Count of current child template  in CellEndEdit event

My Code : 
 private void dgvSalaryReport_CellEndEdit(object sender, GridViewCellEventArgs e)
 {
 int rowIndex = Convert.ToInt32(e.RowIndex);
 if (rowIndex > -1 && (e.Row.ViewInfo.Rows.Count - 1 == rowIndex))
 {
        e.Row.ViewTemplate.Rows.Add(3, empid, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 }
 }

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 May 2021, 10:08 AM

Hi, Ras Ran,

In the CellEndEdit event you have access to the affected data Row. Its HirerachicalLevel property indicates whether you are editing a child or a parent row. The Row.Parent property return the parent row from where you can get how many child rows a certain parent has. Please refer to the following code snippet: 

        public RadForm1()
        {
            InitializeComponent();

            this.radGridView1.CellEndEdit += radGridView1_CellEndEdit;
        }

        private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            if (e.Row.HierarchyLevel==1)
            {
                GridViewHierarchyRowInfo parentRow = e.Row.Parent as GridViewHierarchyRowInfo;
                int childRowsCount = parentRow.ActiveView.Rows.Count;
                
            } 
        }

        private void RadForm1_Load(object sender, EventArgs e)
        { 
            this.suppliersTableAdapter.Fill(this.nwindDataSet.Suppliers); 
            this.productsTableAdapter.Fill(this.nwindDataSet.Products);

            radGridView1.DataSource = nwindDataSet.Suppliers;

            GridViewTemplate template = new GridViewTemplate();
            template.DataSource = nwindDataSet.Products;
            radGridView1.MasterTemplate.Templates.Add(template);

            GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
            relation.ChildTemplate = template;
            relation.RelationName = "SuppliersProducts";
            relation.ParentColumnNames.Add("SupplierID");
            relation.ChildColumnNames.Add("SupplierID");
            radGridView1.Relations.Add(relation);
        }

Feel free to modify and extend it in a way which suits your requirements best.

Should you have further questions please let me know.

Regards,


Dess | Tech Support Engineer, Sr.
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Tags
GridView
Asked by
Gonzalo
Top achievements
Rank 2
Answers by
David
Top achievements
Rank 2
Gonzalo
Top achievements
Rank 2
Stefan
Telerik team
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
Nadya
Telerik team
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or