我的单元格格式化函数如下:
private void dataGridViewCND_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
if (e.Value != null)
{
if (e.Value.ToString().Contains("S010"))
{
e.Value = "BE";
}
else if (e.Value.ToString().Contains("S011"))
{
e.Value = "BI";
}
}
}
}
当我更新时,非格式化的值被插入到我的数据库中,而不是格式化的值。
Edit :这是我在数据库中插入的代码:
private void buttonEnregistrer_Click(object sender, EventArgs e)
{
dataGridViewCND.EndEdit();
dataAdapter.Update(dataTable);
DataBind();
}
这是我的DataBind()函数
private void DataBind()
{
dataGridViewCND.DataSource = null;
dataTable.Clear();
string query = "SELECT xxxxxxxxxxxx FROM xxxxxxxxxxxx";
SqlConnection con = new SqlConnection(conStringLocal);
con.Open();
SqlCommand command = con.CreateCommand();
command.CommandText = query;
dataAdapter = new SqlDataAdapter(query, con);
commandBuilder = new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(dataTable);
bindingSource = new BindingSource { DataSource = dataTable };
dataGridViewCND.DataSource = bindingSource;
}
发布于 2018-05-31 17:55:59
在您的dataGridViewCND_CellFormatting
方法中,您实际上是在格式化将要在网格视图上显示的值。
这实际上不会改变数据源(这里是dataTable
)中的值。因此,数据源只保存未制定的数据。
因此,当您查询数据库以使用dataTable
的值更新它时,数据库也不会使用格式化数据进行更新。
在设置数据网格视图上的值的格式时,也需要不断更新dataTable
。
更新,如下所示的dataGridViewCND_CellFormatting
。
private void dataGridViewCND_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
if (e.Value != null)
{
if (e.Value.ToString().Contains("S010"))
{
dataTable.Rows[e.ColumnIndex][e.RowIndex] = "BE";
e.Value = "BE";
}
else if (e.Value.ToString().Contains("S011"))
{
dataTable.Rows[e.ColumnIndex][e.RowIndex] = "BI";
e.Value = "BI";
}
}
}
}
https://stackoverflow.com/questions/50619832
复制相似问题