如何从DataGrid中的单元格获取数据

时间:2012-04-28 14:58:15

标签: vb.net datagrid

您好我有一个数据网格,其中包含具有复选框的列。我想在某种情况下禁用复选框。我有一个SQL DB,我从它得到一个DataSet然后我填充数据网格内的数据集。这是我的一些代码

    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()

2 个答案:

答案 0 :(得分: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行,则需要迭代1000.

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

所以这可能比任何迭代都更快

答案 1 :(得分: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