C# &引用;DataGridViewCell.Value=x";做没有什么?
我试图在释放ctrl键时更改一组选定单元格的值C# &引用;DataGridViewCell.Value=x";做没有什么?,c#,windows,forms,datagridview,set,C#,Windows,Forms,Datagridview,Set,我试图在释放ctrl键时更改一组选定单元格的值 private void dataGridView1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.ControlKey) { foreach (DataGridViewCell c in dataGridView1.SelectedCells) {
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
{
foreach (DataGridViewCell c in dataGridView1.SelectedCells)
{
c.ReadOnly = false;
c.Style.BackColor = colorDialog1.Color;
c.ValueType = currentSymbol.GetType();
c.Value = currentSymbol;
dataGridView1.BeginEdit(true);
dataGridView1.EndEdit();
}
}
}
出于某种原因,“c.Value=currentSymbol;”行不起任何作用
c、 值在没有明显原因的情况下保持为空
我已经尝试了我能想到的一切,而Value成员仍然是null
我开始记得为什么我停止使用默认的dataGridView,它们是一个令人沮丧的噩梦,而且从来没有按照文档进行操作。不幸的是,我不得不为这个项目使用一个
我错过了什么?我从来没有见过一个设置命令在没有抛出任何错误的情况下简单地不服从 编辑:OP的工作方式是删除他的DataGridVew并添加一个新的DataGridVew,分配事件处理程序等 试试这个例子,它对我很有用。制作一个新的C#winforms应用程序并放置一个datagridview
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var c = new DataGridViewTextBoxCell();
c.ReadOnly = false;
this.dataGridView1.Columns.Add(new DataGridViewColumn(c));
this.dataGridView1.Rows.Add(5);
this.dataGridView1.KeyUp += new KeyEventHandler(dataGridView1_KeyUp);
}
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
var currentSymbol = "test";
if (e.KeyCode == Keys.ControlKey)
{
foreach (DataGridViewCell c in dataGridView1.SelectedCells)
{
c.ReadOnly = false;
c.Style.BackColor = Color.White;
c.Style.ForeColor = Color.Black;
c.ValueType = currentSymbol.GetType();
c.Value = currentSymbol;
dataGridView1.BeginEdit(true);
dataGridView1.EndEdit();
}
}
}
}
}
如果它也适用于您,那么在您的示例中,您一定有其他不同的做法。我看不到在您的代码中定义或初始化currentSymbol,我是否遗漏了什么?您的DataGridView数据绑定了吗?那么这可能就是代码前面定义的currentSymbol。DataGridView没有数据绑定。currentSymbol的类型是什么?currentSymbol是字符串。我甚至不能在运行时编辑单元格值。如果我输入文本并按enter键,单元格将再次变为空白。这是怎么回事?你用的是什么版本的.NET?只是试着改变版本,没有区别,单元格值根本无法编辑。即使手动输入并按enter键,文本也会消失,单元格仍为空白。好的,只是删除了整个dataGridView并添加了一个新的dataGridView,分配了事件处理程序等,现在它可以毫无理由地工作了。有一个相当致命的错误在那里的某个地方,因为我无法为我的生活,找出为什么它不允许编辑。谢谢你的帮助。好吧,奖励最佳答案,因为你的建议是正确的,VS中只存在一个bug。谢谢。更新了我的答案以反映您的实际解决方案。