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);
}
}
WinForm 바인딩을 수행 한 이후로 오랜 시간이 걸렸지 만 올바르게 작성된 삽입 쿼리/문 대신 .InsertCommand에 대해 동일한 선택 쿼리를 전달하고 있다고 생각합니다. – AaronLS
어떤 유형의 쿼리를 사용해야하는지 알고 있습니까? – Akinni
확실하지 않습니다. 이 예제에서는 예제 쿼리에서 InsertCommand 속성을 사용하지만 이는 그리드 뷰가 아닙니다. 그것은 폼 매개 변수가 쿼리에 전달되는 방식에 달려 있습니다. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand(v=vs.110).aspx – AaronLS