winfrom表格DataGridView下拉框DataGridViewComboBoxColumn选择事件
来源:互联网 发布:数据挖掘 毕业论文 编辑:程序博客网 时间:2024/04/25 08:34
1、列的ColumnType选择:DataGridViewComboBoxColumn;
2、表格的属性EditMode设置为EditOnEnter;
3、表格添加EditingControlShowing事件;
4、实现单击一次显示下拉列表框,添加CellEnter事件;
bool isBind = false;//是否已经绑定事件
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//判断要处理的DataGridViewComboBoxColumn名称,若符合条件,编辑控件被强制转换为ComboBox以处理,添加SelectedIndexChanged事件
if (this.dataGridView1.CurrentCell.OwningColumn.Name == "Good" && dataGridView1.CurrentCell.RowIndex != -1)
{
System.Windows.Forms.ComboBox cb = (System.Windows.Forms.ComboBox)e.Control;
if (!isBind)
{
cb.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
isBind = true;
}
}
else
{
isBind = false;
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
System.Windows.Forms.ComboBox cb = (System.Windows.Forms.ComboBox)sender;
//MessageBox.Show("dao");
if (cb.SelectedIndex > -1 && cb.SelectedValue != null && cb.SelectedValue.ToString() != "System.Data.DataRowView")
{
string ss = cb.SelectedValue.ToString();
int index = int.Parse(cb.SelectedValue.ToString());
DataGridViewRow dv = this.dataGridView1.CurrentRow;
if (index > -1)
{
DataRow[] dr = dtPrice.Select("id=" + index.ToString());
if (dr.Count() > 0)
{
dv.Cells["price"].Value = dr[0]["price"].ToString();
}
}
}
if (isBind)
{
cb.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
isBind = false;
}
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
//实现单击一次显示下拉列表框
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
{
SendKeys.Send("{F4}");
}
}
5、数据绑定:
DataTable dtPrice = new DataTable();
dtPrice = new DataTable();
dtPrice.Columns.Add("id", typeof(int));
dtPrice.Columns.Add("price", typeof(double));
DataRow drp1 = dtPrice.NewRow();
drp1["id"] = 1;
drp1["price"] = "555";
dtPrice.Rows.Add(drp1);
DataRow drp2 = dtPrice.NewRow();
drp2["id"] = 2;
drp2["price"] = "6";
dtPrice.Rows.Add(drp2);
DataRow drp3 = dtPrice.NewRow();
drp3["id"] = 3;
drp3["price"] = "90";
dtPrice.Rows.Add(drp3);
DataTable dtGood = new DataTable();
dtGood.Columns.Add("id",typeof(int));
dtGood.Columns.Add("name", typeof(string));
DataRow dr1 = dtGood.NewRow();
dr1["id"] = 1;
dr1["name"] = "手机";
dtGood.Rows.Add(dr1);
DataRow dr2 = dtGood.NewRow();
dr2["id"] = 2;
dr2["name"] = "苹果";
dtGood.Rows.Add(dr2);
DataRow dr3 = dtGood.NewRow();
dr3["id"] = 3;
dr3["name"] = "衣服";
dtGood.Rows.Add(dr3);
//Good为下拉框的名称
this.Good.DataSource = dtGood;
this.Good.DisplayMember = "name";
this.Good.ValueMember = "id";
this.Good.DataPropertyName = "id";
DataTable dtSource = new DataTable();
dtSource.Columns.Add("id", typeof(int));
dtSource.Columns.Add("price", typeof(int));
DataRow drs1 = dtSource.NewRow();
drs1["id"] = 1;
drs1["price"] = 555;
dtSource.Rows.Add(drs1);
DataRow drs2 = dtSource.NewRow();
drs2["id"] = 2;
drs2["price"] = 666;
dtSource.Rows.Add(drs2);
this.dataGridView1.DataSource = dtSource;
行编辑时,列“id”存放的是下拉框选择项的id。
- winfrom表格DataGridView下拉框DataGridViewComboBoxColumn选择事件
- winform dataGridView DataGridViewComboBoxColumn 下拉框事件代码
- 设置DataGridView控件DataGridViewComboBoxColumn下拉框默认值
- Datagridview控件实现下拉列表(DataGridViewComboBoxColumn)
- C# DataGridView 中 DataGridViewComboBoxCell 下拉列表框设置选择事件
- datagridview中DataGridViewComboBoxColumn值的比较与选择触发的事件
- DataGridView中comboBox(DataGridViewComboBoxColumn)的事件编写
- DataGridView中comboBox(DataGridViewComboBoxColumn)的事件编写
- DataGridViewComboBoxColumn 添加下拉及触发事件
- 在Datagridview 控件DataGridViewComboBoxColumn怎么点两次才下拉下来
- Winfrom DataGridView好看表格样式调整方法
- WinFrom DataGridView 常用事件执行顺序
- winform datagridview combobox 下拉框 事件
- Winfrom中DataGridView的ComboBox功能实现(Gridview下拉框)
- DataGridView使用之DataGridViewComboBoxColumn
- datagridview中使用DataGridViewComboBoxColumn
- 把datagridview导出到execl表格中、datagridview添加下拉列表框
- 给DataGridView中的控件添加事件和下拉框
- 分享个免费VPN链接
- Oracle外部表 External Table
- 搞清楚CFBundleVersion和CFBundleShortVersionString的区别
- boost::ASIO的异步方式
- NodeJS深受欢迎的6大原因
- winfrom表格DataGridView下拉框DataGridViewComboBoxColumn选择事件
- android -- 编译不同库及编译脚本之区别
- 黑马程序员——Java基础---面向对象(接口)--第7天--第95-97集
- 黑马程序员java自学总结之--模式
- freemarker.core.ParseException:Unexpected end of file reached
- 在vs&qt中使用资源文件
- 使用enable_shared_from_this
- 安卓渗透神器dsploit笔记
- QString字符转换