怎么把数据绑定到DataGridView中的ComboBox上并显示出来
this.dgvEquInfor.ReadOnly=false;  ...
this.dgvEquInfor.ReadOnly = false; wms.BLL.equCate equCateBLL = new wms.BLL.equCate(); DataSet ds = equCateBLL.GetList(""); DataTable dt = ds.Tables[0]; ComEquCate.DataSource = dt; this.ComEquCate.DisplayMember = "equCateName"; this.ComEquCate.ValueMember = "equCateNO";ComEquCate是DataGridView中的那个列名绑定上去运行时显示不出来,DataGridView中还有什么需要设置的吗一定要把DataGridView设置成可编辑,否则下拉框拉不下来!~~这句话值的是哪个属性啊(ReadOnly ?) comboBox1.DataSource = dt; this.comboBox1.DisplayMember = "equCateName";单用一个ComboBox就可以显示出来 这个问题应该怎么解决~~希望大侠指点一二~~~
展开
1个回答
展开全部
/// <summary>
/// 初始化下拉列表框
/// </summary>
private void InitDGVComboBox(DataGridView dgv, ComboBox cbx)
{
m_CbxHelper.BindWeldProcesses(cbx); //绑定数据源到ComboBox
dgv.Controls.Add(cbx); //将ComboBox 添加到dgv
}
//激活某单元格
private void dgvDetail21_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
lastCbxRowIndex = e.RowIndex;
lastCbxColIndex = e.ColumnIndex;
DisplayComboxOnCell(dgvDetail21, cbxWeldingProcess, e.RowIndex, e.ColumnIndex);
}
}
//离开某单元格
private void dgvDetail21_CellLeave(object sender, DataGridViewCellEventArgs e)
{
UpdateComboBoxCellValue(dgvDetail21, cbxWeldingProcess, e.RowIndex, e.ColumnIndex);
}
/// <summary>
/// 在制定的单元格显示下拉列表
/// </summary>
private void DisplayComboxOnCell(DataGridView dgv, ComboBox cbx, int rowIndex, int colIndex)
{
DataGridViewCell cell = dgv.Rows[rowIndex].Cells[colIndex];
Rectangle rect = dgv.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);
cbx.Location = rect.Location;
cbx.Size = rect.Size;
//根据单元格值,设置ComboBox当前选定值
m_CbxHelper.MatchComboBoxValue(cbx, cell.Value.ToString());
cbx.Visible = true;
}
/// <summary>
/// 更新下拉列表所在单元格值
/// </summary>
private void UpdateComboBoxCellValue(DataGridView dgv, ComboBox cbx, int rowIndex, int colIndex)
{
if (colIndex == 2)
{
DataGridViewCell cell = dgv.Rows[rowIndex].Cells[colIndex];
if (cell.Value != null && cell.Value.ToString() != cbx.Text)
{
cell.Value = cbx.Text;
}
cbx.Visible = false;
}
}
/// 初始化下拉列表框
/// </summary>
private void InitDGVComboBox(DataGridView dgv, ComboBox cbx)
{
m_CbxHelper.BindWeldProcesses(cbx); //绑定数据源到ComboBox
dgv.Controls.Add(cbx); //将ComboBox 添加到dgv
}
//激活某单元格
private void dgvDetail21_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
lastCbxRowIndex = e.RowIndex;
lastCbxColIndex = e.ColumnIndex;
DisplayComboxOnCell(dgvDetail21, cbxWeldingProcess, e.RowIndex, e.ColumnIndex);
}
}
//离开某单元格
private void dgvDetail21_CellLeave(object sender, DataGridViewCellEventArgs e)
{
UpdateComboBoxCellValue(dgvDetail21, cbxWeldingProcess, e.RowIndex, e.ColumnIndex);
}
/// <summary>
/// 在制定的单元格显示下拉列表
/// </summary>
private void DisplayComboxOnCell(DataGridView dgv, ComboBox cbx, int rowIndex, int colIndex)
{
DataGridViewCell cell = dgv.Rows[rowIndex].Cells[colIndex];
Rectangle rect = dgv.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);
cbx.Location = rect.Location;
cbx.Size = rect.Size;
//根据单元格值,设置ComboBox当前选定值
m_CbxHelper.MatchComboBoxValue(cbx, cell.Value.ToString());
cbx.Visible = true;
}
/// <summary>
/// 更新下拉列表所在单元格值
/// </summary>
private void UpdateComboBoxCellValue(DataGridView dgv, ComboBox cbx, int rowIndex, int colIndex)
{
if (colIndex == 2)
{
DataGridViewCell cell = dgv.Rows[rowIndex].Cells[colIndex];
if (cell.Value != null && cell.Value.ToString() != cbx.Text)
{
cell.Value = cbx.Text;
}
cbx.Visible = false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询