选择显示字体大小

c#中为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等类似的控件。
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons