Asp.net 不使用绑定方法将网格数据从数据库加载到gridview

Asp.net 不使用绑定方法将网格数据从数据库加载到gridview,asp.net,vb.net,sql-server-2008,Asp.net,Vb.net,Sql Server 2008,我正在尝试从mssql服务器数据库获取信息,并希望在gridview上显示它。但问题是,我在运行时出错,因为索引超出范围。必须为非负数且小于集合的大小。 参数名称:index,我的代码为 img_green = ("~\Icons\" & "circle_green.ico") img_orange = ("~\Icons\" & "circle_orange.ico") img_red = ("~\Icons\" & "circle_red.ic

我正在尝试从mssql服务器数据库获取信息,并希望在gridview上显示它。但问题是,我在运行时出错,因为索引超出范围。必须为非负数且小于集合的大小。 参数名称:index,我的代码为

    img_green = ("~\Icons\" & "circle_green.ico")
    img_orange = ("~\Icons\" & "circle_orange.ico")
    img_red = ("~\Icons\" & "circle_red.ico")

    GridView1.AllowPaging = True
    GridView1.PageSize = 10
    cmd = New SqlCommand("SELECT  a.curr_datetime, a.site_id, b.site_name, a.dc_volt, a.curr_temp, a.eb_val, a.dc_low, a.hrt_temp, a.curr_dfs, a.curr_dft, a.curr_llop, a.curr_dgonl, a.fa_alarm, a.door_open, a.curr_spare FROM final_test a INNER JOIN site_details b ON a.site_id = b.site_id;", conn)

    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If

    da.SelectCommand = cmd
    da.Fill(ds, "final_test")
    dt = ds.Tables("final_test")

    dr = cmd.ExecuteReader

    If dr.Read() Then

        GridView1.Rows(index_flag).Cells(0).Text = ds.Tables(0).Rows(0).Item("curr_datetime").ToString


        lcl_ebval = ds.Tables(0).Rows(0).Item("eb_val").ToString
        If lcl_ebval = 0 Then
            eb_img = img_green
        ElseIf lcl_ebval = 1 Then
            eb_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(5).Text = ds.Tables(0).Rows(0).Item("eb_img").ToString


        lcl_dclow = ds.Tables(0).Rows(0).Item("dc_low").ToString
        If lcl_dclow = 0 Then
            dclow_img = img_green
        ElseIf lcl_dclow = 1 Then
            dclow_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(6).Text = ds.Tables(0).Rows(0).Item("dclow_img").ToString

        lcl_hrt = ds.Tables(0).Rows(0).Item("hrt_temp").ToString
        If lcl_hrt = 0 Then
            hrt_img = img_green
        ElseIf lcl_hrt = 1 Then
            hrt_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(7).Text = ds.Tables(0).Rows(0).Item("hrt_img").ToString

        lcl_dfs = ds.Tables(0).Rows(0).Item("curr_dfs").ToString
        If lcl_dfs = 0 Then
            dfs_img = img_green
        ElseIf lcl_dfs = 1 Then
            dfs_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(8).Text = ds.Tables(0).Rows(0).Item("dfs_img").ToString

        lcl_dft = ds.Tables(0).Rows(0).Item("curr_dft").ToString
        If lcl_dft = 0 Then
            dft_img = img_green
        Else
            dft_img = img_orange
        End If
        GridView1.Rows(index_flag).Cells(9).Text =    ds.Tables(0).Rows(0).Item("dft_img").ToString


        index_flag = index_flag + 1

我可以看到,您正在通过将每行的单元格设置为从datareader获得的值来手动填充GridView

但实际上,您可以通过以下方式填充网格:

GridView1.DataSource = dt
GridView1.DataBind()

我认为你让这件事变得比你必须做的更困难了。将整个数据读入表中,绑定数据,然后使用OnRowDataBound事件修改图像

另一种方法是创建一个字段,然后填充该字段,并让gridview显示该图像名称。因此,在绑定之前,您需要遍历所有记录,将一些字段设置为要使用的图像的名称,然后绑定它

我还注意到,在这句话中:

GridView1.Rows(index_flag).Cells(5).Text = ds.Tables(0).Rows(0).Item("eb_img").ToString
实际上有一个叫做eb_img的字段吗?您可以在上面几行中设置局部变量调用eb_img,然后尝试从表中读取它。是否要将文本设置为变量eb_img而不是表列


这只会设置文本,实际上不会显示图像。如果您想自己构建单元,您需要定义一个image类型的控件,并将image位置设置为eb_img。

我已经选中了此方法。假设数据库中有10行。但是如果dr.rows或dr.read()只运行一次,并且只在屏幕上显示结果。但我希望,若数据库中有10行,那个么循环必须运行10次,因为根据每行的值,我必须在gridview中显示带有数据的图像……您可以在网格的itemDataBound Event中设置图像,这个问题是相同的。索引超出范围。必须包含非负值如果ds.Tables.Count>0并且ds.Tables(0).Rows.Count>0,则必须检查