c#datagridview更改未反映在基础绑定源中
本文关键字:绑定 c#datagridview | 更新日期: 2023-09-27 18:25:27
EDIT:我使用的是绑定到下面BindingSource的DataGridView,但是当我在DataGridView中编辑单元格时,检查BindingSource.CurrentItemChanged会显示该单元格的原始值。我是否错误地使用了BindingSource?我认为重点是它将反映绑定控件所做的更改。下面列出了我的代码,如果有任何帮助,我们将不胜感激!
using System;
using System.Windows.Forms;
using System.Drawing;
public class GuiProductMaintenanceForm : Form {
private const int FORM_HEIGHT = 600;
private const int FORM_WIDTH = 800;
private BindingNavigator menu;
private DataGridView grid;
private BindingSource source;
public GuiProductMaintenanceForm() {
instComponents();
posComponents();
setProperties();
addComponents();
}
private void instComponents() {
this.source = new BindingSource(
DataHandler.getProductData(), "Products");
this.menu = new BindingNavigator(source);
this.grid = new DataGridView();
}
private void posComponents() {
int xPos = 0;
int yPos = 0;
this.menu.Location = new Point(xPos, yPos);
this.grid.Location = new Point(xPos, yPos + this.menu.Height);
}
private void setProperties() {
//controls
this.menu.Size = new Size(FORM_WIDTH, 32);
this.grid.Size = new Size(FORM_WIDTH, FORM_HEIGHT - this.menu.Height);
this.grid.BorderStyle = BorderStyle.FixedSingle;
this.grid.DataSource = source;
this.grid.AllowUserToAddRows = false;
this.grid.ReadOnly = false;
this.grid.DefaultCellStyle.Font = new Font(
FontFamily.GenericSansSerif, 12);
//this form
this.Text = "Product Maintenance";
this.ClientSize = new Size(FORM_WIDTH, FORM_HEIGHT);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
private void addComponents() {
this.Controls.Add(this.menu);
this.Controls.Add(this.grid);
}
}
GetProductData()只是返回一个包含数据的DataSet,但为了安全起见,我将包含代码。
public static DataSet getProductData() {
MySqlConnection c = makeConnection(getDefaultConnStr());
c.Open();
string selectStatement = "SELECT * FROM Products";
MySqlDataAdapter da = new MySqlDataAdapter(selectStatement, c);
DataSet ds = new DataSet();
da.Fill(ds, "Products");
return ds;
}
我知道这篇文章很旧,但对于那些偶然发现这篇文章的人来说,这里有一些东西。我相信你的DataEdit在Gridview中,你正在编辑那个单元格,除非你在DataGridView上调用endedit方法,否则它不会传播到binidngsource。
在BindingSource上的EndEdit更新DataTable,但行状态仍然没有改变
以及这里如何正确地将bindingSource更改提交到源数据库?
你也可以看看这个如何:使用BindingSource 在Windows窗体控件中反映数据源更新