从数据基础负荷电网数据GridView控件不使用绑定的方法 [英] Load grid data from data base to gridview without using Binding method

查看:130
本文介绍了从数据基础负荷电网数据GridView控件不使用绑定的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来从MSSQL Server数据库信息和要显示它的GridView。但问题是,当我运行它的索引超出范围,我得到一个错误。必须是非负并小于集合的大小。
参数名称:索引
我的code是

  img_green =(〜\\图标\\&放大器;circle_green.ico)
    img_orange =(〜\\图标\\&放大器;circle_orange.ico)
    img_red =(〜\\图标\\&放大器;circle_red.ico)    GridView1.AllowPaging = TRUE
    GridView1.PageSize = 10
    CMD =新的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_t​​est一个INNER JOIN site_details b关于a.site_id = b.site_id;,康恩)    如果conn.State = ConnectionState.Closed然后
        conn.Open()
    万一    da.SelectCommand = CMD
    da.Fill(DS,final_t​​est)
    DT = ds.Tables(final_t​​est)    博士= cmd.ExecuteReader    如果dr.Read()然后        GridView1.Rows(index_flag).Cells(0)。文本= ds.Tables(0).Rows(0).Item(curr_datetime)。的ToString
        lcl_ebval = ds.Tables(0).Rows(0).Item(eb_val)。的ToString
        如果lcl_ebval = 0。然后
            eb_img = img_green
        elseif的lcl_ebval = 1,则
            eb_img = img_red
        万一
        GridView1.Rows(index_flag).Cells(5)。文本= ds.Tables(0).Rows(0).Item(eb_img)。的ToString
        lcl_dclow = ds.Tables(0).Rows(0).Item(dc_low)。的ToString
        如果lcl_dclow = 0。然后
            dclow_img = img_green
        elseif的lcl_dclow = 1,则
            dclow_img = img_red
        万一
        GridView1.Rows(index_flag).Cells(6)。文本= ds.Tables(0).Rows(0).Item(dclow_img)。的ToString        lcl_hrt = ds.Tables(0).Rows(0).Item(hrt_temp)。的ToString
        如果lcl_hrt = 0。然后
            hrt_img = img_green
        elseif的lcl_hrt = 1,则
            hrt_img = img_red
        万一
        GridView1.Rows(index_flag).Cells(7)。文本= ds.Tables(0).Rows(0).Item(hrt_img)。的ToString        lcl_dfs = ds.Tables(0).Rows(0).Item(curr_dfs)。的ToString
        如果lcl_dfs = 0。然后
            dfs_img = img_green
        elseif的lcl_dfs = 1,则
            dfs_img = img_red
        万一
        GridView1.Rows(index_flag).Cells(8)。文本= ds.Tables(0).Rows(0).Item(dfs_img)。的ToString        lcl_dft = ds.Tables(0).Rows(0).Item(curr_dft)。的ToString
        如果lcl_dft = 0。然后
            dft_img = img_green
        其他
            dft_img = img_orange
        万一
        GridView1.Rows(index_flag).Cells(9)。文本= ds.Tables(0).Rows(0).Item(dft_img)。的ToString
        index_flag = index_flag + 1


解决方案

我可以看到你通过设置各行'细胞从DataReader的获得价值手动填充GridView控件。

但实际上,你可以通过简单地填充网格:

  GridView1.DataSource = DT
GridView1.DataBind()

I am trying to fetch info from mssql server database and want to display it on gridview. But the problem is that i got an error while i run it as Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index and my code is

    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

解决方案

I can see that you are populating the GridView manually by setting each rows' cells the value you get from datareader.

But actually, you can populate the grid by simply:

GridView1.DataSource = dt
GridView1.DataBind()

这篇关于从数据基础负荷电网数据GridView控件不使用绑定的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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