Datagridview: How to set a cell in editing mode?

前端 未结 5 1787
梦毁少年i
梦毁少年i 2020-11-28 09:50

I need to programmatically set a cell in editing mode. I know that setting that cell as CurrentCell and then call the method BeginEdit(bool), it should happen, but in my cas

5条回答
  •  感动是毒
    2020-11-28 10:39

    I know this question is pretty old, but figured I'd share some demo code this question helped me with.

    • Create a Form with a Button and a DataGridView
    • Register a Click event for button1
    • Register a CellClick event for DataGridView1
    • Set DataGridView1's property EditMode to EditProgrammatically
    • Paste the following code into Form1:
    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            DataTable m_dataTable;
            DataTable table { get { return m_dataTable; } set { m_dataTable = value; } }
    
            private const string m_nameCol = "Name";
            private const string m_choiceCol = "Choice";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            class Options
            {
                public int m_Index { get; set; }
                public string m_Text { get; set; }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                table = new DataTable();
                table.Columns.Add(m_nameCol);
                table.Rows.Add(new object[] { "Foo" });
                table.Rows.Add(new object[] { "Bob" });
                table.Rows.Add(new object[] { "Timn" });
                table.Rows.Add(new object[] { "Fred" });
    
                dataGridView1.DataSource = table;
    
                if (!dataGridView1.Columns.Contains(m_choiceCol))
                {
                    DataGridViewTextBoxColumn txtCol = new DataGridViewTextBoxColumn();
                    txtCol.Name = m_choiceCol;
                    dataGridView1.Columns.Add(txtCol);
                }
    
                List oList = new List();
                oList.Add(new Options() { m_Index = 0, m_Text = "None" });
                for (int i = 1; i < 10; i++)
                {
                    oList.Add(new Options() { m_Index = i, m_Text = "Op" + i });
                }
    
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i += 2)
                {
                    DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
    
                    //Setup A
                    c.DataSource = oList;
                    c.Value = oList[0].m_Text;
                    c.ValueMember = "m_Text";
                    c.DisplayMember = "m_Text";
                    c.ValueType = typeof(string);
    
                    ////Setup B
                    //c.DataSource = oList;
                    //c.Value = 0;
                    //c.ValueMember = "m_Index";
                    //c.DisplayMember = "m_Text";
                    //c.ValueType = typeof(int);
    
                    //Result is the same A or B
                    dataGridView1[m_choiceCol, i] = c;
                }
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns.IndexOf(dataGridView1.Columns[m_choiceCol]))
                    {
                        DataGridViewCell cell = dataGridView1[m_choiceCol, e.RowIndex];
                        dataGridView1.CurrentCell = cell;
                        dataGridView1.BeginEdit(true);
                    }
                }
            }
        }
    }
    

    Note that the column index numbers can change from multiple button presses of button one, so I always refer to the columns by name not index value. I needed to incorporate David Hall's answer into my demo that already had ComboBoxes so his answer worked really well.

提交回复
热议问题