简体   繁体   中英

VB.net setting hidden field value to Data table column names conditionally

I have a set of asp hidden field controls which I wish to set the values according to my data tables column names, the amount of columns returned differ, so I am setting the unused hidden fields to 0 if not used. Below is what I've attempted so far just struggling to set the correct hidden field accordingly.

VB-

            Dim dt As DataTable
            Dim ds As New DataSet()
            ds = Getdata(4)
            dt = ds.Tables(0)

            Dim ColCnt As String = dt.Columns.Count 'Current ColCnt is 3
            For Each column As DataColumn In dt.Columns
                Select Case ColCnt
                    Case 2
                        hxValue.Value = column.ColumnName 'set to 1st Column Name
                        hxValue1.Value = 0 'Not used
                        hyValue.Value = column.ColumnName 'Set To 2nd Column Name
                    Case 3
                        hxValue.Value = column.ColumnName 'set to 1st Column Name
                        hxValue1.Value = column.ColumnName 'set to 2nd Column Name
                        hyValue.Value = column.ColumnName 'set to 3rd Column Name
                End Select
            Next

Try this, I don't think you need a For Each loop :

Dim ColCnt As Int = dt.Columns.Count 'Current ColCnt is 3    
Select Case ColCnt
    Case 2
        hxValue.Value = dt.Columns[0].ColumnName 'set to 1st Column Name
        hxValue1.Value = 0 'Not used
        hyValue.Value = dt.Columns[1].ColumnName 'Set To 2nd Column Name
    Case 3
        hxValue.Value = dt.Columns[0].ColumnName 'set to 1st Column Name
        hxValue1.Value = dt.Columns[1].ColumnName 'set to 2nd Column Name
        hyValue.Value = dt.Columns[2].ColumnName 'set to 3rd Column Name
End Select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM