Unbound datagridview [vb 2005]

Momo

Member
Joined
May 30, 2006
Messages
21
Programming Experience
Beginner
I have a code as below to customize a unbound datagrid, but i have stuck with how to assign each column type in datagridview (either link, label, image, text...)
VB.NET:
Module AssignDgvColumnName
Public Sub InitialDgv(ByVal ColumnName1() As String, ByVal ColumnHeader() As String, ByVal Newdgv As DataGridView)
Dim TotalColumnName As Integer = ColumnName1.Length
Dim TotalColumnHeader As Integer = ColumnHeader.Length
Dim CompanyPro1 As New CompanyPro
Dim CombineBoth() As Array = {ColumnName1, ColumnHeader}
If TotalColumnName <> 0 AndAlso TotalColumnHeader <> 0 AndAlso TotalColumnName = TotalColumnHeader Then
Dim i, j As Integer
For i = 0 To TotalColumnName - 1 Step +1
For j = 0 To TotalColumnHeader - 1 Step +1
If i = j Then
With Newdgv
.Columns.Add(ColumnName1(i).ToString, ColumnHeader(i).ToString) 
End With
End If
Next
Next
End If
End Sub
Due to each column might have different column type, so how I manually define each column type at here? Thanks in advanced.
 
It is better to set columns for DataGridView (DGV) in Designer than in code, unless you have to for dynamics. When doing it in code you create instances of the specialized DataGridViewColumn classes and use these with the overloaded DGV.Columns.Add method. As you also can see in Designers dialog the six derived DataGridViewColumn classes are:
- DataGridViewLinkColumn
- DataGridViewImageColumn
- DataGridViewComboBoxColumn
- DataGridViewTextBoxColumn
- DataGridViewCheckBoxColumn
- DataGridViewButtonColumn
Example, Newdgv is the instance of DataGridView:
VB.NET:
Dim dgvbc As New DataGridViewButtonColumn
dgvbc.Name = "dgvbc1"
dgvbc.HeaderText = "Button column header"
[SIZE=2]dgvbc.Width = 100
[/SIZE]Newdgv.Columns.Add(dgvbc)
Also, you cannot change an existing column type into something else, you have to remove the column and add another one.
 
Thanks JohnH! I thought unbound datagridview can tend to be flexible as compare to bound datagridview. Sorry, by here I'm not too sure with what u are trying to say in the quote below:
JohnH said:

Also, you cannot change an existing column type into something else, you have to remove the column and add another one.
Thanks.
 
after I test on the method as refer above, it work fine in a blank form but once I put it in my loop as
VB.NET:
Public Sub InitialDgv(ByVal ColumnName1() As String, ByVal ColumnHeader() As String, ByVal Newdgv As DataGridView)
Dim TotalColumnName As Integer = ColumnName1.Length
Dim TotalColumnHeader As Integer = ColumnHeader.Length
Dim CompanyPro1 As New CompanyPro
Dim CombineBoth() As Array = {ColumnName1, ColumnHeader}
Dim myLinkColumn As New datagridviewtextcolumn
If TotalColumnName <> 0 AndAlso TotalColumnHeader <> 0 AndAlso TotalColumnName = TotalColumnHeader Then
Dim i, j As Integer
For i = 0 To TotalColumnName - 1 Step +1
For j = 0 To TotalColumnHeader - 1 Step +1
If i = j Then
With myLinkColumn
.HeaderText = ColumnHeader(i).ToString
.Name = ColumnName1(i).ToString 
End With
 Newdgv.Columns.Add(myLinkColumn)
End If
Next
Next
End If
End Sub
It return me an error of
"Provided column already belongs to the DataGridView control."
Can anybody know what this problem is? Thanks.
 
It's because you only created one New datagridviewtextcolumn (myLinkColumn), changed it's properties and tried to add it to Columns collection again. When you ealier used the Columns.Add(text,name) method a new column was created for you for each call. Now with the overloaded Columns.Add(column) you actually have to create new columns yourself. An instance of column can only be added to the Columns collection once.
 
Back
Top