简体   繁体   中英

Error in Databindings in winform c#

so i plan to create a phone book when i double click the data/cell in the datagridview there will be a second form(details form) that will show all the other details of that id and the data should be appear in the textbox but it doesnt work . so here is my code. thank you!.

private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{

    int id;
    id = dataGridView1.CurrentCell.RowIndex;
    Details details = new Details(id);
    details.Show();

}

in details page

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");
}

1- You should find id instead of row index. You should know which column index is for id, for example if your id is in first column, you can use this:

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

2- Consider using parametrized queries like this:

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

3- Perform databinding the way that Ivan said and call 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");
    // ...
}

First, the following code id = dataGridView1.CurrentCell.RowIndex; smells, make sure you really are getting the emplyeee Id value.

Second, WinForms data bindings do not support "path" (like "Employee.LastName") as WPF do. In order to achieve your goal, you need to bind to DataTable instead, like this:

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");
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM