gpt4 book ai didi

c# - 如何自动结束 DataGridView 单元格编辑模式?

转载 作者:太空宇宙 更新时间:2023-11-03 14:08:08 28 4
gpt4 key购买 nike

这是我上一个问题的后续问题。你可以找到它here .

在最终解决了我的前一个问题后,我遇到了另一个问题,当我分配按钮以将值添加到 DataGrid 中的新行时,整个单元格将处于编辑模式,直到我单击其他单元格并填充它和/或制表符直到行尾(显然这个行不通),然后它将结束编辑模式。我正在使用 dataGridView.BeginEdit(true); 开始编辑模式,这样我就可以将一个值解析到文本框中(请参阅我之前的问题)。因此,如果我插入另一个值并按下按钮,新值将替换之前插入的旧值,因为它当前仍处于编辑模式。我尝试使用 dataGridView.EndEdit();dataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);cell.DataGridView.EndEdit()cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit); 但显然这并没有结束编辑模式 :(我想要的是当我按下按钮时,文本框中的值将被插入到第一个文本框列中(这个已经工作了)。然后我不必单击或填充其他列来结束编辑模式。所以,我只需在文本框中输入任何内容,然后按下按钮直到我想停止。之后我开始填写另一栏。有谁知道如何解决这个问题? sample image编辑 1:您看到区别了吗?查看红色圆圈,最上面的那个当前处于编辑模式(因为它在箭头后面有一个 *)。底部的未处于编辑模式(我通过从组合框中选择一个项目手动完成)。这是我在上一个问题中要求的代码:

private void button1_Click(object sender, EventArgs e)
{
this.surat_jalanDataGridView.AllowUserToAddRows = true;
string tokNum = this.textBox1.Text;

if (this.textBox1.Text != "")
{
foreach (DataGridViewRow sjRow in this.surat_jalanDataGridView.Rows)
{
int RowIndex = surat_jalanDataGridView.RowCount - 1;
DataGridViewRow R = surat_jalanDataGridView.Rows[RowIndex];

DataTable table = new DataTable();
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);

DataGridViewCell cell = R.Cells[2];
this.surat_jalanDataGridView.CurrentCell = cell;
this.surat_jalanDataGridView.BeginEdit(true);

R.Cells[2].Value = tokNum;
cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
}
}
this.surat_jalanDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
}

编辑 2:因此,我将 surat_jalan 从数据源拖放到我的窗口窗体中。然后它自动成为一个数据网格,属性名称为surat_jalanDataGridView,数据源为surat_jalanBindingSource img 2

最佳答案

抱歉耽搁了。在了解如何设置与 DataGridView 的绑定(bind)后,我肯定可以为您提供有关如何编辑网格绑定(bind)到的数据的更好指导。当您将表从 Visual Studio 的数据源 View 中拖放到 DataGridView 上时,Visual Studio 会为您做几件事。重要的是,您至少要了解所做工作的基础知识,这样您才能了解如何操作您的数据向前发展。 ThisMSDN 文章 how to setup binding to windows form controls from Visual Studio。最后一部分描述了你在做什么。最后一句话 “DataGridView 控件现在绑定(bind)到您拖到它上面的表格。DataSet、TableAdapter 和 BindingSource 出现在组件托盘中。” 是重要的。由于 Visual Studio 生成了将控件绑定(bind)到表的代码,因此您应该直接编辑数据以更新数据 GridView 。在这种情况下,您应该使用生成的数据集(我假设它被命名为 surat_jalanDataSet)。 Here描述了如何编辑 DataSet 中的数据。针对您的具体案例 Adding Rows .如果这有助于您实现目标,请告诉我。

关于c# - 如何自动结束 DataGridView 单元格编辑模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8736678/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com