将gridview行数据添加到文本框和标签中

时间:2014-04-10 14:06:18

标签: asp.net sql vb.net gridview

我试图将gridview详细信息放入文本框中以便更好地查看和编辑。 我列出了以下代码来创建gridview:

'Finds all cases that are not closed
    Protected Sub listAllCases()
        sqlCommand = New SqlClient.SqlCommand("SELECT TC.caseId,TS.subName,TSU.userName,TC.caseType,TC.caseRegBy,TC.caseTopic,TC.caseDesc,TC.caseSolu,TC.caseDtCreated, TC.caseStatus FROM TBL_CASE TC INNER JOIN TBL_SUBSIDIARY_USER TSU ON TC.caseUser = TSU.userID INNER JOIN TBL_SUBSIDIARY TS on TSU.usersubId = TS.subId WHERE TC.caseStatus = 0 order by caseId")
        sqlCommand.Connection = sqlConnection
        sqlConnection.Open()
        'sqlCommand.Parameters.AddWithValue("@subID", Me.caseSub.SelectedItem.Value)

        Dim dr As SqlClient.SqlDataReader
        dr = sqlCommand.ExecuteReader
        If dr.HasRows Then
            allCases.DataSource = dr
            allCases.DataBind()
        Else
            allCases.DataSource = Nothing
            allCases.DataBind()
        End If
        dr.Close()
        sqlConnection.Close()
    End Sub

然后我在gridview onselectindexchanged上使用a函数并写入:

Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
        Dim row As GridViewRow = allCases.SelectedRow
        txtcase.Text = row.Cells(1).Text()
        txtsub.Text = row.Cells(2).Text
        txtuser.Text = row.Cells(3).Text
        oDato.Text = row.Cells(9).Text
        lDato.Text = "Saken er ikke lukket!"
        txttype.Text = row.Cells(4).Text.ToString
        txtregBy.Text = row.Cells(5).Text.ToString
        txttopic.Text = row.Cells(6).Text
        txtDesc.Text = row.Cells(7).Text
        txtSolu.Text = row.Cells(8).Text
        lblinfo.Text = row.Cells(6).Text
    End Sub

我只能显示单元格1到9.意味着单元格4到8未列出或为空白,即使我知道它应该包含数据。

非常感谢任何提示或线索!

3 个答案:

答案 0 :(得分:0)

喜欢这个?

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As 
TextBox1.text = DataGridView1.Rows(e.Index).Cells(0).value.toString
TextBox2.text = DataGridView1.Rows(e.Index).Cells(1).value.toString
TextBox3.text = DataGridView1.Rows(e.Index).Cells(2).value.toString
TextBox4.text = DataGridView1.Rows(e.Index).Cells(3).value.toString
TextBox5.text = DataGridView1.Rows(e.Index).Cells(4).value.toString
TextBox6.text = DataGridView1.Rows(e.Index).Cells(5).value.toString
TextBox7.text = DataGridView1.Rows(e.Index).Cells(6).value.toString
TextBox8.text = DataGridView1.Rows(e.Index).Cells(7).value.toString
TextBox9.text = DataGridView1.Rows(e.Index).Cells(8).value.toString
TextBox10.text = DataGridView1.Rows(e.Index).Cells(9).value.toString
End Sub

这将通过单击记录...

显示行中的所有信息

答案 1 :(得分:0)

e As System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridView1.CellContentClick

抱歉...

通过单击数据(e.Index)将找到DataGridView.Rows(),然后单元格。(n)等...将获取您的数据

答案 2 :(得分:0)

确定。我终于做对了。我用来使其工作的代码如下:

Protected Sub allCases_OnSelectedIndexChanged(sender As Object, e As EventArgs)
        Dim row As GridViewRow = allCases.SelectedRow
        txtcase.Text = row.Cells(1).Text()
        txtsub.Text = row.Cells(2).Text.ToString
        txtuser.Text = row.Cells(3).Text
        oDato.Text = row.Cells(9).Text
        txtDesc.Text = TryCast(row.FindControl("lblcaseDesc"), Label).Text
        txtSolu.Text = TryCast(row.FindControl("lblcaseSolu"), Label).Text
end sub

这意味着我需要以不同方式获取boundfields和templatefields。 无论如何,谢谢你的回应。