所以我打算創建一個電話簿,當我雙擊datagridview中的數據/單元格時,會有第二個窗體(細節窗體),它將顯示該窗體的所有其他詳細信息,數據應該出現在文本框中,但它不起作用。所以這裏是我的代碼。謝謝!。Winform c中的數據綁定錯誤#
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int id;
id = dataGridView1.CurrentCell.RowIndex;
Details details = new Details(id);
details.Show();
}
在詳情頁
public Details(int id)
{
InitializeComponent();
int val = id;
GetRecords(val);
}
private void GetRecords(int n)
{
SqlCommand cmd = new SqlCommand();
int id = n;
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Employee WHERE EmployeeID ='" + id + "';";
da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds, "Employee");
}
private void AddDataBinding()
{
txtLN.DataBindings.Add("Text", ds, "Employee.LastName");
txtFN.DataBindings.Add("Text", ds, "Employee.FirstName");
txtMN.DataBindings.Add("Text", ds, "Employee.MiddleName");
txtAddress.DataBindings.Add("Text", ds, "Employee.Address");
txtAge.DataBindings.Add("Text", ds, "Employee.Age");
txtLN.DataBindings.Add("Text", ds, "Employee.LastName");
txtPhone.DataBindings.Add("Text", ds, "Employee.Phone");
txtMobile.DataBindings.Add("Text", ds, "Employee.Mobile");
txtEmail.DataBindings.Add("Text", ds, "Employee.Email");
dtBirthday.DataBindings.Add("Value", ds, "Employee.Birthday");
cbType.DataBindings.Add("SelectedItem", ds, "Employee.Type");
txtName.DataBindings.Add("Text", ds, "Employee.OtherName");
txtOPhone.DataBindings.Add("Text", ds, "Employee.OtherPhone");
txtOMobile.DataBindings.Add("Text", ds, "Employee.OtherMobile");
}
你永遠不會調用'AddDataBinding'。更好地學習在sql查詢中使用參數以避免sql注入。 – LarsTech
** - **在您的'CellContentDoubleClick'中,您傳遞'RowIndex'而不是'Id'。 ** - **在你的詳細表單中,你沒有調用'AddDataBinding' ** - **考慮使用參數化查詢。 ** - **考慮使用設計器來執行此類數據綁定 –
傳遞選定員工ID的正確語法是什麼?謝謝 – elowearth