如何在程序中修改datagridview中某个单元格的值
1个回答
推荐于2016-08-08
展开全部
namespace WinFormCSharp
{
public partial class Form1 : Form
{
DataGridView dv = new DataGridView();
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dv.Parent = this;
dv.Dock = DockStyle.Fill;
dv.DataError += new DataGridViewDataErrorEventHandler(dv_DataError);
dv.CellValidated +=new DataGridViewCellEventHandler(dv_CellValidated);
dt.Columns.Add("money", typeof(decimal));
dt.Rows.Add(10.2m);
dv.DataSource = dt;
dt.AcceptChanges();
}
void dv_CellValidated(object sender, DataGridViewCellEventArgs e)
{
//通过了检测,保存上一次的数值
dt.AcceptChanges();
}
void dv_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//出错时候,自动更正为默认值
MessageBox.Show("输入数据错误,自动恢复到原来数值!");
dv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dt.Rows[e.RowIndex][e.ColumnIndex, DataRowVersion.Original]; //恢复成默认数值
e.Cancel = false;
}
}
}
{
public partial class Form1 : Form
{
DataGridView dv = new DataGridView();
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dv.Parent = this;
dv.Dock = DockStyle.Fill;
dv.DataError += new DataGridViewDataErrorEventHandler(dv_DataError);
dv.CellValidated +=new DataGridViewCellEventHandler(dv_CellValidated);
dt.Columns.Add("money", typeof(decimal));
dt.Rows.Add(10.2m);
dv.DataSource = dt;
dt.AcceptChanges();
}
void dv_CellValidated(object sender, DataGridViewCellEventArgs e)
{
//通过了检测,保存上一次的数值
dt.AcceptChanges();
}
void dv_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//出错时候,自动更正为默认值
MessageBox.Show("输入数据错误,自动恢复到原来数值!");
dv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dt.Rows[e.RowIndex][e.ColumnIndex, DataRowVersion.Original]; //恢复成默认数值
e.Cancel = false;
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询