1

我有一个使用 BindingNavigator 的数据应用程序。我已将导航器连接到绑定源,但它不起作用。它不会添加或删除行。绑定源为:accountBindingSource。我不明白出了什么问题。我有以下代码:

public partial class AccountDataGridView : Form
{
    public AccountDataGridView()
    {
        InitializeComponent();
        Setup();
    }

    private void AccountDataGridView_Load(object sender, EventArgs e)
    {
        // Change the back color of the first column. This can also be changed in the designer
        accountGridView.Columns[0].DefaultCellStyle.BackColor = Color.FromArgb(192, 192, 255);
    }

    private void Setup()
    {
        // Define a global variable for the data table
        Account = new DataTable(Text);
        query = string.Format("SELECT * FROM {0}", Text);
        // Establish a connection between the Database and the form
        conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Tutoring Database.accdb;Persist Security Info=False");
        conn.Open();
        // Setup data table
        OleDbDataAdapter accountAdapter = new OleDbDataAdapter(query, conn);
        if (accountAdapter != null)
        {
            accountAdapter.Fill(Account);
        }
        accountGridView.DataSource = Account;

        conn.Close();
    }

    private void DataErrorRaised(object sender, DataGridViewDataErrorEventArgs e)
    {
        // Data table error handling. This is triggered when the user attempts to input invalid data into the CurrentBalance column
        MessageBox.Show("You have entered an invalid data type for the currency column. Please enter text formatted like so: '$0.00'",
            "Account Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        // Update Access Database
        try
        {
            adapter.SelectCommand = new OleDbCommand(query, conn);
            adapter.InsertCommand = new OleDbCommand(query, conn);
            adapter.DeleteCommand = new OleDbCommand(query, conn);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

            adapter.Update(Account);
            Console.WriteLine("Saved");
        }

        catch
        {

        }
    }

    private DataTable Account;
    private string query;
    private OleDbConnection conn;
    private OleDbDataAdapter adapter = new OleDbDataAdapter();

    private void accountGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        btnSave_Click(null, null);
    }
}
4

3 回答 3

2

在您的问题中,您将数据分配给accountGridView.DataSource. 所以你不能指望绑定导航器工作。未BindingSource连接到数据 并且BindingNavigatorDataGridView连接到BindingSource.

您应该使用设计器或代码执行这些设置:

  • 加载数据并将数据分配给 的DataSource属性BindingSource。(使用代码)
  • 分配BindingSourceDataSource您的财产DataGridView
  • 分配BindingSource给 的BindingSource属性BindingNavigator

笔记

更改查询与绑定源无关。无论适配器或任何提供/保存数据的东西如何,andBindingSource都可以工作。BindingNavigator既不BindingSource也不BindingNavigator知道数据实际来自何处以及如何保存数据。

于 2016-12-10T05:31:04.557 回答
0

我刚刚通过创建一个新的 BindingSource 并通过代码设置其属性来解决我的问题。为此,只需将 BindingSource 拖到表单上。随意命名。在解决方案的代码部分中键入与此类似的内容(在构造函数中)。

BindingSource.DataSource = Account;

确保将 BindingNavigator 的 BindingSource 设置为刚刚创建的 BindingSource。谢谢大家的帮助!

编辑:我不知道这是否与我的解决方案有关,但我也稍微编辑了我的查询。此代码是项目正常运行所必需的。

try
{
    adapter.SelectCommand = new OleDbCommand(query, conn);
    adapter.InsertCommand = new OleDbCommand("INSERT INTO Account (AccountNumber, LastName, FirstName, CurrentBalance) " +
    "VALUES (?, ?, ?, ?)", conn);
    adapter.DeleteCommand = new OleDbCommand(query, conn);
    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

    adapter.Update(Account);
    Console.WriteLine("Saved");
}
catch
{
}
于 2016-12-10T04:50:40.313 回答
0

为后代:

BindingNavigator 的按钮是轻量级控件,与真正的 Button 控件不同。

当您单击 BindingNavigator 中的任何按钮时,焦点不会从当前控件中被盗,因此导致数据被推送到底层数据库的事件序列永远不会被调用。

我知道我有一个示例,您必须将几行代码放入每个轻量级按钮的 Click 处理程序中,但现在找不到。

这一直是皮塔饼!

于 2020-04-09T09:38:43.403 回答