在DataGrid中简单使用下拉列表框

DataGrid中使用下拉列表问题。这篇文章讲了如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件。不过原文不全,无法调试,在这里为了说清楚点,对原文作了一些修改,整篇文章主要包括三方面的内容。

1.   DataGrid中加入ComboBox列;

2.   把在DataGrid中的修改保存到对应的网格;

3.   设置DataGrid中网格的焦点。

 

下面是整个源代码,一些功能可以看注释。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace DataGridTest

{

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.DataGrid dgdFunctionArea;

         private DataTable dtblFunctionalArea;

         private System.Windows.Forms.Button buttonFocus;

         private System.ComponentModel.Container components = null;

 

         public Form1()

         {

              InitializeComponent();

              PopulateGrid();

         }

 

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if (components != null)

                   {

                       components.Dispose();

                   }

              }

              base.Dispose( disposing );

         }

 

         #region Windows 窗体设计器生成的代码

         private void InitializeComponent()

         {

              this.dgdFunctionArea = new System.Windows.Forms.DataGrid();

              this.buttonFocus = new System.Windows.Forms.Button();

              ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).BeginInit();

              this.SuspendLayout();

              //

              // dgdFunctionArea

              //

              this.dgdFunctionArea.DataMember = "";

              this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.ControlText;

              this.dgdFunctionArea.Location = new System.Drawing.Point(4, 8);

              this.dgdFunctionArea.Name = "dgdFunctionArea";

              this.dgdFunctionArea.Size = new System.Drawing.Size(316, 168);

              this.dgdFunctionArea.TabIndex = 0;

              //

              // buttonFocus

              //

              this.buttonFocus.Location = new System.Drawing.Point(232, 188);

              this.buttonFocus.Name = "buttonFocus";

              this.buttonFocus.Size = new System.Drawing.Size(84, 23);

              this.buttonFocus.TabIndex = 1;

              this.buttonFocus.Text = "获取焦点";

              this.buttonFocus.Click += new System.EventHandler(this.buttonFocus_Click);

              //

              // Form1

              //

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(332, 217);

              this.Controls.Add(this.buttonFocus);

              this.Controls.Add(this.dgdFunctionArea);

              this.Name = "Form1";

              this.Text = "Form1";

              ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).EndInit();

              this.ResumeLayout(false);

 

         }

         #endregion

 

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

         [STAThread]

         static void Main()

         {

              Application.Run(new Form1());

         }

         //初始化DataGrid

         private void PopulateGrid()

         {

              //创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。

              dtblFunctionalArea  = new DataTable ("FunctionArea");

              string[] arrstrFunctionalArea = new string [3]{"Functional Area","Min","Max"};

              DataColumn dtCol = null;

              //创建String列      

              for(int i=0; i< 3;i++)

              {   

                   dtCol = new DataColumn(arrstrFunctionalArea[i]);

                   dtCol.DataType  = Type.GetType("System.String");

                   dtCol.DefaultValue = "";

                   dtblFunctionalArea.Columns.Add(dtCol);              

              }    

 

              //创建Boolean列,用CheckedBox来显示。   

              DataColumn dtcCheck = new DataColumn("IsMandatory");

              dtcCheck.DataType = System.Type.GetType("System.Boolean");

              dtcCheck.DefaultValue = false;

              dtblFunctionalArea.Columns.Add(dtcCheck);

 

              //把表绑定到DataGrid

              dgdFunctionArea.DataSource    = dtblFunctionalArea; 

 

              //为DataGrid加载DataGridTableStyle样式

              if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))

              {

                   DataGridTableStyle dgdtblStyle = new DataGridTableStyle();

                   dgdtblStyle.MappingName = dtblFunctionalArea.TableName;

                   dgdFunctionArea.TableStyles.Add(dgdtblStyle);

                   dgdtblStyle.RowHeadersVisible = false;

                   dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;

                   dgdtblStyle.AllowSorting  = false;

                   dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107);

                   dgdtblStyle.RowHeadersVisible = false;

                   dgdtblStyle.HeaderForeColor = Color.White;

                   dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, 

                       System.Drawing.FontStyle.Bold,

                       System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

                   dgdtblStyle.GridLineColor = Color.DarkGray;

                   dgdtblStyle.PreferredRowHeight = 22;

                   dgdFunctionArea.BackgroundColor = Color.White; 

 

                   //设置列的宽度 

                   GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles[0].GridColumnStyles;

                   colStyle[0].Width = 100;

                   colStyle[1].Width = 50;

                   colStyle[2].Width = 50;

                   colStyle[3].Width = 80;

              }

 

              DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[0];

              ComboBox cmbFunctionArea = new ComboBox();

              cmbFunctionArea.Items.AddRange(new object[]{"选项一","选项二","选项三"});

              cmbFunctionArea.Cursor = Cursors.Arrow;

              cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;

              cmbFunctionArea.Dock = DockStyle.Fill;

              //在选定项发生更改并且提交了该更改后发生

              cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted);

              //把ComboBox添加到DataGridTableStyle的第一列

              dgtb.TextBox.Controls.Add(cmbFunctionArea);            

 

         }

         //设置焦点模拟

         private void GetFocus(int row,int col)

         {

              //先把焦点移动到DataGrid

              this.dgdFunctionArea.Focus();  

              //把焦点移动到DataGridCell

              DataGridCell dgc = new DataGridCell(row,col);

              this.dgdFunctionArea.CurrentCell = dgc;

              DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[col];

              //设置焦点

              dgtb.TextBox.Focus();

 

         }         

         //把Combobox上修改的数据提交到当前的网格

         private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)

         {

              this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();

         }       

         //设置新的焦点

         private void buttonFocus_Click(object sender, System.EventArgs e)

         {

              //焦点模拟,这里设置第三行第一列

              GetFocus(2,0);

         }

     }

}

下面是测试界面:

 

         总结,这里是通过DataGridTextBoxColumn.TextBox.Controls.Add方法实现在列中添加ComboBox控件;对于数据的保存是使用ComboBox.SelectionChangeCommitted事件来完成;设置焦点是通过DataGridTextBoxColumn.TextBox.Focus方法来实现。另外通过这个方法也可以添加DateTimePicker等类似的控件。

 

 

         总结,这里是通过DataGridTextBoxColumn.TextBox.Controls.Add方法实现在列中添加ComboBox控件;对于数据的保存是使用ComboBox.SelectionChangeCommitted事件来完成;设置焦点是通过DataGridTextBoxColumn.TextBox.Focus方法来实现。另外通过这个方法也可以添加DateTimePicker等类似的控件。

 


[ 点击此处收藏本文]
发表于 2004年05月31日 4:50 PM
href="http://blog.csdn.net/zhzuo/Services/Pingback.aspx" rel="pingback"/>

jacky 发表于 2004-07-28 1:33 AM  
如果添加的Combo的数据源是动态的呢?
比如Functional Area中Combo的选项内容需要根据 Min的值来从数据库中取对应的值。

秋枫 发表于 2004-07-28 2:51 PM  
使用ComboBox.DropDown 事件来完成工作,
为ComboBox控注册DropDown事件,以上面的例子为例,
cmbFunctionArea.DropDown += new EventHandler(cmbFunctionArea_DropDown);

//事件处理函数如下:
private void cmbFunctionArea_DropDown(object sender, EventArgs e)
{
//这里编写根据其他列的值动态从数据库或内存数据集或其他数据源获取数据,
//不过需要注意依赖列没有数据的情况,
//下面是我模拟用ArrayList做容器来获取数据 。
ComboBox box = (ComboBox)sender;
//box.DataSource = null;
//box.Items.Clear();
ArrayList al = new ArrayList();
//获取数据
Random r = new Random();
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
box.DataSource = al;
//上面是绑定,也可以直接填充到box的项中去
}

ClampHammer 发表于 2004-07-28 3:01 PM  
有一个问题,我是将combobox绑定到一个数据源的,我运行了你的例子
不错,但是我有一个标,他的主键要在下拉框里面选,但是新增一行时,combobox中的值
均未与绑定管理器currencyManager里面的值对应(事实上,新行的combobox超出了绑定管理器的
范围,这里我不知道怎么才能把这个新行更新到数据源中。

秋枫 发表于 2004-07-28 3:18 PM  
如果DataGrid是绑定在DataView上,
我在上面的函数中可以增加一些代码如下:

//把Combobox上修改的数据提交到当前的网格
private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
{
this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();
//提交当前的更改
this.BindingContext[this.dgdFunctionArea.DataSource,this.dgdFunctionArea.DataMember].EndCurrentEdit();

}
//这里ComboBox实际上起到了选择数据的作用,并没有其他的实现。

jacky 发表于 2004-07-28 8:06 PM  
那么如何在mbFunctionArea_DropDown中如何取得当前行中的Min列的内容呢?

秋枫 发表于 2004-07-30 8:35 AM  
//下面显示的需要的相关行的数据,
private void cmbFunctionArea_DropDown(object sender, EventArgs e)
{
//显示相关列的数据
MessageBox.Show(this.dgdFunctionArea[this.dgdFunctionArea.CurrentRowIndex,1].ToString());
//这里编写根据其他列的值动态从数据库或内存数据集或其他数据源获取数据,
//不过需要注意依赖列没有数据的情况,
//下面是我模拟用ArrayList做容器来获取数据 。
ComboBox box = (ComboBox)sender;
//box.DataSource = null;
//box.Items.Clear();
ArrayList al = new ArrayList();
//获取数据
Random r = new Random();
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
al.Add(r.Next(1,1000));
box.DataSource = al;
//上面是绑定,也可以直接填充到box的项中去
}

btut2004 发表于 2004-08-31 10:39 AM  
dgColumn=new DataGridTextBoxColumn();//建一个新列
ComboBox cmbFunctionArea = new ComboBox();
cmbFunctionArea.Items.AddRange(new object[]{"选项一","选项二","选项三"});
cmbFunctionArea.Cursor = Cursors.Arrow;
cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
cmbFunctionArea.Dock = DockStyle.Fill;
//在选定项发生更改并且提交了该更改后发生
//cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted);
//把ComboBox添加到DataGridTableStyle的第一列
dgColumn.TextBox.Controls.Add(cmbFunctionArea);//把控件加入到列中
dgtStyle.GridColumnStyles.Add(dgColumn);//把列设置加入到样式表中


为什么这样的代码显示不出这个列,但也不会出错??????
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值