我正在研究一个程序DataGridViews.在一个中DatagridView有一个DataGridViewTextBoxColumn,它可以由用户编辑.当用户完成键入数字时,他按下键盘上的ENTER.现在它DataGridView做了所有的Events,毕竟Events,最后一件事就是问题.
一切都完成了,Windows将选择下一个DataGridViewRow,我无法阻止这一点.
我试过了
if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true; // or e.Handled
Run Code Online (Sandbox Code Playgroud)
在我发现的几乎所有事件中.可悲的是,我只能在DataGridViewTextBoxColumn未处于编辑模式时阻止ENTER键.
继续我的方法在编辑时找到了ENTER
添加事件
private void dgr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress += new KeyPressEventHandler(dgr_KeyPress_NumericTester);
}
Run Code Online (Sandbox Code Playgroud)
这是仅接受数字输入的事件.
private void dgr_KeyPress_NumericTester(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8) e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
详细解释:
当用户输入一个具有某些依赖性的Value时,我想给另一个控件提供焦点,因此他用来纠正依赖.
我也尝试过,DependingControl.Focus()但最后一次"输入"将是视图中的最后一件事.
有人知道如何防止这种情况吗?
此代码适用于使单元格的背景为蓝色:
DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate];
dgvr.Cells[colName].Style.BackColor = Color.Blue;
dgvr.Cells[colName].Style.ForeColor = Color.Yellow;
Run Code Online (Sandbox Code Playgroud)
...但ForeColor的效果并不是我所期望/希望的:字体颜色仍然是黑色,而不是黄色.
如何将字体颜色设置为黄色?
如何将 RightToLeft 属性设置为 DatagridviewCell?我试图设置
Alignment 属性为“MiddleRight”,但由于我的 DatagridviewCell 值为
阿拉伯语和英语从右到左没有按照我想要的方式显示。
我想要一个有两列的DataGridView.第一列将始终为DataGridViewComboBoxColumn类型.根据该列中的选择,我希望能够将第二列中的相应单元格更改为DataGridViewComboBoxCell或DataGridViewTextBoxCell.
我想我只需要创建DataGridViewColumn类型的第二列,但不了解如何动态更改单元格类型的机制.
我在Visual Studio 2005中使用VB.NET.
提前致谢!
更新: 我认为,绕过它的一种方法是将第二列作为DataGridViewComboBoxColumn,并更改单元格的属性,使其行为类似于下拉列表,或者作为(可编辑)下拉列表,而不是元素.后者看起来就像一个我可以忍受它的文本框,它不会涉及改变单元格的类型.
vb.net datagridview visual-studio-2005 datagridviewcomboboxcell datagridviewtextboxcell
我只需要允许将一个字符输入到可编辑的 datagridview 单元格中(每隔一列,奇数列都是可编辑的);如果用户在其中一个单元格中添加第二个字符,则光标应向下移动到下一个单元格并将第二个值放在那里(再次按下该键再次向下移动,依此类推)。如果在网格的底部(第 12 行),它应该移动到第 0 行并向右移动两列。
我尝试这样做:
private void dataGridViewPlatypus_KeyDown(object sender, KeyEventArgs e) {
var currentCell = dataGridViewPlatypus.CurrentCell;
int currentCol = currentCell.ColumnIndex;
int currentRow = currentCell.RowIndex;
if (currentCell.Value.ToString().Length > 0) {
if (currentRow < 11) {
dataGridViewPlatypus.CurrentCell.RowIndex = currentRow+1;
} else if (currentRow == 11) {
currentCell.RowIndex = 0;
currentCell.ColumnIndex = currentCell.ColumnIndex + 2;
dataGridViewPlatypus.CurrentCell = currentCell;
}
}
}
Run Code Online (Sandbox Code Playgroud)
...但我收到 RowIndex 和 ColumnIndex 无法分配的错误消息,因为它们是只读的。
那么我怎样才能做到这一点呢?
警告:我知道如果当前位于最后一个可编辑列的底部,我还必须添加逻辑以移动到第 1 列。
从 tergiver 的回答来看,这是我到目前为止所得到的,但我不知道如何前进到下一个单元格。
protected override bool ProcessCmdKey(ref Message msg, …Run Code Online (Sandbox Code Playgroud) 我有一个数据网格,其中有一列文本,我希望允许用户复制文本.我已经设置了例程来复制整个单元格或行,但是在编辑单元格和键入CTRL + C时遇到问题.
这是我用来编辑单元格的代码.进入后,我可以突出显示文本并右键单击它进行复制.这很好用,如果我突出显示文本并键入CTRL + C然后复制行,而不是突出显示的文本.
我不想创建自己的类,如果不可能,我会保持原样.
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.EditingControl == null ||
dataGridView1.CurrentCell.EditType != typeof (DataGridViewTextBoxEditingControl))
return;
dataGridView1.CancelEdit();
dataGridView1.EndEdit();
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
{
dataGridView1.BeginEdit(false);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个自定义DataGridView控件,并希望在设计器(CellStyle构建器)中设置自定义列的文本格式.
假设我想将文本格式设置为大写.在搜索了这个之后,我找到了一些添加新事件然后更改文本格式的解决方案,但这不是我想要的.我想为所有设计的列添加一个新属性,并设置或更改文本格式.
这该怎么做?
谢谢,最好的问候.
我有以下代码将以前的值输入DataGridView单元格.如果在第0行和第2列或更大,则向左的val,否则直接在上面的值:
private void dataGridViewPlatypi_CellEnter(object sender, DataGridViewCellEventArgs args)
{
// TODO: Fails if it sees nothing in the previous cell
string prevVal = string.Empty;
if (args.RowIndex > 0)
{
prevVal = dataGridViewPlatypi.Rows[args.RowIndex - 1].Cells[args.ColumnIndex].Value.ToString();
} else if (args.ColumnIndex > 1)
{
prevVal = dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex-1].Value.ToString();
}
dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex].Value = prevVal;
}
Run Code Online (Sandbox Code Playgroud)
只要有值可以查看和复制,这就很有用.如果单元格是空白的,我得到:
用户代码未处理System.NullReferenceException
消息=未将对象引用设置为对象的实例.
我猜这是一个使用null coalesce运算符的机会,但(假设我的猜测很好),我该如何实现呢?
c# datagridview nullreferenceexception winforms datagridviewtextboxcell
我有一个datagridviewcell,文本框作为它托管的控件.现在我如何在代码的其他部分以编程方式获得控件类型?
我像这样添加列:
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.ReadOnly = false;
col.Name = "Status";
col.HeaderText = "Status";
dgv.Columns.Add(col);
Run Code Online (Sandbox Code Playgroud)
该列中的所有单元格现在都有一个文本框.我可以将控件作为这样的文本框:
private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgv.CurrentCell.ColumnIndex == 5 && e.Control is TextBox)
{
//something
}
}
Run Code Online (Sandbox Code Playgroud)
如何获取其他位置的单元格中托管的控件类型?如何e.Control从代码的其他部分获取,以便我可以执行以下操作:
((TextBox)dgv[i, j].EditinControl).AutoCompleteSource = AutoCompleteSource.CustomSource;
((TextBox)dgv[i, j].EditinControl).AutoCompleteCustomSource = someSource;
((TextBox)dgv[i, j].EditinControl).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Run Code Online (Sandbox Code Playgroud)
什么可以代替EditinControl上面的那一行.. ??