2016-12-10 2 views
1

BindingNavigator를 사용하는 데이터 응용 프로그램이 있습니다. 바인딩 소스에 네비게이터를 연결했지만 작동하지 않습니다. 행을 추가하거나 삭제하지 않습니다. 바인딩 소스는 accountBindingSource입니다. 나는 틀린 것을 이해하지 못한다.C# 바인딩 탐색기가 작동하지 않습니다.

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); 
    } 
} 
+0

WinForm 바인딩을 수행 한 이후로 오랜 시간이 걸렸지 만 올바르게 작성된 삽입 쿼리/문 대신 .InsertCommand에 대해 동일한 선택 쿼리를 전달하고 있다고 생각합니다. – AaronLS

+0

어떤 유형의 쿼리를 사용해야하는지 알고 있습니까? – Akinni

+0

확실하지 않습니다. 이 예제에서는 예제 쿼리에서 InsertCommand 속성을 사용하지만 이는 그리드 뷰가 아닙니다. 그것은 폼 매개 변수가 쿼리에 전달되는 방식에 달려 있습니다. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand(v=vs.110).aspx – AaronLS

답변

2

질문에 accountGridView.DataSource에 데이터를 지정했습니다. 따라서 바인딩 탐색기가 작동하는 것을 기대할 수 없습니다. BindingSource 데이터에 연결되지 않고 BindingNavigatorDataGridView 당신은 디자이너 또는 코드를 사용하여 이러한 설정을 수행해야 BindingSource.

에 연결되지 않은 :

  • 데이터로드 및 BindingSourceDataSource 속성에 데이터를 할당합니다. (사용 코드)
  • 지정 BindingSourceBindingNavigator 당신의 DataGridView
  • 지정 BindingSource-BindingSource 재산의 재산 DataSource합니다.

변경 쿼리 소스를 바인딩과는 아무 상관이 없습니다. BindingSourceBindingNavigator은 어댑터 또는 데이터를 제공/저장하는 것과 관계없이 작동합니다. BindingSource도 아니요 BindingNavigator도 실제로 데이터가 어디에서오고 데이터가 저장 될지에 대해 알지 못합니다.

0

난 그냥 새로운 BindingSource에를 생성하고 코드를 통해 속성을 설정하여 내 문제를 해결 : 나는 다음과 같은 코드가 있습니다. 이렇게하려면 BindingSource를 폼으로 끌어 놓기 만하면됩니다. 원하는 이름을 지정하십시오. 솔루션의 코드 부분에 이와 비슷한 (생성자에서).

BindingSource.DataSource = Account; 

방금 ​​만든 BindingSource에 BindingNavigator의 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 
{ 
} 
+0

질문에 'accountGridView.DataSource'에 데이터를 지정했습니다. 따라서 바인딩 탐색기가 작동하는 것을 기대할 수 없습니다. 'BindingSource'는 데이터에 연결되지 않고'BindingNavigator'와'DataGridView'는'BindingSource '에 연결되지 않습니다.' –

+0

변경 쿼리는 바인딩 소스와 아무 관련이 없습니다. 'BindingSource'와'BindingNavigator'는 어댑터 나 데이터를 제공/저장하는 것에 관계없이 작동합니다. BindingSource와 BindingNavigator는 실제로 데이터가 어디에서 오는 것인지, 어떻게 데이터가 저장 될 것인지를 알지 못한다. –

+0

감사합니다. 지금은 이해. 그러나 쿼리가 여전히 올바르지 않으며 지금은 거의 완료되었습니다. – Akinni

관련 문제