DataGridView控件中加入下拉框的编程实现

以下是在网上看到的网友发的文章“VC++.NET2005中DataGridView控件中加入下拉框的编程实现”用C#作了

修改的后的方法如下:

VC#2005中DataGridView控件中加入下拉框的编程实现

VS2005中新增加了数据浏览控件DataGridView,虽然我们可以通过其DataGridViewComboBoxColumn方法在DataGridView中添加下拉框列,但随之而来得问题是一整列的下拉框,很不美观,并且还要编程为其绑定数据,不符合.NET的尽量少干涉的原则。我最近通过对其Form控件的事件的简单编程实现了VS2005中DataGridView控件中加入下拉框的功能,写出来供大家参考:
在VS2005中创立一个C#语言的windows窗体应用程序,然后在Form1中添加一个DataGridView控件,这时系统会提示你为DataGridView控件绑定数据。完成以上工作后,在Form1上添加一个comboBox控件comboBox1
如图:
VC#2005中DataGridView控件中加入下拉框的编程实现

然后,用鼠标在Form1窗体上双击,进入 窗体Form1_Load事件代码编写处,如下所示:

private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;
this.DataGridView1.Controls.Add(this.comboBox1);
}
然后再进入到dataGridView1的CurrentCellChanged事件中加入以下代码:
try
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;
  if(this.dgvStu.CurrentCell == null)
                {
                    this.cobgv.Visible = false;
                    this.cobgv.Width = 0;
                }
                else
                {
                    if (this.dgvStu.CurrentCell.ColumnIndex == 2)//下拉框所放置的列
                    {
                        this.cobgv.Visible = false;
                        this.cobgv.Width = 0;
                        this.cobgv.Left = this.dgvStu.GetCellDisplayRectangle(this.dgvStu.CurrentCell.ColumnIndex, this.dgvStu.CurrentCell.RowIndex, true).Left;
                        this.cobgv.Top = this.dgvStu.GetCellDisplayRectangle(this.dgvStu.CurrentCell.ColumnIndex, this.dgvStu.CurrentCell.RowIndex, true).Top;
                        this.cobgv.Width = this.dgvStu.GetCellDisplayRectangle(this.dgvStu.CurrentCell.ColumnIndex, this.dgvStu.CurrentCell.RowIndex, true).Width;
                        string ffff = Convert.ToString(this.dgvStu.CurrentCell.Value);
                        this.cobgv.Text = ffff;
                        this.cobgv.Visible = true;
                    }
                    else
                    {
                        this.cobgv.Visible = false;
                        this.cobgv.Width = 0;
                    }
                }
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


其中ColumnIndex == 1的 1 是你希望把comboBox1放在那一列上使用,这由你的需要而定。
再,这里一定要用TRY.........Catch结构,不然当你使用时,鼠标单击到列标题时,就会出现异常。

然后进入到dataGridView1的Scroll事件,加入以下代码:
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;

然后进入到comboBox1的SelectionChangeCommitted事件,加入以下代码:
dataGridView1.CurrentCell.Value = ((System.Windows.Forms.ComboBox)sender).SelectedItem.ToString();

然后进入到comboBox1的KeyPress事件,加入以下代码:
this.comboBox1.Visible = false;
  this.comboBox1.Width = 0;
comboBox1的KeyPress事件加入的代码主要解决编辑下拉框所在单元的数据时,如果不选用下拉框提供的
选项,而自己输入的问题。

以上就完成了下拉框的加入,我觉得这种方法比较简单,既不用重写控件,又利用了.NET提供的简单、便捷的设计方法,可以一用。在具体使用中可根据需要随便加入几个下拉框。以下几张图可见效果:
运行开始:
VC#2005中DataGridView控件中加入下拉框的编程实现

鼠标点击到下拉框单元:
VC#2005中DataGridView控件中加入下拉框的编程实现

开始在下拉框中选择:
VC#2005中DataGridView控件中加入下拉框的编程实现

选择完,鼠标离开下拉框单元后:
VC#2005中DataGridView控件中加入下拉框的编程实现

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值