2013-05-01 173 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。