DataGridView列在由Typed DataSet填充时是如何命名的?

时间:2014-12-11 22:52:59

标签: .net vb.net datagridview

当通过类型化的DataSet填充DataGridView时,它如何/它自然地命名列?

例如:

Dim sqlstrg as String = "SELECT [Welding_CWB].[CWB_Process] AS 'Process', [Welding_CWB].[CWB_Classification] AS 'Class', [Welding_CWB].[CWB_Position] AS 'Position', [Welding_CWB].[CWB_Standard] AS 'Standard', [Welding_CWB].[CWB_Thickness_Range] AS 'Thickness', [Welding_CWB].[CWB_Expiry] AS 'Expiry' FROM [Welding_CWB];"
Dim cs as String = My.Settings.ConnectionString
Dim con as new SqlConnection(cs)
Dim ds as new DataSet
Dim da as new DqlDataAdapter(sqlstrg, con)

con.open()
da.fill(ds, "TableName")
con.close()

Datagridview1.Datasource = ds.Tables(0)

我指的是:

For I As Integer To Me.datagridview1.rows.count - 1

Me.Datagridview1.Rows(I).cells(Name of column).value 
                                     ^
Next                                 |
                                     |

列的名称是什么?有没有办法可以指定该名称是什么?

1 个答案:

答案 0 :(得分:0)

除非ColumnName属性另有定义,否则列的名称将与源DataTable相同。

' Wrap the source DataTable in a DataView.
Datagridview1.Datasource = New DataView(ds.Tables(0))

For I As Integer To Me.datagridview1.rows.count - 1
    Me.Datagridview1.Rows(I).cells("NameFromSourceSQL").value
Next

在您的情况下,有效名称为:

  • 过程
  • 位置
  • 标准
  • 厚度
  • 到期

您可以通过执行以下操作来迭代列名称:

For Each col As DataGridViewColumn In DataGridView1.Columns
    MessageBox.Show("Column Name = " & col.ColumnName & " / Header = " & col.HeaderText)
Next