【问题标题】:Error in Databindings in winform c#winform c#中的数据绑定错误
【发布时间】:2015-10-16 17:34:02
【问题描述】:

所以我计划创建一个电话簿,当我双击 datagridview 中的数据/单元格时,将出现第二个表单(详细信息表单),它将显示该 ID 的所有其他详细信息,并且数据应该出现在文本框,但它不起作用。所以这是我的代码。谢谢!

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 注入。
  • - 在您的CellContentDoubleClick 中,您传递的是RowIndex 而不是Id- 在您没有调用AddDataBinding 的详细表单中- 考虑使用参数化查询。 - 考虑使用设计器来执行这样的数据绑定
  • 传递所选员工 ID 的正确语法是什么?谢谢
  • @elowearth 如果您对答案有任何疑问,请告诉我。

标签: c# winforms visual-studio datagridview


【解决方案1】:

1- 你应该找到 id 而不是行索引。您应该知道哪个列索引是 id,例如,如果您的 id 在第一列,您可以使用:

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

2- 考虑使用这样的参数化查询:

cmd.CommandText = "SELECT * FROM Employee WHERE EmployeeID =@Id";
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");
    // ...
}

【讨论】:

  • 如果用户选择了随机行呢?如何获取该选定行的主键(id)?
  • @elowearth 正如我所说,您可以使用var id = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value; 而不是0,而是使用Id 列的索引。注意e.RowIndex 指向用户双击它的行。您应该将该代码放在您编写的dataGridView1_CellContentDoubleClick 中。
  • @elowearth 顺便说一句,在 StackOverFlow 中,当您发现有帮助的答案时,您可以单击答案附近的复选标记以使其被接受。这样,它将对其他用户更有用。您只能接受一个答案,但是当您获得声望时,您可以通过点击答案附近的向上箭头,投票给您认为有帮助的尽可能多的答案,包括已接受的答案。
  • int n; n = dataGridView1.CurrentCell.RowIndex; var id = (int)dataGridView1.Rows[e.RowIndex].Cells[n].Value; Details details = new Details(id); details.Show();} 所以我使用了这段代码,但出现错误“指定的演员表无效。”
  • @elowearth using n in (int)dataGridView1.Rows[e.RowIndex].Cells[n].Value; is wring,你应该查看你的datagridview而不是n,使用包含Id的列的索引:)这样你使用n,对于第 0 行 => (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value; 第 1 行 => (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value; 第 2 行 => (int)dataGridView1.Rows[e.RowIndex].Cells[2].Value; 和 ... 虽然它应该是例如如果 id 在你的第一列 (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
【解决方案2】:

首先,以下代码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");
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2013-06-21
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多