我有一个 DataTable,我正在使用它从数据库中查询数据,然后我遍历每一行,添加一行并将值应用于每行以获取已经存在的值。当这条线运行时
DirectCast(DataGridView1.Rows(i).Cells("Key Field 1 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield1"))
DirectCast(DataGridView1.Rows(i).Cells("Key Field 2 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield2"))
DirectCast(DataGridView1.Rows(i).Cells("Key Field 3 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield3"))
它正确设置了字段的 .value ,但它没有在下拉列表中设置值,因此看不到该值。我有一个 data_error 处理程序,它没有被触发,所以它不是由于不正确的数据类型,如果我从下拉列表中选择一个值,即使它应该加载的值,它也会改变 .value 并且它正确保存到数据库,但我似乎找不到将选定项设置为所需值的方法。
完整的代码块在这里:
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
If CStr(row("RATETYPE")).ToUpper() = "HEADER" + RateType Then
handleHeaderRow(row)
ElseIf CStr(row("RATETYPE")).ToUpper() = RateType Then
DataGridView1.Rows.Add()
DataGridView1.Rows(i).Cells("cuid").Value = CStr(row("cuid"))
DataGridView1.Rows(i).Cells("Key Field 1 text").Value = CStr(row("keyfield1"))
DataGridView1.Rows(i).Cells("Key Field 2 text").Value = CStr(row("keyfield2"))
DataGridView1.Rows(i).Cells("Key Field 3 text").Value = CStr(row("keyfield3"))
DirectCast(DataGridView1.Rows(i).Cells("Key Field 1 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield1"))
DirectCast(DataGridView1.Rows(i).Cells("Key Field 2 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield2"))
DirectCast(DataGridView1.Rows(i).Cells("Key Field 3 cmb"), DataGridViewComboBoxCell).Value = CStr(row("keyfield3"))
DataGridView1.Rows(i).Cells("Rate 1").Value = CDbl(row("rate1")).ToString("F2")
DataGridView1.Rows(i).Cells("Rate 2").Value = CDbl(row("rate2")).ToString("F2")
DataGridView1.Rows(i).Cells("Rate 3").Value = CDbl(row("rate3")).ToString("F2")
DataGridView1.Rows(i).Cells("Rate 4").Value = CDbl(row("rate4")).ToString("F2")
DataGridView1.Rows(i).Cells("Rate 5").Value = CDbl(row("rate5")).ToString("F2")
i += 1
End If
Next row
End If