在C#中显示从gridview到多个文本框的多个选定行 [英] Display Multiple selected rows from gridview to mutiple textboxes in c#

查看:87
本文介绍了在C#中显示从gridview到多个文本框的多个选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从Datagridview获取到文本框,但我希望我选择的每一行都应显示在多个文本框中?我正在这样做,但是它只选择一行并在一个文本框中显示,但是我想将多个行的数据发送到多个文本框。

I want to get data from Datagridview to text boxes but i want every row i select should be show in multiple text boxes? I am doing it but it is only select one row and show in one text box but i want to get data of multiple rows to multiple Textboxes.

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


推荐答案

,这应该可以正常工作(未经彻底测试):

Well, this should work (not tested thoroughly):

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

它将使用与插入到文本框列表中一样多的文本框,或者选择的行数多了,就越少。

It will use as many textboxes as inserted to the list of textboxes, or as many rows as selected, which ever is less.

这篇关于在C#中显示从gridview到多个文本框的多个选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆