2012-04-28 136 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

相关问题