[WinForm]DataGridview 和 ComboBox 有关
ID Name 。。。。。
第一列是DataGridViewComboBoxColumn
能否根据第一列下拉选项变化时 触发事件,
让第二列textbox内容和只读属性跟着变化
不知道我表达明白没,谢谢
------解决方案--------------------
绑定数据源
DataTable dts = new DataTable();
dts.Columns.Add("sSex");
dts.Columns.Add("sName");
for (int i = 0; i < 10; i++)
{
DataRow dr2=dts.NewRow();
if (i < 5)
dr2[0] = "男";
else
dr2[0] = "女";
dr2[1] = i.ToString();
dts.Rows.Add(dr2);
}
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = dts;
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataPropertyName = "sSex";
col.HeaderText = "性别";
DataTable dt = new DataTable();
dt.Columns.Add("sexname");
dt.Columns.Add("sexvalue");
DataRow dr = dt.NewRow();
dr[0] = "男";
dr[1] = "男";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "女";
dr1[1] = "女";
dt.Rows.Add(dr1);
col.DataSource = dt;
col.ValueMember = "sexvalue";
col.DisplayMember = "sexname";
this.dataGridView1.Columns.Add(col);
DataGridViewTextBoxColumn cols = new DataGridViewTextBoxColumn();
cols.DataPropertyName = "sName";
cols.HeaderText = "名称";
this.dataGridView1.Columns.Add(cols);
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dgr = this.dataGridView1.Rows[e.RowIndex];
if (dgr.Cells[0].Value.ToString() == "男")
{
dgr.Cells[1].ReadOnly = true;
dgr.Cells[1].Value = "男的";
}
else
{
dgr.Cells[1].ReadOnly = false;
dgr.Cells[1].Value = "女的";
}
}
------解决方案--------------------
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dgr = this.dataGridView1.Rows[e.RowIndex];
if (dgr.Cells[0].Value.ToString() == "男")
{
dgr.Cells[1].ReadOnly = true;
dgr.Cells[1].Value = "男的";
}
else
{
dgr.Cells[1].ReadOnly = false;
dgr.Cells[1].Value = "女的";
}
}
------解决方案--------------------
可以在DataGridView.EditingControlShowing 事件中处理
举例
C# code
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= new
EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectedIndexChanged += new
EventHandler(cb_SelectedIndexChanged);
}
}
v