private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, object[] itemsToAdd)
{
DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell) dataGrid.Rows[rowIndex].Cells[colIndex];
// You might pass a boolean to determine whether to clear or not.
dgvcbc.Items.Clear();
foreach (object itemToAdd in itemsToAdd)
{
dgvcbc.Items.Add(itemToAdd);
}
}
Private Sub FillGroups()
Try
'Create Connection and SQLCommand here.
Conn.Open()
Dim dr As SqlDataReader = cm.ExecuteReader
dgvGroups.Rows.Clear()
Dim PreviousGroup As String = ""
Dim l As New List(Of Groups)
While dr.Read
Dim g As New Groups
g.RegionID = CheckInt(dr("cg_id"))
g.RegionName = CheckString(dr("cg_name"))
g.GroupID = CheckInt(dr("vg_id"))
g.GroupName = CheckString(dr("vg_name"))
l.Add(g)
End While
dr.Close()
Conn.Close()
For Each a In (From r In l Select r.RegionName, r.RegionID).Distinct
Dim RegionID As Integer = a.RegionID 'Doing it this way avoids a warning
dgvGroups.Rows.Add(New Object() {a.RegionID, a.RegionName})
Dim c As DataGridViewComboBoxCell = CType(dgvGroups.Rows(dgvGroups.RowCount - 1).Cells(colGroup.Index), DataGridViewComboBoxCell)
c.DataSource = (From g In l Where g.RegionID = RegionID Select g.GroupID, g.GroupName).ToArray
c.DisplayMember = "GroupName"
c.ValueMember = "GroupID"
Next
Catch ex As Exception
End Try
End Sub
Private Class Groups
Private _RegionID As Integer
Public Property RegionID() As Integer
Get
Return _RegionID
End Get
Set(ByVal value As Integer)
_RegionID = value
End Set
End Property
Private _RegionName As String
Public Property RegionName() As String
Get
Return _RegionName
End Get
Set(ByVal value As String)
_RegionName = value
End Set
End Property
Private _GroupName As String
Public Property GroupName() As String
Get
Return _GroupName
End Get
Set(ByVal value As String)
_GroupName = value
End Set
End Property
Private _GroupID As Integer
Public Property GroupID() As Integer
Get
Return _GroupID
End Get
Set(ByVal value As Integer)
_GroupID = value
End Set
End Property
End Class
dgv1.datasource = datatable1;
dgv1.columns.add ( "cbxcol" , typeof(string) );
// different source for each comboboxcell in rows
var dict_rowInd_cbxDs = new Dictionary<int, object>();
dict_rowInd_cbxDs[1] = new list<string>(){"en" , "us"};
dict_rowInd_cbxDs[2] = new list<string>(){ "car", "bike"};
// !!!!!! setting comboboxcell after creating doesnt work here
foreach( row in dgv.Rows.asEnumerable() )
{
var cell = res_tn.dgv.CurrentCell as DataGridViewComboBoxCell;
cell.DataSource = dict_dgvRowI_cbxDs[res_tn.dgv.CurrentCell.RowIndex];
}
工作示例:
dgv1.datasource = datatable1;
dgv1.columns.add ( "cbxcol" , typeof(string) );
// different source for each comboboxcell in rows
var dict_rowInd_cbxDs = new Dictionary<int, object>();
dict_rowInd_cbxDs[1] = new list<string>(){"en" , "us"};
dict_rowInd_cbxDs[2] = new list<string>(){ "car", "bike"};
// cmboboxcell datasource Assingment Must be done after BindingComplete (not tested ) or cellbeginEdit (tested by me)
res_tn.dgv.CellBeginEdit += (s1, e1) => {
if (res_tn.dgv.CurrentCell is DataGridViewComboBoxCell) {
if (dict_dgvRowI_cbxDs.ContainsKey(res_tn.dgv.CurrentCell.RowIndex))
{
var cll = res_tn.dgv.CurrentCell as DataGridViewComboBoxCell;
cll.DataSource = dict_dgvRowI_cbxDs[res_tn.dgv.CurrentCell.RowIndex];
// required if it is list<mycustomClass>
// cll.DisplayMember = "ColName";
// cll.ValueMember = "This";
}
}
};
dataGridServices.DataBindingComplete += DataGridServices_DataBindingComplete;
dataGridServices.DataError += (sender,e) => { }; // required otherwise DataGridView will complain that "DataGridViewComboBoxCell value is not valid"
private void DataGridServices_DataBindingComplete(object? sender, DataGridViewBindingCompleteEventArgs e)
{
int nCol = dataGridServices.Columns[nameof(PackageService.CheckMethod)].Index;
int nRow = 0;
foreach (var packageService in AllServices)
SetCellComboBoxItems(nRow++, nCol, packageService.MethodsWithoutParameters);
}
public void SetCellComboBoxItems(int rowIndex, int colIndex, IEnumerable<string> items)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)Grid.Rows[rowIndex].Cells[colIndex];
cell.MaxDropDownItems = 100; // not sure if useful. Default is 8, max is 100
cell.Items.Clear();
cell.Items.AddRange(items.ToArray());
}
7条答案
按热度按时间q3aa05251#
可以。这可以使用DataGridViewComboBoxCell来完成。
下面是一个示例方法,用于将项添加到一个单元格,而不是整个列。
ctehm74n2#
avkwfej43#
以防有人找到这个线程,这是我在VB 2008中的解决方案。它提供的优点是允许您为组合框中的每个值分配一个ID。
7kjnsjlb4#
这是具有2个组合框列的gridView的示例,并且当组合框列1所选索引改变时,则用来自数据库的两个不同列的数据加载组合框列2。
gorkyyrv5#
在设置数据源后立即设置组合框单元格对我不起作用.它必须在绑定操作完成后进行.我选择了CellBeginEdit
空下拉列表示例:
工作示例:
dly7yett6#
2023.Net7我在这上面浪费了一个小时。正如上面的一些帖子所提到的,填充组合框项必须在数据绑定之后完成,这样对我来说才有效:
7hiiyaii7#