DataGridView1.ColumnCount = 5
sql_str = "select 新製令編號 'Column1',作業名稱 'Column2',sum(報工量) 'Column4',sum(使用時間)/60 'Column5' from workorders where 日期 = '" & DateTimePicker1.Value.Date & "'" & _
" group by 作業名稱 order by 新製令編號"
Dim data As DataTable
data = Nothing
DataGridView1.DataSource = data
data = New DataTable
da = New MySqlDataAdapter(sql_str, MDIParent1.conn)
cb = New MySqlCommandBuilder(da)
da.Fill(data)
DataGridView1.DataSource = data
DataGridView1.Columns(0).Name = "Column3"
DataGridView1.Columns(1).Name = "Column8"
DataGridView1.Columns(2).Name = "Column9"
DataGridView1.Columns(3).Name = "Column10"
DataGridView1.Columns(4).Name = "Column11"
DataGridView1.Columns("Column3").HeaderText = " 品名"
.....
.......
.......
DataGridView1.Columns("Column9").HeaderText = " 標準工時"
DataGridView1.Columns("Column10").HeaderText = " 效率%"
DataGridView1.Columns("Column11").HeaderText = " 低於80%之異常處理"
當取消'DataGridView1.ColumnCount = 5 時出現此錯誤:
索引超出範圍。必須為非負數且小於集合的大小。
參數名稱: index
DataGridView1.Columns(0).Name = "Column3"
DataGridView1.Columns(1).Name = "Column8"
DataGridView1.Columns(2).Name = "Column9"
DataGridView1.Columns(3).Name = "Column10"
DataGridView1.Columns(4).Name = "Column11"
DataGridView1.Columns("Column3").HeaderText = " 品名"
結果應該是這樣就OK了
Dim data As DataTable
data = Nothing
DataGridView1.DataSource = data
data = New DataTable '
DataGridView1.ColumnCount = 5 注意此程式位置
sql_str = "select 新製令編號 'Column1',作業名稱 'Column2',sum(報工量) 'Column4',sum(使用時間)/60 'Column5' from workorders where 日期 = '" & DateTimePicker1.Value.Date & "'" & _
" group by 作業名稱 order by 新製令編號"