动态设置 BoundField 数据类型的格式
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您像这样设置复选框列表:
您应该能够迭代项目并检查当前项目的值,如下所示:
If you set your check box list like this:
You should be able to iterate through the items and check the value of the current Item like so: