【问题标题】:Bind a textbox to a datagridview row将文本框绑定到 datagridview 行
【发布时间】:2021-02-24 14:56:35
【问题描述】:

我想将同一列中的所有 DataGridView 行绑定到一个文本框,以便在我单击该列中的单元格时使该单元格的文本出现在该文本框中。问题是我不能使用 e.RowIndex。有没有办法用属性或代码做到这一点?

【问题讨论】:

    标签: c# database ms-access datagridview textbox


    【解决方案1】:

    您可以使用TextBoxes DataBindings 属性。比如……

    textBox1.DataBindings.Add(new Binding("Text", GridDataSource, "ColumnOrPropertyName"));
    

    如果网格没有数据源,那么您可以连接网格SelectionChanged 事件并引用网格CurrentRow 属性。比如……

    private void dataGridView1_SelectionChanged(object sender, EventArgs e) {
      if (dataGridView1.CurrentRow != null) {
        textBox1.Text = dataGridView1.CurrentRow.Cells["ColumnName"].Value.ToString();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 2022-08-02
      • 1970-01-01
      • 2012-05-09
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      相关资源
      最近更新 更多