VC++.NET2005中DataGridView控件中加入下拉框的编程实现

来源:互联网 发布:淘宝店铺全屏轮播代码 编辑:程序博客网 时间:2024/04/25 20:03

VC++.NET2005中DataGridView控件中加入下拉框的编程实现             

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

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



        图中的“ // TODO: 这行代码将数据加载到表“guoDataSet.Table_1”中。您可以根据需要移动或移除它。
     this->Table_1TableAdapter->Fill(this->guoDataSet->Table_1);”两行是你在绑定数据时系统自动加上的。你要在其后加上三行代码:
           
             this->dataGridView1->Controls->Add(this->comboBox1);
             this->comboBox1->Visible = false;
              this->comboBox1->Width = 0;

        然后再进入到dataGridView1的CurrentCellChanged事件中加入以下代码:
           try{
                   if(this->dataGridView1->CurrentCell->ColumnIndex == 1)
                 {
                     comboBox1->Visible = false;
                     comboBox1->Width = 0;
                     comboBox1->Left = dataGridView1->GetCellDisplayRectangle(dataGridView1->CurrentCell-         >ColumnIndex,dataGridView1->CurrentCell->RowIndex,true).Left;
               //(上两行是一行,叶面不够,写代码时要写在一行)


                     comboBox1->Top = dataGridView1->GetCellDisplayRectangle(dataGridView1->CurrentCell->ColumnIndex,dataGridView1->CurrentCell->RowIndex,true).Top;
                 //  (上两行是一行,叶面不够,写代码时要写在一行)
                    comboBox1->Text = this->dataGridView1->CurrentCell->Value->ToString();
                    comboBox1->Width = dataGridView1->GetCellDisplayRectangle(dataGridView1->CurrentCell->ColumnIndex,dataGridView1->CurrentCell->RowIndex,true).Width;
              //(上两行是一行,叶面不够,写代码时要写在一行)
                  comboBox1->Visible = true;
      }
 else
    {
           this->comboBox1->Visible = false;
     this->comboBox1->Width = 0;
  }   
    }
 catch(Exception^ e)
   {
      }  

     其中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提供的简单、便捷的设计方法,可以一用。在具体使用中可根据需要随便加入几个下拉框。以下几张图可见效果:
                运行开始:

         
                   鼠标点击到下拉框单元:


            开始在下拉框中选择:


           选择完,鼠标离开下拉框单元后:

posted on 2006-05-14 17: 

C#修改一下把->改为.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1562372