amrswalha
amrswalha

Reputation: 334

How to get data from a cell in DataGrid

Hello I have a Data Grid That have columns that have check box. I would like to disable the check box on a certain condition. I have a SQL DB that I get from it a DataSet then I fill the data set inside the Data Grid. Here is some of my code

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

Upvotes: 1

Views: 1764

Answers (2)

amrswalha
amrswalha

Reputation: 334

Here is the code

 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

Upvotes: 0

kbvishnu
kbvishnu

Reputation: 15630

I think this is a solution for your query.

//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;
            }

        }

best practice : Please form your datatable from SQL. using a simple case and exclude this loop in your code. So if there may be a 1000 rows, u need to iterate 1000.

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

So this may works faster than any iteration

Upvotes: 1

Related Questions