2013-05-01 178 views
0

我想从mssql服务器数据库中获取信息,并希望在gridview上显示它。但问题是,我得到一个错误,而我运行它为指数超出范围。必须是非负数且小于集合的大小。 参数名:指数和我的代码是从数据库加载网格数据到gridview而不使用绑定方法

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 

回答

0

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

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

GridView1.DataSource = dt 
GridView1.DataBind() 
+0

我已经检查过这个方法。假设我在数据库中有10行。但是如果dr.rows或dr.read()只运行一次,并且只在屏幕上显示结果。但我想,如果我在数据库中有10行,那么循环必须运行10次,因为在每行值的基础上,我必须在gridview中显示图像的数据..... – 2013-05-01 05:21:15

+0

你可以使用While(dr .Read())'如果你有更多的行.. – Sachin 2013-05-01 06:16:21

+0

你可以在网格的itemDataBound事件中设置图像 – ajakblackgoat 2013-05-01 06:16:44

0

我认为你正在比你这个更加困难。将整个数据读入表中,绑定数据,然后使用OnRowDataBound事件修改图像。

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

我也注意到 - 在这行:

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

是否有实际上是一个叫eb_img场?您在上面的行中设置了一个局部变量call eb_img,然后尝试从表中读取它。你不想将文本设置为变量eb_img而不是表格列吗?

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