| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1120 人关注过本帖
标题:如何实现在DBGrid中实现下拉组合框?
只看楼主 加入收藏
deria
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-2-18
收藏
 问题点数:0 回复次数:7 
如何实现在DBGrid中实现下拉组合框?
请问各们高手,我想请教各位如何实现在DBGrid中实现下拉组合框?谢谢了!
搜索更多相关主题的帖子: DBGrid中 
2006-04-25 17:56
deria
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-2-18
收藏
得分:0 
有没有知道啊,在线等!谢谢了!

我的论坛送LFJ,FF,CCF的邀请! http://.cn/bbs/index.php
2006-04-26 09:33
deria
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-2-18
收藏
得分:0 

知道的教教我吧!


我的论坛送LFJ,FF,CCF的邀请! http://.cn/bbs/index.php
2006-04-28 10:22
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 

VS2003的DG好象没有这项功能,但2005的有,不行的话用VS2005试试


2006-04-28 14:14
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
是WinForm下还是ASP.NET中?

如果是ASP.NET中,则使用模版列,将下拉框放进去

如果是WinForm中,则在DataGrid控件的CurrentCellChanged事件中把下拉框写进去

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-04-28 14:41
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
如果是Asp.net则创建一个模板列,将下拉框控件托放进去即可.
如果是Windows则需要设置一下.

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

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);
 }
}

}

2006-04-28 20:09
deria
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2006-2-18
收藏
得分:0 
以下是引用梦幻情缘在2006-4-28 20:09:00的发言:
如果是Asp.net则创建一个模板列,将下拉框控件托放进去即可.
如果是Windows则需要设置一下.

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

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);
 }
}

}

这个程序在我的电脑上怎么运行不了啊,希望楼主最好能把那个文件上传上来看看,谢谢了!


我的论坛送LFJ,FF,CCF的邀请! http://.cn/bbs/index.php
2006-05-08 09:45
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
给个邮箱,我给你发去看看

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-05-09 11:59
快速回复:如何实现在DBGrid中实现下拉组合框?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026984 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved