winfrom datagridview中DataGridViewTextBoxColumn的联动处理
2015-06-04 15:57
302 查看
这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件
方法一:
首先 窗体上拖拽一个 DataGridview
然后在这个DataGridview中添加两列DataGridViewTextBoxColumn (第一列叫A,第二列叫B)
然后绑定A代码
A.DataSource = ds.Tables[0].DefaultView;
A.DisplayMember = "table_name";
A.ValueMember = "table_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).DefaultCellStyle.NullValue = "--请选择--"; //默认值
其次是绑定B代码
//当前选中行的第二列赋值
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).DataSource = ds.Tables[0].DefaultView;
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).DisplayMember = "comments";
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).ValueMember = "column_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[2]).DefaultCellStyle.NullValue = "--请选择--";
然后添加SelectedIndexChanged事件代码
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.CurrentCell.OwningColumn.Name == "A")
{
ComboBox cb = (ComboBox)e.Control;
cb.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
SelectedIndexChanged事件代码
public void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (dataGridView1.CurrentCell.OwningColumn.Name == "表名")
{
if (comboBox.Text != "")
{
//这是绑定B的方法
DBFieldNote(comboBox.Text);
}
}
}
方法二:
首先实例化combobox对象
private ComboBox comboBox = new ComboBox();
private ComboBox cb = new ComboBox();
其次:
this.dataGridView1.Controls.Add(comboBox);//将控件添加到DataGridview中
DBTableName();//绑定comboBox
comboBox.Visible = false;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);//添加事件
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string name = ((ComboBox)sender).Text;
dataGridView1.CurrentCell.Value = name;//将选中的值添加到DataGridview当前选中的单元格里
DBFieldNote(name);//绑定第二个combobox(也就是cb)
}
public void DBFieldNote(string tablename)
{
this.dataGridView1.Controls.Add(cb);
cb.Visible = false;
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
......
}
private void cb_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = cb.Text;
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell != null)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)//如果选中的是第一列 就显示第一个combobox
{
System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex ,false);//获取当前选中的单元格的属性(宽 ,高等)
comboBox.Left = rect.Left;
comboBox.Top = rect.Top;
comboBox.Width = rect.Width;
comboBox.Height = rect.Height;
comboBox.Visible = true;
}
else if (dataGridView1.CurrentCell.ColumnIndex==2)//如果是选中第二列就显示cb
{
System.Drawing.Rectangle rect1 = dataGridView1.GetCellDisplayRectangle(comboxIndex + 1, dataGridView1.CurrentCell.RowIndex, false);
cb.Left = rect1.Left;
cb.Top = rect1.Top;
cb.Width = rect1.Width;
cb.Height = rect1.Height;
cb.Visible = true;
}
}
else
{
comboBox.Visible = false;
}
}
方法一:
首先 窗体上拖拽一个 DataGridview
然后在这个DataGridview中添加两列DataGridViewTextBoxColumn (第一列叫A,第二列叫B)
然后绑定A代码
A.DataSource = ds.Tables[0].DefaultView;
A.DisplayMember = "table_name";
A.ValueMember = "table_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[0]).DefaultCellStyle.NullValue = "--请选择--"; //默认值
其次是绑定B代码
//当前选中行的第二列赋值
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).DataSource = ds.Tables[0].DefaultView;
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).DisplayMember = "comments";
((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1]).ValueMember = "column_name";
((DataGridViewComboBoxColumn)dataGridView1.Columns[2]).DefaultCellStyle.NullValue = "--请选择--";
然后添加SelectedIndexChanged事件代码
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.CurrentCell.OwningColumn.Name == "A")
{
ComboBox cb = (ComboBox)e.Control;
cb.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
SelectedIndexChanged事件代码
public void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (dataGridView1.CurrentCell.OwningColumn.Name == "表名")
{
if (comboBox.Text != "")
{
//这是绑定B的方法
DBFieldNote(comboBox.Text);
}
}
}
方法二:
首先实例化combobox对象
private ComboBox comboBox = new ComboBox();
private ComboBox cb = new ComboBox();
其次:
this.dataGridView1.Controls.Add(comboBox);//将控件添加到DataGridview中
DBTableName();//绑定comboBox
comboBox.Visible = false;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);//添加事件
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string name = ((ComboBox)sender).Text;
dataGridView1.CurrentCell.Value = name;//将选中的值添加到DataGridview当前选中的单元格里
DBFieldNote(name);//绑定第二个combobox(也就是cb)
}
public void DBFieldNote(string tablename)
{
this.dataGridView1.Controls.Add(cb);
cb.Visible = false;
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
......
}
private void cb_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = cb.Text;
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell != null)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)//如果选中的是第一列 就显示第一个combobox
{
System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex ,false);//获取当前选中的单元格的属性(宽 ,高等)
comboBox.Left = rect.Left;
comboBox.Top = rect.Top;
comboBox.Width = rect.Width;
comboBox.Height = rect.Height;
comboBox.Visible = true;
}
else if (dataGridView1.CurrentCell.ColumnIndex==2)//如果是选中第二列就显示cb
{
System.Drawing.Rectangle rect1 = dataGridView1.GetCellDisplayRectangle(comboxIndex + 1, dataGridView1.CurrentCell.RowIndex, false);
cb.Left = rect1.Left;
cb.Top = rect1.Top;
cb.Width = rect1.Width;
cb.Height = rect1.Height;
cb.Visible = true;
}
}
else
{
comboBox.Visible = false;
}
}
相关文章推荐
- 【转】ubuntu下解压缩zip,tar,tar.gz和tar.bz2文件
- c/c++字符串传递
- request的用法
- SAT数学题练习之概率部分
- dojo中tree的使用教程(二)
- abstract修饰符 和 interface修饰符
- hiho一下 第三周
- SDL2.0在mfc窗口中显示yuv 数据
- windows 编译libxml2 64位及32位都可以使用
- 用cut,awk删除某一列
- 读取配置文件,获取值
- Python Knowledge
- eclipse中开机优化1
- 第十四周项目1 - 小玩文件(2)
- C# tostring 格式化输出
- 第二章 Spring MVC入门
- 02-线性结构2. 一元多项式求导 (25)
- 安装sqlserver 遇到服务操作系统与安装文件语言环境不一致问题
- Caffe 代码解析-convert_imageset
- JS事件委托