• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

        在DataGrid中容易運用下拉下文框

        在DataGrid中容易運用下拉下文框

        更新時間:2022-05-10 文章作者:未知 信息來源:網絡 閱讀次數:

        在DataGrid中簡單使用下拉列表框

        作者:Tushar Ameta

        翻譯:秋楓

        在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等類似的控件。

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 亚洲乱码中文字幕在线| 中文字幕亚洲精品资源网| 激情小说亚洲图片| 在线播放免费人成视频在线观看| 亚洲第一永久在线观看| 免费观看国产网址你懂的| 亚洲女人初试黑人巨高清| 免费看黄视频网站| 亚洲精品无码久久久久久| 国产精品无码素人福利免费| 菠萝菠萝蜜在线免费视频| 亚洲片国产一区一级在线观看| 春意影院午夜爽爽爽免费| 亚洲日韩在线观看| 久久国产精品免费看| 亚洲黄色在线网站| 日韩吃奶摸下AA片免费观看| 亚洲精品无码专区| 亚洲麻豆精品国偷自产在线91| a级毛片在线免费| 亚洲春色在线观看| 日本一道高清不卡免费| 成人a毛片免费视频观看| 亚洲AV无码一区二区乱子伦| 精品免费人成视频app| 亚洲国产成人AV在线播放| 国产a v无码专区亚洲av| 67194成手机免费观看| 亚洲日韩精品A∨片无码加勒比| 亚洲国产精品狼友中文久久久| 免费看黄的成人APP| 亚洲一区二区三区久久久久| 日韩亚洲国产综合久久久| 国产免费拔擦拔擦8X高清在线人| 亚洲一区无码中文字幕乱码| www.亚洲色图.com| 每天更新的免费av片在线观看| 色九月亚洲综合网| 精品亚洲成a人片在线观看 | 国产精品久久免费| 成人亚洲国产精品久久|