我尝试让用户直接在DataGridView中更改数据 此DataGridView从数据库的DataTable获取数据源
我正在使用DataError事件来尝试帮助用户正确写入数据。
我的错误流应为
1)用户写入数据 2)用户在应该为Int的单元格上写'a' 3)我告诉用户他在某些单元上错了 4)当他遇到错误时,我再次将用户聚焦在同一单元格上 5)我不会删除该行,也不会删除该行的值,我也不会让用户从焦点移到此单元格和行上。
实际上,在使用事件之后,我没有找到如何将焦点重新回到单元格的方法 此代码对我不起作用... 私有void dataGridViewDefinePath_DataError(对象发送者,
DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("הייתה בעייה בהכנסת אחד הנתונים");
((DataGridView)sender).CurrentCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
((DataGridView)sender).BeginEdit(true);
e.Cancel = false;
}
在此错误之后,我的行也被删除。
这是我的代码:
using PathDefiner;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UIPath
{
public partial class DefineForm : Form
{
private Action<string, DataTable> SaveAction { get; set; }
private Func<string, DataTable> SetAction { get; set; }
private string currentCatalog;
public DefineForm(string catalog, Action<string, DataTable> saveAction, Func<string, DataTable> setAction)
{
InitializeComponent();
SaveAction = saveAction;
SetAction = setAction;
currentCatalog = Globals.CurrentCatalog = catalog;
dataGridViewDefinePath.DataSource = SetAction(currentCatalog);
DataTable dt = dataGridViewDefinePath.DataSource as DataTable;
dt.Columns[0].ReadOnly = true;
}
private void dataGridViewPathDefine_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["BatteryCatalog"].Value = Globals.CurrentCatalog;
}
private void buttonExit_Click(object sender, EventArgs e)
{
Close();
}
private void buttonSaveAndExit_Click(object sender, EventArgs e)
{
DataTable dt = dataGridViewDefinePath.DataSource as DataTable;
dt.Columns[0].ReadOnly = false;
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["BatteryCatalog"] = Globals.CurrentCatalog;
}
dt.Columns[0].ReadOnly = true;
SaveAction(Globals.CurrentCatalog, dt);
Close();
}
private void dataGridViewDefinePath_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("הייתה בעייה בהכנסת אחד הנתונים");
((DataGridView)sender).CurrentCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
((DataGridView)sender).BeginEdit(true);
e.Cancel = false;
}
private void dataGridViewDefinePath_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
}
}
}