重新绑定有按钮列的datagridview
重新绑定有按钮列的datagridview,遇到问题:点击更新后,重新绑定datagridview,按钮列总是重复(一列正常的,一列哦空白的,而且正常显示的按钮列跑到最前面了,本意是在最后面的),而且有一列空白按钮列,重新绑定时我把生成按钮的代码去掉了也还是会有一列空白按钮列
代码,获取数据源,生成datagridview的,加载时调用一次,更新后调用一次:
Private Sub getData(ByVal sender As System.Object, ByVal e As System.EventArgs)
ComDB.DBConn()
ComDB.OpenConn()
Dim comm As New ComDB
Dim myDataSet As DataSet
Dim selSql As String = "select STAFF_ID,STAFF_NAME,(convert(varchar(10),WORK_START_TIME,111)+'~'+convert(varchar(10),WORK_END_TIME,111)) as '作業期間'," &
"WORK_CONTENT,CHARGE_FIELD from M_STAFF s " &
" full join M_PROJECT_STAFF ps on s.STAFF_ID_SEQ=ps.STAFF_ID_SEQ " &
" full join M_PROJECT p on p.PROJECT_ID=ps.PROJECT_ID where " &
" ps.PROJECT_ID='" + Me.project_id.Text + "' order by CHARGE_FIELD"
myDataSet = ComDB.GetDataSet(selSql, "id")
ComDB.CloseConn()
Me.dataGridView_staff.DataSource = myDataSet.Tables(0)
Me.dataGridView_staff.AllowUserToAddRows = False
Me.dataGridView_staff.Columns(2).FillWeight = 150
'ソートを禁止する
For i As Int32 = 0 To Me.dataGridView_staff.Columns.Count - 1
Me.dataGridView_staff.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
Next
Dim db As New DataGridViewButtonColumn()
db.Name = ""
dataGridView_staff.Columns.Add(db)
For i = 0 To dataGridView_staff.RowCount - 1
dataGridView_staff("", i).Value = "編集"
Next
For i = 0 To dataGridView_staff.RowCount - 1
If myDataSet.Tables(0).Rows(i).Item(4).ToString = "4" Then
Me.dataGridView_staff.Rows(i).Cells(4).Value = "担当"
End If
If myDataSet.Tables(0).Rows(i).Item(4).ToString = "3" Then
Me.dataGridView_staff.Rows(i).Cells(4).Value = "サブリーダ"
End If
If myDataSet.Tables(0).Rows(i).Item(4).ToString = "1" Then
Me.dataGridView_staff.Rows(i).Cells(4).Value = "リーダ"
End If
Next
End Sub
[解决办法]
DataGridView1.Columns.Clear()