【发布时间】:2021-02-24 14:56:35
【问题描述】:
我想将同一列中的所有 DataGridView 行绑定到一个文本框,以便在我单击该列中的单元格时使该单元格的文本出现在该文本框中。问题是我不能使用 e.RowIndex。有没有办法用属性或代码做到这一点?
【问题讨论】:
标签: c# database ms-access datagridview textbox
我想将同一列中的所有 DataGridView 行绑定到一个文本框,以便在我单击该列中的单元格时使该单元格的文本出现在该文本框中。问题是我不能使用 e.RowIndex。有没有办法用属性或代码做到这一点?
【问题讨论】:
标签: c# database ms-access datagridview textbox
您可以使用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();
}
}
【讨论】: