-1

我有一个包含两个按钮和一个包含 6 列的数据网格的屏幕。当按下按钮 1 时,它会在行中显示第 1 到第 4 列的数据。当我按下按钮 2 时,我希望我的应用程序填充行中的剩余列(即第 5 列和第 6 列)。

解决方案可能很明显,但我不确定如何解决它。

编辑:对不起,我之前忘了提,当我点击第二个按钮时,数据出现在正确的位置,即最后两列。除非它不在同一行。数据显示在下一行。

这是我的代码:

Private Sub btnClockIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClockIn.Click

Homescreen.DataGridViewEmployee.ColumnCount = 6
Homescreen.DataGridViewEmployee.Columns(0).Name = "Employee Number"
Homescreen.DataGridViewEmployee.Columns(1).Name = "Day"
Homescreen.DataGridViewEmployee.Columns(2).Name = "Week Number"
Homescreen.DataGridViewEmployee.Columns(3).Name = "Time In"
Homescreen.DataGridViewEmployee.Columns(4).Name = "Time Out"
Homescreen.DataGridViewEmployee.Columns(5).Name = "Hours"

Dim row As String() = New String() {ENumber, day, WeekNum, StartTime, "", ""}
Homescreen.DataGridViewEmployee.Rows.Add(row)
Homescreen.DataGridViewEmployee.ReadOnly = True

Private Sub btnClockOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClockOut.Click

Homescreen.DataGridViewEmployee.ColumnCount = 6
Homescreen.DataGridViewEmployee.Columns(0).Name = "Employee Number"
Homescreen.DataGridViewEmployee.Columns(1).Name = "Day"
Homescreen.DataGridViewEmployee.Columns(2).Name = "Week Number"
Homescreen.DataGridViewEmployee.Columns(3).Name = "Time In"
Homescreen.DataGridViewEmployee.Columns(4).Name = "Time Out"
Homescreen.DataGridViewEmployee.Columns(5).Name = "Hours"

Dim row As String() = New String() {"", "", "", "", EndTime, Diff}
Homescreen.DataGridViewEmployee.Rows.Add(row)
Homescreen.DataGridViewEmployee.ReadOnly = True
4

1 回答 1

0

问题是您使用的rows.add()方法的第二个按钮被命名为添加到一行。如果要添加到最后一行,则需要使用获取最后一行,rows(rows.Count -1)然后编辑列表中的相关字段

Dim row as DataGridViewRow = rows(rows.Count -1)
row(4) = EndTime
row(5) = Diff

无需在每次按下按钮时设置数据网格视图的属性,因此可以删除所有关于列名的代码,只读设置也可以。

于 2013-01-12T15:49:16.407 回答