编辑:我使用绑定到下面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);
}
}
DataHandler.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;
}
答案 0 :(得分:1)
我知道这篇文章已经过时了,但是对于那些偶然发现这个帖子的人来说。 我相信您的DataEdit在Gridview中,您正在编辑该单元格,除非您在DataGridView上调用endit方法,否则不会将其传播到binidngsource。
看这里EndEdit on BindingSource updates DataTable, but rowstate still unchanged
和How to properly commit bindingSource changes to source DB?
您也可以查看一下 How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource