Datagridview:如何在编辑模式下设置单元格?

我需要以编辑方式以编辑方式设置单元格。 我知道将该单元设置为CurrentCell然后调用方法BeginEdit(bool),它应该发生,但在我的情况下,它不会。

我真的很想要,我的DGV有几个列,用户只能选择并编辑前两个。 其他列已经是只读的,但用户可以选择它们,这就是我不想要的。

所以我在想,每当它完成在单元格上写入时告诉用户TAB,然后选择第二个单元格,然后再次选项卡并选择并开始编辑下一行的第一个单元格……

我怎样才能做到这一点?

设置CurrentCell然后调用BeginEdit(true)对我来说效果很好。

以下代码显示了KeyDown事件的eventHandler,该事件将单元格设置为可编辑。

我的例子只实现了一个必要的按键覆盖,但理论上其他的应该是相同的。 (我总是将[0] [0]单元格设置为可编辑,但任何其他单元格都可以工作)

  private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1) { e.Handled = true; DataGridViewCell cell = dataGridView1.Rows[0].Cells[0]; dataGridView1.CurrentCell = cell; dataGridView1.BeginEdit(true); } } 

如果您以前没有找到它, DataGridView FAQ是一个很好的资源,由DataGridView控件的程序管理器编写,它涵盖了您可能想要对控件执行的大部分操作。

 private void DgvRoomInformation_CellEnter(object sender, DataGridViewCellEventArgs e) { if (DgvRoomInformation.CurrentCell.ColumnIndex == 4) //example-'Column index=4' { DgvRoomInformation.BeginEdit(true); } } 

好吧,我会检查你的任何列是否设置为ReadOnly 。 我从来没有使用过BeginEdit,但也许有一些合法用途。 完成dataGridView1.Columns[".."].ReadOnly = False; ,不是ReadOnly的字段应该是可编辑的。 您可以使用DataGridView CellEnter事件来确定输入的单元格,然后在从前两列到下一组列的编辑通过后关闭这些单元格上的编辑,并关闭最后两列的编辑。

我知道这个问题很老了,但我想我会分享一些这个问题对我有帮助的演示代码。

  • 使用ButtonDataGridView创建一个表单
  • 为button1注册Click事件
  • 为DataGridView1注册CellClick事件
  • 将DataGridView1的属性EditMode设置为EditProgrammatically
  • 将以下代码粘贴到Form 1中:
 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); } } } } } 

请注意,列索引号可以从按钮1的多个按钮按下更改,因此我总是按名称引用列而不是索引值。 我需要将David Hall的答案整合到我已经拥有ComboBoxes的演示中,这样他的答案才能很好地完成。

我知道这是一个老问题,但没有一个答案对我有用,因为我想在可能执行其他事件(如工具栏按钮点击,菜单选择等)时可靠(始终能够)将单元设置为编辑模式这些事件返回后可能会影响默认焦点。 我最终需要一个计时器并调用。 以下代码位于从DataGridView派生的新组件中。 这段代码允许我简单地调用myXDataGridView.CurrentRow_SelectCellFocus(myDataPropertyName); 我想随意将数据绑定单元格设置为编辑模式(假设单元格不处于ReadOnly模式)。

 // If the DGV does not have Focus prior to a toolbar button Click, // then the toolbar button will have focus after its Click event handler returns. // To reliably set focus to the DGV, we need to time it to happen After event handler procedure returns. private string m_SelectCellFocus_DataPropertyName = ""; private System.Timers.Timer timer_CellFocus = null; public void CurrentRow_SelectCellFocus(string sDataPropertyName) { // This procedure is called by a Toolbar Button's Click Event to select and set focus to a Cell in the DGV's Current Row. m_SelectCellFocus_DataPropertyName = sDataPropertyName; timer_CellFocus = new System.Timers.Timer(10); timer_CellFocus.Elapsed += TimerElapsed_CurrentRowSelectCellFocus; timer_CellFocus.Start(); } void TimerElapsed_CurrentRowSelectCellFocus(object sender, System.Timers.ElapsedEventArgs e) { timer_CellFocus.Stop(); timer_CellFocus.Elapsed -= TimerElapsed_CurrentRowSelectCellFocus; timer_CellFocus.Dispose(); // We have to Invoke the method to avoid raising a threading error this.Invoke((MethodInvoker)delegate { Select_Cell(m_SelectCellFocus_DataPropertyName); }); } private void Select_Cell(string sDataPropertyName) { /// When the Edit Mode is Enabled, set the initial cell to the Description foreach (DataGridViewCell dgvc in this.SelectedCells) { // Clear previously selected cells dgvc.Selected = false; } foreach (DataGridViewCell dgvc in this.CurrentRow.Cells) { // Select the Cell by its DataPropertyName if (dgvc.OwningColumn.DataPropertyName == sDataPropertyName) { this.CurrentCell = dgvc; dgvc.Selected = true; this.Focus(); return; } } }