简体   繁体   English

VB.net有条件地将隐藏字段值设置为数据表列名称

[英]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. 我有一组asp隐藏字段控件,我希望根据我的数据表列名称设置值,返回的列数也不同,所以如果不使用,我会将未使用的隐藏字段设置为0。 Below is what I've attempted so far just struggling to set the correct hidden field accordingly. 到目前为止,我一直在尝试以下工作,以便相应地设置正确的隐藏字段。

VB- 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 : 试试这个,我认为您不需要For Each循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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