winfom DataGridView下拉列表联动
设备类型有:设备1,设备2,设备3设备名称有:设备1-机组,设备1-主板;设备2-子板;设备3-模块,设备3-监控模块
右击菜单选择编辑,dataGridview为可用,再选择第一列设备类型,得到值查询sql语句再赋值给第二列设备名称
这个应该怎么组呢?
设备类型选择 设备1时,设备名称就变成 设备1-机组,设备1-主板
设备类型选择 设备2时,设备名称就变成 设备2-子板
设备类型选择 设备3时,设备名称就变成 设备3-模块,设备3-监控模块
这个应该怎么做呢? DataGridView下拉列表联动 datagridview --------------------编程问答-------------------- 我这里有两种实现的思路,
1.定义两个combobox控件,之后把这两个控件添加到dgv上,在dgv的编辑咧事件判断当前编辑咧是类型列就显示你定义的类型的cbo控件,这是简单的方法
--------------------编程问答--------------------
--------------------编程问答-------------------- 2.首先定义一个combobox,之后在dgv的编辑单元格控件的事件中个定义的cbo赋值,并注册事件,剩下的事情就可以在注册事件中去写了
DataGridViewComboBoxColumn col;//先获取到下拉列
col.Items.IndexOf("设备")//获得指定内容的索引,根据此索引去设置其他列
给你一段代码
--------------------编程问答-------------------- 第二种方法容易报错
private Combobox cbo = null;
void dgvEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if cbo == null && e.Control as Combobox != null && dgv.CurrentCell.OwningColumn.Name == "你的类型列的名字"
cbo = e.Control as Combobox;
//cbo的下拉选择事件
cbo.SelectedIndexChanged += new EventHandler(cboEdit_SelectedIndexChanged);
}
void cbo_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if cbo != null
cbo.SelectedIndexChanged -= new EventHandler(cboEdit_SelectedIndexChanged);
cbo = null;
}
--------------------编程问答--------------------
//第一种方法的代码
private ComboBox cboCustomsRateP = new ComboBox();
cboCustomsRateR.Visible = false;
cboCustomsRateP.Visible = false;
dgvCost_FeeInfo_Income.Controls.Add(cboCustomsRateR);
dgvCost_FeeInfo_Pay.Controls.Add(cboCustomsRateP);
cboCustomsRateR.SelectedIndexChanged += new EventHandler(cboCustomsRateR_SelectedIndexChanged);
cboCustomsRateP.SelectedIndexChanged += new EventHandler(cboCustomsRateP_SelectedIndexChanged);
cboCustomsRateR.DropDownStyle = ComboBoxStyle.DropDownList;
cboCustomsRateP.DropDownStyle = ComboBoxStyle.DropDownList;
private void dgvCost_FeeInfo_Pay_CurrentCellChanged(object sender, EventArgs e)
{
try
{
if (dgvCost_FeeInfo_Pay.CurrentCell.OwningColumn.Name == "dgvcmbCustomsRate_Pay")
{
Rectangle rect = dgvCost_FeeInfo_Pay.GetCellDisplayRectangle(dgvCost_FeeInfo_Pay.CurrentCell.ColumnIndex, dgvCost_FeeInfo_Pay.CurrentCell.RowIndex, false);
string value = Convert.ToString(dgvCost_FeeInfo_Pay.Rows[dgvCost_FeeInfo_Pay.CurrentCell.RowIndex].Cells[dgvCost_FeeInfo_Pay.CurrentCell.ColumnIndex].Value);
if (!cboCustomsRateP.Items.Contains(value) && dgvCost_FeeInfo_Pay.Rows[dgvCost_FeeInfo_Pay.CurrentCell.RowIndex].ReadOnly)
{
cboCustomsRateP.Items.Add(value);
cboCustomsRateP.Text = value;
}
else
{
cboCustomsRateP.Text = value;
}
cboCustomsRateP.Location = new Point(rect.X, rect.Y);
cboCustomsRateP.Height = rect.Height;
cboCustomsRateP.Width = rect.Width;
cboCustomsRateP.Visible = true;
if (cboCustomsRateR.Visible) cboCustomsRateR.Visible = false;
cboCustomsRateP.Enabled = !dgvCost_FeeInfo_Pay.Rows[dgvCost_FeeInfo_Pay.CurrentCell.RowIndex].ReadOnly;
}
else
{
cboCustomsRateP.Visible = false;
cboCustomsRateR.Visible = false;
}
}
catch (Exception)
{
}
}
/// <summary>
/// 付款dgv滚动条事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvCost_FeeInfo_Pay_Scroll(object sender, ScrollEventArgs e)
{
cboCustomsRateP.Visible = false;
}
/// <summary>
/// 付款dgv改变列宽事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvCost_FeeInfo_Pay_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
cboCustomsRateP.Visible = false;
}
试了下,不行。还有cbo_CellEndEdit事件在哪里注册 --------------------编程问答-------------------- 在 dgv 的 事件 里 找 一下 --------------------编程问答--------------------
还是不行
我的代码为
private ComboBox cobFacilityType = null;
private void dgvLoad_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (cobFacilityType != null)
cobFacilityType.SelectedIndexChanged -= new EventHandler(cboEdit_SelectedIndexChanged);
cobFacilityType = null;
}
void dgvEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (cobFacilityType == null && e.Control as ComboBox != null && dgvLoad.CurrentCell.OwningColumn.Name == "设备种类")
cobFacilityType = e.Control as ComboBox; //cbo的下拉选择事件
cobFacilityType.SelectedIndexChanged += new EventHandler(cboEdit_SelectedIndexChanged);
}
void cboEdit_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dsEquipmentName = dataBase.RunProcReturn("select EquipmentPowerID,EquipmentName from EquipmentPower where EquipmentType='BTS设备功率'");//获取设备名称信息信息
FacilityName.DisplayMember = "EquipmentName";//设置格式化的值的数据源,即在用户看到的下拉文本来自数据库表的列,这里数据库表名为QuestionType
FacilityName.ValueMember = "EquipmentName";//设置下拉文本对应的真实值的数据源
FacilityName.DataSource = dsEquipmentName.Tables[0]; //QuestionType为datagridview中问题类别列的列名。设置数据源
} --------------------编程问答-------------------- 那里 不行 提示 描述
补充:.NET技术 , C#