Click here to Skip to main content
15,887,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have datagridview with 5 columns which i need on several forms with same functionality.
i want to create class control for datagriedview .

how to add five columns by default for datagridview when added to form. Width and Height working fine

i get error on below line
Me.Columns.Add(5)

Error BC30311 Value of type 'Integer' cannot be converted to 'DataGridViewColumn'.


What I have tried:

Public Class ClsDgvJournal
    Inherits DataGridView


   Public Sub New()
        Width = 50
        Height = 200
        Me.Columns.Add(5)
    End Sub

End Class
Posted
Updated 22-Nov-18 23:35pm

The Columns.Add method allows you to add an actual column to the DGV - not to specify the number of columns you want. If you think about it, that makes sense, because a column is more than just a black space - it's a descriptor of what the column contains and must at the very least have a string for the title, and a string for the access name: DataGridViewColumnCollection Class (System.Windows.Forms) | Microsoft Docs[^]
The Columns collection is the schema for the table, which controls how the data in Rows is displayed.

The easiest way to keep them all the same is to use the same data: set the DataSource property of the DGV and it will extract the schema from that, and you need add no more columns.
 
Share this answer
 
You can add rows by just specifying the number of rows you want to add, but columns you have to define first before adding a range of them.
Here is the reference documentation DataGridViewColumnCollection.AddRange(DataGridViewColumn[]) Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
pravin9455 23-Nov-18 5:47am    
i tried with solution 2 below code and it works fine when i put this datagrid on form.

But when i run the form columns are shown two times i.e 10 columns

AAA|BBB|CCC|DDD|EEE|AAA|BBB|CCC|DDD|EEE


Hide Copy Code
Public Class ClsDgvJournal
Inherits DataGridView
Public Sub New()
Width = 500
Height = 200
Dim Col1 = New DataGridViewTextBoxColumn With {.HeaderText = "AAA", .Name = "A"}
Dim Col2 = New DataGridViewTextBoxColumn With {.HeaderText = "BBB", .Name = "B"}
Dim Col3 = New DataGridViewTextBoxColumn With {.HeaderText = "CCC", .Name = "C"}
Dim Col4 = New DataGridViewTextBoxColumn With {.HeaderText = "DDD", .Name = "D"}
Dim Col5 = New DataGridViewTextBoxColumn With {.HeaderText = "EEE", .Name = "E"}
Me.Columns.AddRange({Col1, Col2, Col3, Col4, Col5})

End Sub

End Class
CHill60 23-Nov-18 7:34am    
You must have already had those columns in the DataGridView... if you are trying to set up all the columns you want like this then don't forget to use Me.Columns.Clear first - see DataGridViewColumnCollection.Clear Method (System.Windows.Forms) | Microsoft Docs[^]
But also look at Solution 1 - DataSource is probably the better way forward ... you can add columns as required to a datatable Adding Columns to a DataTable | Microsoft Docs[^] and just set the DGVs to have that datatable as the DataSource
pravin9455 23-Nov-18 12:23pm    
i just added the datagridview control on form and run to test .D'int do anything .

when i add control on form it shows only five columns

when i run it shows ten columns
CHill60 26-Nov-18 3:00am    
"when i add control on form it shows only five column" - so the control has 5 columns before your program runs.
"when i run it shows ten columns" - because you are not clearing the column collection before adding the 5 columns again.
pravin9455 23-Nov-18 12:40pm    
i am using this datagrid to add new data row to sql table and not for data display.

since i want this datagrid on several forms trying to avoid creating columns every time i add datagrid on new form by creating class control with default columns

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900