我需要以编程方式设置一个单元格处于编辑模式。我知道将该单元格设置为当前单元格,然后调用BeginEdit(bool)方法,应该会发生这种情况,但在我的情况下,它并没有发生。
我真的希望,对于我的具有多个列的DGV,用户只能选择并编辑前两个列。其他列已经是只读的,但用户仍然可以选择它们,这就是我不想要的。
所以我在思考,告诉用户每次完成单元格写入后按TAB键,然后选择第二个单元格,然后再按一次tab键,它将选择并开始编辑下一行的第一个单元格...
我该怎么做?
我需要以编程方式设置一个单元格处于编辑模式。我知道将该单元格设置为当前单元格,然后调用BeginEdit(bool)方法,应该会发生这种情况,但在我的情况下,它并没有发生。
我真的希望,对于我的具有多个列的DGV,用户只能选择并编辑前两个列。其他列已经是只读的,但用户仍然可以选择它们,这就是我不想要的。
所以我在思考,告诉用户每次完成单元格写入后按TAB键,然后选择第二个单元格,然后再按一次tab键,它将选择并开始编辑下一行的第一个单元格...
我该怎么做?
CurrentCell
,然后调用BeginEdit(true)
对我很有效。KeyDown
事件的事件处理程序,用于设置单元格可编辑。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);
}
}
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
事件确定哪个单元格被输入,然后在您将编辑从前两列传递到下一组列并关闭最后两列的编辑之后,在这些单元格上打开编辑。
我知道这个问题相当古老,但我想分享一些演示代码,这个问题帮助了我。
Button
和DataGridView
的表单Click
事件CellClick
事件EditMode
设置为EditProgrammatically
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<Options> oList = new List<Options>();
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);
}
}
}
}
}
private void FOperations_Load(object sender, EventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
private void dgvOperation_Layout(object sender, LayoutEventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
我知道这是一个老问题,但是没有一个答案适用于我,因为我想要可靠地(始终能够)在可能执行其他事件(如工具栏按钮单击、菜单选择等)后将单元格设置为编辑模式,这些事件可能会影响这些事件返回后的默认焦点。最终我需要一个计时器和调用。以下代码位于从DataGridView派生的新组件中。这段代码允许我随时调用myXDataGridView.CurrentRow_SelectCellFocus(myDataPropertyName);
来任意设置数据绑定单元格为编辑模式(假设该单元格不在只读模式下)。
// 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;
}
}
}
我终于找到了答案。在我的情况下,我想要在添加新行后选择特定的索引或项,但这也适用于其他情况。
单元格本身并不包含组合框控件。DataGridView (DGV) 拥有当前单元格的控件。因此,您必须将当前单元格设置为组合框单元格,然后进入编辑模式,将 dgv 控件转换为 ComboBox,然后您就可以访问 selectedIndex 和 selectedItem 方法。
Dim rowIndex = myDgv.Rows.Add()
myDgv.ClearSelection()
myDgv.CurrentCell = myDgv.Rows(rowIndex).Cells("colName")
myDgv.BeginEdit(True)
Dim myCombo as ComboBox = CType(myDgv.EditingControl, ComboBox)
myCombo.SelectedIndex = 3