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。(コードを使用)
  • BindingSourceあなたのDataSource財産に割り当てるDataGridView
  • BindingSourceBindingSourceプロパティに割り当てますBindingNavigator

ノート

クエリの変更はバインディング ソースとは関係ありません。BindingSourceとはBindingNavigator、アダプターやデータを提供/保存するものに関係なく機能します。どちらBindingSourceBindingNavigator、データが実際にどこから来て、どのようにデータが保存されるかについて認識していません。

于 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 に答える