[英]How to set datagridview cell format when editing and after editing in C#?
[英]Datagridview: How to set a cell in editing mode?
我需要以编程方式将单元格设置为编辑模式。 我知道将该单元格设置为 CurrentCell,然后调用方法 BeginEdit(bool),它应该发生,但在我的情况下,它不会发生。
我真的很想要,我的 DGV 有几列,用户只能 select 并且还可以编辑前两列。 其他列已经是只读的,但用户可以 select 它们,这是我不想要的。
所以我在想,每次在单元格上写完时告诉用户 TAB,然后是第二个单元格 select,然后再次选择 select 并开始编辑下一行的第一个单元格...
我怎样才能做到这一点?
设置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
事件来确定输入了哪个单元格,然后在将编辑从前两列传递到下一组列并关闭对最后两列的编辑后,打开对这些单元格的编辑。
我知道这个问题已经很老了,但我想我会分享一些这个问题对我有帮助的演示代码。
Button
和DataGridView
的表单Click
事件CellClick
事件EditProgrammatically
将 DataGridView1 的属性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);
}
}
}
}
}
请注意,列索引号可能会因多次按下按钮一而发生变化,因此我总是按名称而不是索引值来引用列。 我需要将 David Hall 的答案合并到我已经有 ComboBoxes 的演示中,因此他的答案非常有效。
我知道这是一个老问题,但没有一个答案对我有用,因为我想在可能执行其他事件(如工具栏按钮单击、菜单选择等)时可靠地(始终能够)将单元格设置为编辑模式。这些事件返回后可能会影响默认焦点。 我最终需要一个计时器并调用。 以下代码位于派生自 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;
}
}
}
我终于找到了这个问题的答案。 就我而言,我想在添加新行后选择特定索引或项目,但这应该适用于其他情况。
单元格不包含组合框控件。 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
这个问题很老,但建议的解决方案对我的情况没有帮助。 加载表单时需要 select 单元格。 此选项不起作用:
private void FOperations_Load(object sender, EventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
如果在“Layout”事件中进行单元格选择,那么一切都成功了:
private void dgvOperation_Layout(object sender, LayoutEventArgs e)
{
dgvOperations.CurrentCell = dgvOperations[nameof(Operation.DisplayName), 0];
dgvOperations.Select();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.