2015-10-16 88 views
1

所以我打算創建一個電話簿,當我雙擊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"); 
} 
+0

你永遠不會調用'AddDataBinding'。更好地學習在sql查詢中使用參數以避免sql注入。 – LarsTech

+0

** - **在您的'CellContentDoubleClick'中,您傳遞'RowIndex'而不是'Id'。 ** - **在你的詳細表單中,你沒有調用'AddDataBinding' ** - **考慮使用參數化查詢。 ** - **考慮使用設計器來執行此類數據綁定 –

+0

傳遞選定員工ID的正確語法是什麼?謝謝 – elowearth

回答

2

1-你應該找到ID而不是行索引。你應該知道這列索引是ID,例如,如果你的ID是第一列,您可以使用此:

var id = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value; 

2-考慮使用參數化查詢是這樣的:

cmd.CommandText = "SELECT * FROM Employee WHERE EmployeeID [email protected]"; 
cmd.Parameters.AddWithValue("@Id", id); 
//cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id; 

3-執行數據綁定是Ivan說話的方式,並呼籲AddDataBinding

DataTable dt; 

private void GetRecords(int n) 
{ 
    //... 
    da.Fill(ds, "Employee"); 
    dt = ds.Tables["Employee"]; 
    AddDataBinding(); 
} 

private void AddDataBinding() 
{ 
    txtLN.DataBindings.Add("Text", dt, "LastName"); 
    txtFN.DataBindings.Add("Text", dt, "FirstName"); 
    // ... 
} 
+0

如果用戶選擇了一個隨機行,該怎麼辦?我怎麼能得到所選行的主鍵(id)? – elowearth

+0

@elowearth正如我所說的,你可以使用'var id =(int)dataGridView1.Rows [e.RowIndex] .Cells [0] .Value;'而不是0,使用'Id'列的索引。注意'e.RowIndex'指向用戶雙擊的行。您應該將該代碼放入您編寫的'dataGridView1_CellContentDoubleClick'中。 –

+0

@elowearth順便說一下,在StackOverFlow中,當您找到有用的答案時,您可以點擊答案附近的複選標記以使其被接受。這樣它對其他用戶會更有用。您只能接受一個答案,但是當您獲得聲望時,您可以投出儘可能多的答案,因爲您可以通過點擊答案附近的向上箭頭來發現包括已接受方案在內的有用答案。 –

1

首先,下面的代碼id = dataGridView1.CurrentCell.RowIndex;氣味,請確保你真的是得到emplyeee Id值。

其次,WinForms數據綁定不像WPF那樣支持「路徑」(如「Employee.LastName」)。爲了實現自己的目標,你需要綁定到DataTable代替,就像這樣:

DataTable dt; 

private void GetRecords(int n) 
{ 
    //... 
    da.Fill(ds, "Employee"); 
    dt = ds.Tables["Employee"]; 
} 

private void AddDataBinding() 
{ 
    txtLN.DataBindings.Add("Text", dt, "LastName"); 
    txtFN.DataBindings.Add("Text", dt, "FirstName"); 
    // ... 
}