2012-04-28 135 views
1

你好我有一個數據網格有列的複選框。我想在特定條件下禁用複選框。我有一個從數據集中獲得的SQL DB,然後填充數據網格中的數據集。這裏是我的一些代碼如何從數據網格中的單元格獲取數據

Dim loopRow As Integer = ds.Tables(0).Rows.Count - 1 
     Dim ColDS As New DataColumn("Status") 
     ds.Tables(0).Columns.Add(ColDS) 
     For loopval As Integer = 0 To loopRow 
      If ds.Tables(0).Rows(loopval)(8).ToString = "True" Then 
       ds.Tables(0).Rows(loopval)(11) = "Confirmed" 
      Else 
       ds.Tables(0).Rows(loopval)(11) = "Pending" 
      End If 
     Next 
     For loopDate As Integer = 0 To ds.Tables(0).Rows.Count - 1 
      If ds.Tables(0).Rows(loopDate)("ProgramTours_FromDate") <= Now.Date Then 

      End If 
     Next 
     GrdAgentBL.DataSource = ds 
     GrdAgentBL.DataBind() 
+0

條件是遊日期如果它小於現在的日期,那麼它將禁用數據網格中名爲Confirmed的列,該列中有一個複選框。如果符合條件,我需要禁用欄目 – amrswalha 2012-04-28 15:00:58

回答

1

我認爲這是您的查詢解決方案。

//I am setting dataTable using this function 
Private Sub setDataTable() 
     Dim dt As DataTable 
     dt = New DataTable() 
     dt.Columns.Add("Name") 
     dt.Columns.Add("Val") 
     Dim dr As DataRow = dt.NewRow() 
     dr("Name") = "Abcd" 
     dr("Val") = 1 
     dt.Rows.Add(dr) 
     dr = dt.NewRow() 
     dr("Name") = "xyz" 
     dr("Val") = 2 
     dt.Rows.Add(dr) 
     grdView.AutoGenerateColumns = False 
     grdView.DataSource = dt 
     grdView.DataBind() 

    End Sub 

// This code will do enabling or disabling the checkbox. 
if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DataRowView grdRow = (DataRowView)e.Row.DataItem; 
      if (grdRow.Row.ItemArray[1].ToString() == "1") 
      { 
       e.Row.Enabled = false; 
      } 

     } 

的最佳實踐:請從形成你的SQL數據表。使用一個簡單的例子,並在你的代碼中排除這個循環。所以,如果有可能是1000行,U需要迭代1000

select val1,val2,(case when val1=0 then 1 else 0 end) as val3 from tbl. 

所以這可能超過了任何迭代

+0

這是一個很好的答案,但我會爲您提供另一個 – amrswalha 2012-04-29 16:24:10

+0

好兄弟。希望你發佈你有,它也會幫助我:) – kbvishnu 2012-04-30 05:46:39

+0

這裏是代碼 – amrswalha 2012-05-01 13:01:35

0

這裏更快的代碼

For Each Item As DataGridItem In GrdAgentBL.Items 'load the items first 
       Dim lblTemp As New Label 'My Data is stored in a label 
       lblTemp = Item.Cells(2).Controls(1) 'So I take it inside that new label 
       Dim tx As String = lblTemp.Text 'Then I load it on the Tx var 
       If tx <= Now.Date Then 'if it's meet the condition we will disable the column 
        Item.Cells(11).Enabled = False 
       End If 
Next 
+0

如果這是爲什麼你不能比較這個值,並從數據庫中獲得布爾值?有像Getdate()等函數返回今天的日期。因此,根據條件,您可以簡單地啓用或禁用網格。 – kbvishnu 2012-05-01 16:55:56

+0

感謝您的回答。我不確定這些行何時執行。會在一些發現後更新你 – kbvishnu 2012-05-01 16:57:58

+0

我可以知道使用rawdatabound和你的方法有什麼區別嗎? – kbvishnu 2012-05-02 14:59:37

相關問題