单击按钮完成行的第二部分?

时间:2013-01-10 21:25:49

标签: vb.net button datagrid datagridview

我有一个包含两个按钮和一个包含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

1 个答案:

答案 0 :(得分:0)

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

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

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