在互联网上找不到任何东西,我想我错过了一些小事。
我在访问数据库中有一个列名列表,它应该与我在我的vb.net项目中创建的DataGridViewTextBoxColumn相匹配。我想使用访问数据库中的列名称将DataGridViewTextBoxColumn调用到我的dgv中。示例代码如下:
Dim column1 As New DataGridViewTextBoxColumn
Dim column2 As New DataGridViewTextBoxColumn
Dim myheader As New DataGridViewTextBoxColumn
With column1
.Name = "column1"
.HeaderText = "Column 1"
.SortMode = DataGridViewColumnSortMode.NotSortable
.Width = 200
End With
With column2
.Name = "column2"
.HeaderText = "Column 2"
.SortMode = DataGridViewColumnSortMode.NotSortable
.Width = 400
End With
a = 0
Do While a < 2
myheader = ds.Tables("columnheadersdatabase").Rows(a).Item("J001") '//rows(a) = column1 and rows(a + 1) = column2
dgv.Columns.Add(myheader)
a += 1
Loop
问题区域如下:
myheader = ds.Tables("columnheadersdatabase").Rows(a).Item("J001") '//rows(a) = column1 and rows(a + 1) = column2
dgv.Columns.Add(myheader)
我似乎可以将访问单元格值转换为DataGridViewTextBoxColumn(myheader)。我尝试使用dim myheader作为字符串,但这也不起作用。
有没有人有任何想法,任何帮助都会得到很多赞赏。
答案 0 :(得分:0)
DataGridViewTextBoxColumn
个对象可以存储在列表中。函数可以检查是否必须在DataGridView的列集合中添加该列表的项目。
请尝试以下代码:
'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Test data
'Dim ds As New DataSet
'Dim tb As New DataTable("columnheadersdatabase")
'tb.Columns.Add("J001")
'tb.Rows.Add(tb.NewRow)
'tb.Rows.Add(tb.NewRow)
'tb.Rows.Add(tb.NewRow)
'tb(0).Item("J001") = "column1"
'tb(1).Item("J001") = "column2"
'tb(2).Item("J001") = "column3"
'ds.Tables.Add(tb)
'A list that will contain all the DataGridViewTextBoxColumns
Dim TextBoxColumnList As New List(Of DataGridViewTextBoxColumn)
Dim column1 As New DataGridViewTextBoxColumn With
{
.Name = "column1",
.HeaderText = "Column 1",
.SortMode = DataGridViewColumnSortMode.NotSortable,
.Width = 200
}
Dim column2 As New DataGridViewTextBoxColumn With
{
.Name = "column2",
.HeaderText = "Column 2",
.SortMode = DataGridViewColumnSortMode.NotSortable,
.Width = 400
}
'The column1, column2 DataGridViewTextBoxColumns are being added in the list
TextBoxColumnList.Add(column1)
TextBoxColumnList.Add(column2)
Dim a As Integer = 0
Do While a < 2
'Reads the name of the column from the datatable
Dim ColumnName As String = ds.Tables("columnheadersdatabase").Rows(a).Item("J001").ToString
'Searches the index of the DataGridViewTextBoxColumn from the list that has the
'same name as the column name that was read from the datatable
Dim ColumnIndex As Integer = GetColumnIndex(TextBoxColumnList, ColumnName)
'Checks whether the index has been found
If ColumnIndex <> -1 Then
'Adds the DataGridViewTextBoxColumn in the DataGridView
dgv.Columns.Add(TextBoxColumnList(ColumnIndex))
End If
a += 1
Loop
'End Sub
Private Function GetColumnIndex(ByVal TextBoxColumnList As List(Of DataGridViewTextBoxColumn), ByVal ColumnName As String) As Integer
'Checks whether the list has an object reference
If TextBoxColumnList Is Nothing Then
Return -1
End If
Dim i As Integer
'Loops for all the items of the DataGridViewTextBoxColumn list
For i = 0 To TextBoxColumnList.Count - 1
'Compares the names
If TextBoxColumnList(i).Name.ToUpper = ColumnName.ToUpper Then
Return i
End If
Next
Return -1
End Function