我在Winforms应用程序中有一个DataGridView。 除了来自数据库表的4列以外,我还需要在datagridview [可能使用DataGridViewComboColumn?]显示一个具有组合框的附加列。 2.然后我想添加不同的项目组到每个行的每个组合框。将项目添加到DataGridView中的组合框
我该如何解决这个问题?
谢谢。
我在Winforms应用程序中有一个DataGridView。 除了来自数据库表的4列以外,我还需要在datagridview [可能使用DataGridViewComboColumn?]显示一个具有组合框的附加列。 2.然后我想添加不同的项目组到每个行的每个组合框。将项目添加到DataGridView中的组合框
我该如何解决这个问题?
谢谢。
您可以尝试通过添加它们DataBindingComplete电网
东西在这些线路上
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0] is DataGridViewComboBoxCell && row.Index == 1)
(row.Cells[0] as DataGridViewComboBoxCell).Items.Add("A");
else
(row.Cells[0] as DataGridViewComboBoxCell).Items.Add("B");
}
}
希望这有助于 编辑
(row.Cells[0] as DataGridViewComboBoxCell).Value = (row.Cells[0] as DataGridViewComboBoxCell).Items[0];
当该细胞是选择,则第一个值将显示为
我一直在寻找这个在VB.NET中的答案,但在这里找到了C#的答案。
在VB中,你可以这样做:
Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
For Each row As DataGridViewRow in DataGridView1.Rows
If TypeOf row.Cells(0) Is DataGridViewComboBoxCell AndAlso row.Index = 1 Then
TryCast(row.Cells(0), DataGridViewComboBoxCell).Items.Add("A")
Else
TryCast(row.Cells(0), DataGridViewComboBoxCell).Items.Add("B")
End If
Next
End Sub
进行编辑:
TryCast(row.Cells(0), DataGridViewComboBoxCell).Value = TryCast(row.Cells(0), DataGridViewComboBoxCell).Items(0)
感谢lot.This works.Could请你告诉我,我怎么显示的第一个项目是在下拉列表中选择在每个细胞? – Jimmy 2011-03-15 13:40:31
再次感谢。此工作也! – Jimmy 2011-03-15 18:32:39