我想从Datagridview获取数据到文本框但我希望我选择的每一行都应显示在多个文本框中?我这样做但它只选择一行并显示在一个文本框中,但我想将多行数据传输到多个文本框。
private void dataGridView_1CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
// to set oem no to textfield particular.
dataGridView1.Refresh();
try {
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Celles["OEM_No"].Value.ToString();
}
catch (Exception ex) {
MessageBox.Show(es.Message);
}
}
答案 0 :(得分:0)
嗯,这应该有效(没有彻底测试):
VB:
Dim TBlist As List(Of TextBox)
TBlist.Add(Me.TextBox1)
TBlist.Add(Me.TextBox2)
TBlist.Add(Me.TextBox3)
TBlist.Add(Me.TextBox4)
TBlist.Add(Me.TextBox5)
Dim mLimit As Int16 = Math.Min(Me.dgv.SelectedRows.Count - 1, TBlist.Count - 1)
For ir = 0 To mLimit
Dim tb As TextBox = DirectCast(TBlist.Item(ir), TextBox)
tb.Text = Me.dgv.SelectedRows(ir).cells("RequiredCellName").value
Next
转换后的C#:
List<TextBox> TBlist = default(List<TextBox>);
TBlist.Add(this.TextBox1);
TBlist.Add(this.TextBox2);
TBlist.Add(this.TextBox3);
TBlist.Add(this.TextBox4);
TBlist.Add(this.TextBox5);
Int16 mLimit = Math.Min(this.dgv.SelectedRows.Count - 1, TBlist.Count - 1);
for (ir = 0; ir <= mLimit; ir++) {
TextBox tb = (TextBox)TBlist.Item(ir);
tb.Text = this.dgv.SelectedRows(ir).cells("RequiredCellName").value;
}
它将使用插入到文本框列表中的文本框尽可能多的文本框,或者使用与所选内容一样多的文本框,这些文本框的数量可能更少。