动态设置 BoundField 数据类型的格式

发布于 2024-12-05 18:31:55 字数 1005 浏览 1 评论 0原文

我使用 CheckBoxList 来定义 GridView 中显示哪些列。我使用类似的查询填充 CheckBoxList

select column_name, data_type from information_schema.columns
    where table_name = 'myTable'

在用户选择了他们希望显示的列(其中包含各种数据类型)后,按下按钮,以下 VB 代码片段将生成 GridView。

For Each item As ListItem In chooseColsList.Items
    If item.Selected Then
        Dim bf As New BoundField()
        'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
        bf.DataFormatString = "{0:dd-MMM-yyyy}"
        bf.ApplyFormatInEditMode = True
        bf.DataField = item.Value
        bf.HeaderText = item.Value
        bf.SortExpression = item.Value
        statusReportGrid.Columns.Add(bf)
    End If
Next

我想要做的是将 DataFormatString 仅应用于其中包含“日期”数据类型的列。但是,我没有找到以编程方式确定绑定列中数据类型的方法,也没有任何方法将此信息传递给控件。此信息存在于 information_schema 中,如我的查询中所示,但我不知道如何提取该信息并使用它来动态设置我的 BoundFields。值得注意的是,我使用的是 SqlDataSource

我已经尝试了几乎所有我能想到的可能的解决方案,并最终来到这里。

预先感谢您的帮助,非常感谢:)

I'm using a CheckBoxList to define which columns are displayed in a GridView. I populate the CheckBoxList using a query like

select column_name, data_type from information_schema.columns
    where table_name = 'myTable'

After the user has chosen the columns (with various Data Types in them) they wish to display, they press a button and the following snippet of VB code generates the GridView.

For Each item As ListItem In chooseColsList.Items
    If item.Selected Then
        Dim bf As New BoundField()
        'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
        bf.DataFormatString = "{0:dd-MMM-yyyy}"
        bf.ApplyFormatInEditMode = True
        bf.DataField = item.Value
        bf.HeaderText = item.Value
        bf.SortExpression = item.Value
        statusReportGrid.Columns.Add(bf)
    End If
Next

What I want to do is to apply the DataFormatString ONLY to the columns with a 'date' Data Type in them. However, I have found no way of programmatically determining the Type of the data in the column being bound, no any way of passing this info to the Control. This information exists in the information_schema, as shown in my query, but I don't know how to extract that out and use it to dynamically setup my BoundFields. It is important to note that I'm using an SqlDataSource.

I've experimented with just about every possible solution I can think of and end up here.

Thanks in advance for your help, it is VERY appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2024-12-12 18:31:55

如果您像这样设置复选框列表:

<asp:checkboxlist id="list" runat="server"  
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />

您应该能够迭代项目并检查当前项目的值,如下所示:

For Each item As ListItem In chooseColsList.Items
    If item.Selected Then
        Dim bf As New BoundField()

        If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
            bf.DataFormatString = "{0:dd-MMM-yyyy}"
            bf.ApplyFormatInEditMode = True
        End If
        bf.DataField = item.Text
        bf.HeaderText = item.Text
        bf.SortExpression = item.Text
        statusReportGrid.Columns.Add(bf)
    End If
Next

If you set your check box list like this:

<asp:checkboxlist id="list" runat="server"  
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />

You should be able to iterate through the items and check the value of the current Item like so:

For Each item As ListItem In chooseColsList.Items
    If item.Selected Then
        Dim bf As New BoundField()

        If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
            bf.DataFormatString = "{0:dd-MMM-yyyy}"
            bf.ApplyFormatInEditMode = True
        End If
        bf.DataField = item.Text
        bf.HeaderText = item.Text
        bf.SortExpression = item.Text
        statusReportGrid.Columns.Add(bf)
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文