locked
DataGridView - Winforms RRS feed

  • Question

  • Can someone please explain what the deal is with the DataGridView? I don't mean to sound negative, but why is it so difficult to select specific cells during events? Also, why does autocomplete move you down to the next row when you select a popup value with your left mouse click? How do you keep a user from moving to the next cell on the right before data is entered in the current cell and validated?

    Thanks!

    • Moved by Paul Zhou Friday, January 13, 2012 5:45 AM move for better support (From:.NET Base Class Library)
    Thursday, January 12, 2012 12:58 PM

Answers

  • Everything works fine in this test project, if there's anything you think I misunderstood you, then please re-describe your question with more clearly information, so that we can know what function you did, what codes you used, and what result you expected.

    https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!696&parid=BB789F72272D4858!685

            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
    
                this.Column1.DataSource = dt;
                this.Column1.ValueMember = "col0";
                this.Column1.DisplayMember = "col1";
    
                this.dataGridView1.Rows.Add(3);
    
                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
    
            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
                    te.AutoCompleteMode = AutoCompleteMode.Append;
                    te.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two", "three"});
                }
            }
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 13, 2012 6:19 AM
  • Hi dgrm44

    Maybe you can have a try something like this following:)

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
                DataGridViewComboBoxColumn dccol =  this.Columns[1] as DataGridViewComboboxColumn;
    dccol.DataSource = dt;

    dccol.ValueMember = "col0";
    dccol.DisplayMember = "col1";
    ………………………… }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处



    Tuesday, January 17, 2012 3:07 AM
  • Do you mean that you cannot access the skydrive web site?

    I copy out the code here:

    //Form1.cs
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ConsoleApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
    
                this.Column1.DataSource = dt;
                this.Column1.ValueMember = "col0";
                this.Column1.DisplayMember = "col1";
    
                this.dataGridView1.Rows.Add(3);
    
                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
    
            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
                    te.AutoCompleteMode = AutoCompleteMode.Append;
                    te.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two", "three"});
                }
            }
        }
    }
    
    //Form1.Designer.cs
    namespace ConsoleApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2});
                this.dataGridView1.Location = new System.Drawing.Point(13, 13);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(259, 155);
                this.dataGridView1.TabIndex = 0;
                // 
                // Column1
                // 
                this.Column1.HeaderText = "Column1";
                this.Column1.Name = "Column1";
                // 
                // Column2
                // 
                this.Column2.HeaderText = "Column2";
                this.Column2.Name = "Column2";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        }
    }


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, January 17, 2012 9:32 AM

All replies

  • I'm moving this thread to WinForm forum to get better support.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 13, 2012 5:44 AM
  • Everything works fine in this test project, if there's anything you think I misunderstood you, then please re-describe your question with more clearly information, so that we can know what function you did, what codes you used, and what result you expected.

    https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!696&parid=BB789F72272D4858!685

            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
    
                this.Column1.DataSource = dt;
                this.Column1.ValueMember = "col0";
                this.Column1.DisplayMember = "col1";
    
                this.dataGridView1.Rows.Add(3);
    
                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
    
            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
                    te.AutoCompleteMode = AutoCompleteMode.Append;
                    te.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two", "three"});
                }
            }
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 13, 2012 6:19 AM
  • this.Column1.DataSource = dt;
    this.Column1.ValueMember = "col0";
    this.Column1.DisplayMember = "col1";

    These are errors as Column1 does not have these properties.

    Friday, January 13, 2012 3:54 PM
  • Column1 type is the DataGridViewComboBoxColumn, while the Column2 type is the DataGridViewTextBoxColumn.
    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Saturday, January 14, 2012 4:44 AM
  • I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 7:12 AM
  • The status is I am unable to download the example you posted and your sample code is incomplete so I have no answers yet to any of my original questions.
    Monday, January 16, 2012 12:23 PM
  • Hi dgrm44

    Maybe you can have a try something like this following:)

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
                DataGridViewComboBoxColumn dccol =  this.Columns[1] as DataGridViewComboboxColumn;
    dccol.DataSource = dt;

    dccol.ValueMember = "col0";
    dccol.DisplayMember = "col1";
    ………………………… }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处



    Tuesday, January 17, 2012 3:07 AM
  • Column1 type is the DataGridViewComboBoxColumn, while the Column2 type is the DataGridViewTextBoxColumn.
    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    If you dynamically add a column into DataGridView, I'm afraid you cannot get Column1 as a public from dataGridView directly,but you can only do something through the accessor——

    DataGridView1.Columns[index,0-based]……

    And later I've noticed it that you are using "this.Column1"?Have you had a public self-defined property in the WinForm itself???

    Correct me if I'm wrong:)


       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Tuesday, January 17, 2012 6:06 AM
  • Do you mean that you cannot access the skydrive web site?

    I copy out the code here:

    //Form1.cs
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ConsoleApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
    
                dt.Rows.Add(0, "mike0");
                dt.Rows.Add(1, "mike1");
    
                this.Column1.DataSource = dt;
                this.Column1.ValueMember = "col0";
                this.Column1.DisplayMember = "col1";
    
                this.dataGridView1.Rows.Add(3);
    
                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
    
            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
                    te.AutoCompleteMode = AutoCompleteMode.Append;
                    te.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two", "three"});
                }
            }
        }
    }
    
    //Form1.Designer.cs
    namespace ConsoleApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2});
                this.dataGridView1.Location = new System.Drawing.Point(13, 13);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(259, 155);
                this.dataGridView1.TabIndex = 0;
                // 
                // Column1
                // 
                this.Column1.HeaderText = "Column1";
                this.Column1.Name = "Column1";
                // 
                // Column2
                // 
                this.Column2.HeaderText = "Column2";
                this.Column2.Name = "Column2";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        }
    }


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, January 17, 2012 9:32 AM
  • I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 8:05 AM