如何从数据网格中的单元格获取数据
问题描述:
你好我有一个数据网格有列的复选框。我想在特定条件下禁用复选框。我有一个从数据集中获得的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()
答
我认为这是您的查询解决方案。
//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.
所以这可能超过了任何迭代
答
这里更快的代码
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
条件是游日期如果它小于现在的日期,那么它将禁用数据网格中名为Confirmed的列,该列中有一个复选框。如果符合条件,我需要禁用栏目 – amrswalha 2012-04-28 15:00:58