通过DataGridView数据填充文本框

时间:2019-03-20 17:16:44

标签: vb.net winforms

DataGridView1将由1行或2行或3行填充..最多5行..我有4列 我想要当我单击Button1时,TextBox从1到20将由DataGridView1上显示的数据填充 第一行将填充文本框从1到4 从5到8的文本框将由第二行填充 从9到12的文本框将由第三行填充 从13到16的文本框将由第四行填充 第五行将填充从17到20的文本框 例如..如果我在DataGridView1上只有3行,那么在这种情况下,从13到20的文本框将为空。 当填充5行DataGridView1时,我不会有问题,但是当我在DataGridView1上只有4或3或2或1行时,我会遇到问题,并且它不起作用。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles       Button1.Click
If DataGridView1.Rows.Count = 0 Then
TextBox1.Text = DataGridView1.Rows.Item(0).Cells(0).Value.ToString()
TextBox2.Text = DataGridView1.Rows.Item(0).Cells(1).Value.ToString()
TextBox3.Text = DataGridView1.Rows.Item(0).Cells(2).Value.ToString()
        TextBox4.Text = DataGridView1.Rows.Item(0).Cells(3).Value.ToString()
    ElseIf DataGridView1.Rows.Count = 1 Then
        TextBox5.Text = DataGridView1.Rows.Item(1).Cells(0).Value.ToString()
        TextBox6.Text = DataGridView1.Rows.Item(1).Cells(1).Value.ToString()
        TextBox7.Text = DataGridView1.Rows.Item(1).Cells(2).Value.ToString()
        TextBox8.Text = DataGridView1.Rows.Item(1).Cells(3).Value.ToString()
    ElseIf DataGridView1.Rows.Count = 2 Then
        TextBox9.Text = DataGridView1.Rows.Item(2).Cells(0).Value.ToString()
        TextBox10.Text = DataGridView1.Rows.Item(2).Cells(1).Value.ToString()
        TextBox11.Text = DataGridView1.Rows.Item(2).Cells(2).Value.ToString()
        TextBox12.Text = DataGridView1.Rows.Item(2).Cells(3).Value.ToString()
    ElseIf DataGridView1.Rows.Count = 3 Then
        TextBox13.Text = DataGridView1.Rows.Item(3).Cells(0).Value.ToString()
        TextBox14.Text = DataGridView1.Rows.Item(3).Cells(1).Value.ToString()
        TextBox15.Text = DataGridView1.Rows.Item(3).Cells(2).Value.ToString()
        TextBox16.Text = DataGridView1.Rows.Item(3).Cells(3).Value.ToString()
    ElseIf DataGridView1.Rows.Count = 4 Then
        TextBox17.Text = DataGridView1.Rows.Item(4).Cells(0).Value.ToString()
        TextBox18.Text = DataGridView1.Rows.Item(4).Cells(1).Value.ToString()
        TextBox19.Text = DataGridView1.Rows.Item(4).Cells(2).Value.ToString()
 TextBox20.Text =DataGridView1.Rows.Item(4).Cells(3).Value.ToString()
End If
End Sub

1 个答案:

答案 0 :(得分:0)

我会这样写代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If DataGridView1.Rows.Count > 4 Then
        TextBox17.Text = DataGridView1.Rows.Item(4).Cells(0).Value.ToString()
        TextBox18.Text = DataGridView1.Rows.Item(4).Cells(1).Value.ToString()
        TextBox19.Text = DataGridView1.Rows.Item(4).Cells(2).Value.ToString()
        TextBox20.Text = DataGridView1.Rows.Item(4).Cells(3).Value.ToString()
    End If
    If DataGridView1.Rows.Count > 3 Then
        TextBox13.Text = DataGridView1.Rows.Item(3).Cells(0).Value.ToString()
        TextBox14.Text = DataGridView1.Rows.Item(3).Cells(1).Value.ToString()
        TextBox15.Text = DataGridView1.Rows.Item(3).Cells(2).Value.ToString()
        TextBox16.Text = DataGridView1.Rows.Item(3).Cells(3).Value.ToString()
    End If
    If DataGridView1.Rows.Count > 2 Then
        TextBox9.Text = DataGridView1.Rows.Item(2).Cells(0).Value.ToString()
        TextBox10.Text = DataGridView1.Rows.Item(2).Cells(1).Value.ToString()
        TextBox11.Text = DataGridView1.Rows.Item(2).Cells(2).Value.ToString()
        TextBox12.Text = DataGridView1.Rows.Item(2).Cells(3).Value.ToString()
    End If
    If DataGridView1.Rows.Count > 1 Then
        TextBox5.Text = DataGridView1.Rows.Item(1).Cells(0).Value.ToString()
        TextBox6.Text = DataGridView1.Rows.Item(1).Cells(1).Value.ToString()
        TextBox7.Text = DataGridView1.Rows.Item(1).Cells(2).Value.ToString()
        TextBox8.Text = DataGridView1.Rows.Item(1).Cells(3).Value.ToString()
    End If
    If DataGridView1.Rows.Count > 0 Then
        TextBox1.Text = DataGridView1.Rows.Item(0).Cells(0).Value.ToString()
        TextBox2.Text = DataGridView1.Rows.Item(0).Cells(1).Value.ToString()
        TextBox3.Text = DataGridView1.Rows.Item(0).Cells(2).Value.ToString()
        TextBox4.Text = DataGridView1.Rows.Item(0).Cells(3).Value.ToString()
    End If
End Sub


Public Class TestItem
    Public Property Prop1 As String
    Public Property Prop2 As String
    Public Property Prop3 As String
    Public Property Prop4 As String

    Public Sub New(P1 As String, P2 As String, P3 As String, P4 As String)
        Prop1 = P1
        Prop2 = P2
        Prop3 = P3
        Prop4 = P4
    End Sub
End Class

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim source As New List(Of TestItem)
    source.Add(New TestItem("a", "b", "c", "d"))
    source.Add(New TestItem("e", "f", "g", "h"))
    source.Add(New TestItem("i", "j", "k", "l"))
    source.Add(New TestItem("m", "n", "o", "p"))
    'source.Add(New TestItem("q", "r", "s", "t"))

    DataGridView1.DataSource = source
End Sub

请不要将Count'集合Rows的{​​{1}}属性误认为是基于0的。在您的代码中,您正在检查是否Index,然后访问DataGridView1.Rows.Count = 0。如果DataGridView1.Rows.Item(0)为0,则Rows.Count不存在,因为它将是第一行,但是由于行数为0,因此网格中没有行。