六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

C#中為DataGrid添加下拉下文框

[摘要]本文將介紹如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件,主要包括三方面的內(nèi)容。   1. 在DataGrid中加入ComboBox列;  2. 把在DataGrid中的修改保存到對(duì)應(yīng)的網(wǎng)格;   3. 設(shè)置DataGrid中網(wǎng)格的焦點(diǎn)。  下面是...

  本文將介紹如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件,主要包括三方面的內(nèi)容。

  1. 在DataGrid中加入ComboBox列;

  2. 把在DataGrid中的修改保存到對(duì)應(yīng)的網(wǎng)格;
 
  3. 設(shè)置DataGrid中網(wǎng)格的焦點(diǎn)。

  下面是整個(gè)源代碼,一些功能可以看注釋。

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 窗體設(shè)計(jì)器生成的代碼

  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 = "獲取焦點(diǎn)";
   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>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  //初始化DataGrid
  private void PopulateGrid()
  {
   //創(chuàng)建一個(gè)DataTable對(duì)象,包括四列,前三列為String,最后一列為Boolean。
   dtblFunctionalArea = new DataTable ("FunctionArea");
   string[] arrstrFunctionalArea = new string [3]{"Functional Area","Min","Max"};
   DataColumn dtCol = null;
   //創(chuàng)建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);
   }

   //創(chuàng)建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;

    //設(shè)置列的寬度
    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[]{"選項(xiàng)一","選項(xiàng)二","選項(xiàng)三"});
   cmbFunctionArea.Cursor = Cursors.Arrow;
   cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
   cmbFunctionArea.Dock = DockStyle.Fill;

   //在選定項(xiàng)發(fā)生更改并且提交了該更改后發(fā)生

   cmbFunctionArea.SelectionChangeCommitted += new  EventHandler(cmbFunctionArea_SelectionChangeCommitted);

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

   dgtb.TextBox.Controls.Add(cmbFunctionArea);

  }

  //設(shè)置焦點(diǎn)模擬

  private void GetFocus(int row,int col)
  {
   //先把焦點(diǎn)移動(dòng)到DataGrid
   this.dgdFunctionArea.Focus();
   //把焦點(diǎn)移動(dòng)到DataGridCell
   DataGridCell dgc = new DataGridCell(row,col);
   this.dgdFunctionArea.CurrentCell = dgc;
   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[col];

   //設(shè)置焦點(diǎn)

   dgtb.TextBox.Focus();
  }

  //把Combobox上修改的數(shù)據(jù)提交到當(dāng)前的網(wǎng)格

 private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
 {
  this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();

 }

 //設(shè)置新的焦點(diǎn)

 private void buttonFocus_Click(object sender, System.EventArgs e)
 {
  //焦點(diǎn)模擬,這里設(shè)置第三行第一列
  GetFocus(2,0);
 }
}

}

  下面是測(cè)試界面:

  總結(jié),這里是通過DataGridTextBoxColumn.TextBox.Controls.Add方法實(shí)現(xiàn)在列中添加ComboBox控件;對(duì)于數(shù)據(jù)的保存是使用ComboBox.SelectionChangeCommitted事件來完成;設(shè)置焦點(diǎn)是通過DataGridTextBoxColumn.TextBox.Focus方法來實(shí)現(xiàn)。另外通過這個(gè)方法也可以添加DateTimePicker等類似的控件。




主站蜘蛛池模板: 中文字幕在线有码高清视频 | 欧美小视频在线 | 手机看片自拍日韩日韩高清 | 四虎精品成人免费观看 | 亚色在线观看 | 日韩啪啪网 | 亚洲免费大片 | 天天干天天做 | 欧美视频一 | 欧美亚洲免费久久久 | 伊人久久大线蕉香港三级 | 日韩一卡 二卡 三卡 四卡 免费视频 | 亚洲免费视频一区 | 真实国产乱子伦精品一区二区三区 | 天天躁夜夜躁狠狠躁2024 | 午夜免费视频观看在线播放 | 天天色成人 | 四虎影院毛片 | 日韩伦理在线视频 | 色网站在线观看 | 亚洲国产精品综合欧美 | 日本 欧美 国产 | 青青热久免费精品视频网站 | 色偷偷尼玛图亚洲综合 | 亚洲精品美女在线观看 | 亚洲人xx视频 | 色综合久久伊人 | 色视频在线观看免费 | 青草悠悠视频在线观看 | 天天干免费视频 | 亚洲成a人一区二区三区 | 婷婷综合久久中文字幕一本 | 午夜在线日韩免费精品福利 | 日韩精品在线观看免费 | 午夜视频www | 欧美综合国产精品日韩一 | 五月婷婷精品 | 中国黄色毛片 大片 | 亚洲欧洲综合网 | 社区天堂 | 青青草国产精品久久久久 |