1

こんにちは、チェック ボックスのある列を持つデータ グリッドがあります。特定の条件でチェックボックスを無効にしたい。そこから DataSet を取得する 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()
4

2 に答える 2

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.

したがって、これはどの反復よりも速く機能する可能性があります

于 2012-04-28T18:38:06.880 に答える
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
于 2012-05-01T13:00:00.523 に答える