.NET Winforms BindingNavigator的“添加”和“删除”按钮不起作用 [英] .NET Winforms BindingNavigator Add and Delete buttons do not work

查看:346
本文介绍了.NET Winforms BindingNavigator的“添加”和“删除”按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VS2015 Winforms 应用程序上,我创建了 DataGridView 以及 BindingNavigator 。以下代码成功地在DataGridView中显示了数据,我可以使用BindingNavigator导航数据行。但是,当我尝试使用BindingNavigator上的内置添加/删除按钮添加/删除行时,数据库无法反映这些更改。



代码 >:

 公共部分类Form2:表单
{
public Form2()
{
InitializeComponent();
}
SqlDataAdapter dadapter;
DataSet dset;
BindingSource bs;
string connstring =数据库= emp;服务器= .;用户= sa;密码= wintellect;
private void Form2_Load(对象发送者,EventArgs e)
{
dadapter = new SqlDataAdapter( select * from emp_detail,connstring);
dset = new DataSet();
dadapter.Fill(dset);
bs = new BindingSource();
bs.DataSource = dset.Tables [0] .DefaultView;
bindingNavigator1.BindingSource = bs;
dataGridView1.DataSource = bs;
}
}


解决方案

您忘记将更改保存到数据库。添加,删除或编辑 DataGridView 的项目时,所有更改都在内存中的基础数据源中进行,并且要保留更改,应将这些更改保存到数据库。 / p>

您可以创建有效的 InsertCommand DeleteCommand 和<$使用SqlDataAdapter 的c $ c> UpdateCommand /system.data.sqlclient.sqlcommandbuilder(v=vs.110).aspx rel = nofollow noreferrer> SqlCommandBuilder ,然后调用 更新 适配器的方法将 DataTable 的更改保存到数据库:

  SqlDataAdapter适配器; 
DataSet表;
BindingSource bs;

private void Form1_Load(对象发送方,EventArgs e)
{
var connection = Your Connection String;
var command = SELECT * FROM SomeTable
adapter = new SqlDataAdapter(command,connstring);
this.components.Add(adapter);
table = new DataTable();

//此行为数据适配器创建有效的插入,更新和删除命令
var commandBuilder = new SqlCommandBuilder(myTableAdapter);

dadapter.Fill(table);
bs = new BindingSource();
bs.DataSource =表;
bindingNavigator1.BindingSource = bs;
dataGridView1.DataSource = bs;
}

private void SaveButton_Click(object sender,EventArgs e)
{
this.Validate();
this.dataGridView1.EndEdit();
adapter.Update(table);
}

注意:我这样做的原因 this.components.Add(adapter); 是因为我们不应忘记处置 SqlDataAdapter 。通过将其添加到表单的 components 容器中,它将自动处理。



如果您创建的表单没有设计师,您可能没有 components 容器。在这种情况下,可以通过覆盖表单的 Dispose 或在 Closing 事件中手动处置适配器。


On my VS2015 Winforms app, I created a DataGridView along with a BindingNavigator. The following code successfully displays the data in the DataGridView and I can navigate the data rows using the BindingNavigator. But when I try to add/delete a row using builtin Add/Delete buttons on the BindingNavigator the database does not reflect those changes.

The Code:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlDataAdapter dadapter;
        DataSet dset;
        BindingSource bs;
        string connstring = "database=emp;server=.;user=sa;password=wintellect";
        private void Form2_Load(object sender, EventArgs e)
        {
            dadapter = new SqlDataAdapter("select * from emp_detail", connstring);
            dset = new DataSet();
            dadapter.Fill(dset);
            bs = new BindingSource();
            bs.DataSource = dset.Tables[0].DefaultView;
            bindingNavigator1.BindingSource = bs;
            dataGridView1.DataSource = bs;
        }
    }

解决方案

You forgot to save change to database. When you add, remove or edit items of your DataGridView, all changes are made in the underlying data source in memory and to persist changes, you should save those changes to database.

You can create valid InsertCommand, DeleteCommand and UpdateCommand for the SqlDataAdapter using a SqlCommandBuilder, then call Update method of the adapter to save changes of your DataTable to database:

SqlDataAdapter adapter;
DataSet table;
BindingSource bs;

private void Form1_Load(object sender, EventArgs e)
{
    var connection = "Your Connection String";
    var command = "SELECT * FROM SomeTable"
    adapter = new SqlDataAdapter(command, connstring);
    this.components.Add(adapter);
    table= new DataTable();

    //This line creates valid insert, update and delete commands for data adapter
    var commandBuilder = new SqlCommandBuilder(myTableAdapter);

    dadapter.Fill(table);
    bs = new BindingSource();
    bs.DataSource = table;
    bindingNavigator1.BindingSource = bs;
    dataGridView1.DataSource = bs;
}

private void SaveButton_Click(object sender, EventArgs e)
{
    this.Validate();
    this.dataGridView1.EndEdit();
    adapter.Update(table);
}

Note: The reason that I did this.components.Add(adapter); is because we should not forget to dispose the SqlDataAdapter. By adding it to components container of a form it will be disposed automatically.

If you have created the form without designer, you may don't have components container. In this case, dispose the adapter manually by overriding Dispose of the form or in Closing event.

这篇关于.NET Winforms BindingNavigator的“添加”和“删除”按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆