简体   繁体   English

检查dataype是否是绑定数据中的日期

[英]checking if dataype is date in bound data

I need to format datetime in a gridview to just show the date. 我需要在gridview中格式化datetime以仅显示日期。 I can't just change the format fo the indiviidual column during the extraction from sql because the code uses the .net generated SqlDataAdapter.Fill. 在从sql提取过程中,我不能只是更改单个列的格式,因为代码使用.net生成的SqlDataAdapter.Fill。 I read on other stackoverflow questions for how to change the date format in bound fields and yet other answers call for using somehting like gridview. 我阅读了其他stackoverflow问题,以了解如何更改绑定字段中的日期格式,而其他答案则要求使用诸如gridview之类的东西。 Format which intellisense is not finding and is unsurprisingly giving me a no-definition compile error if I try to just type and use it. 如果我尝试只是键入和使用它,就不会找到智能感知的格式,并且毫不奇怪地给了我一个没有定义的编译错误。 I can't simply change teh property in the aaspx becuase it is a programatically generated gridview. 我不能简单地在aaspx中更改属性,因为它是以编程方式生成的gridview。

Is there a way to enumerate across the columns in the gridview and check if a column is already a datetime type (they will be coming in as datetime from sql, the sort is already working properly so they are not coming in as strings) and change the format on those to just date? 有没有一种方法可以在gridview中的列之间进行枚举,并检查列是否已经是日期时间类型(它们将作为日期时间从sql中传入,排序已经正常工作,因此它们不会作为字符串传入)并进行更改那些日期的格式? currently when I try to enumerate across the columns it skips because the count of columns in the gridview during the data bound is still coming out as 0. Or does anyone have an idea that would be less expensive, these are thousands of rows coming in so it may be much better if I can just format the column or entire gridview for datetime information. 当前,当我尝试枚举各列时,它会跳过,因为在数据绑定期间,GridView中的列数仍为0。或者有人认为它会便宜些,因为有成千上万的行进来了,所以如果我可以仅格式化列或整个gridview以获得日期时间信息,则可能会更好。 thank you 谢谢

EDIT: I found a way by putting this in the gridview_RowDataBound 编辑:我找到了一种方法,将其放在gridview_RowDataBound中

                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    AutoGeneratedField field = cell.ContainingField as AutoGeneratedField;
                    if (field != null && field.DataType == typeof(DateTime))
                        cell.Text = DateTime.Parse(cell.Text).ToShortDateString();
                }

Even if the SQL is executed by the data adapter, you have the possibility to modify the code in the Select command to transform your datetime field in a string using the Convert function. 即使SQL由数据适配器执行,也可以使用“转换”功能修改“选择”命令中的代码以将日期时间字段转换为字符串。 Something like 就像是

SELECT field1, field2,..., fieldN, convert(varchar, getdate(), 103) as mydate

Or, if the SELECT cannot in any way be modified, you can add a field to the datatable on the fly and put the date in string format on that field (but it is time consuming) and can be a solution only if you can hide your gridview columns so to show only the string formatted one. 或者,如果无法以任何方式修改SELECT,则可以在数据表中快速添加一个字段,并将日期以字符串格式放在该字段上(但这很耗时),并且只有当您可以隐藏时才能作为解决方案您的gridview列,以便仅显示格式化的字符串。

The better in my opinion is change the select, or much better, if you need a specific view on the data, simply create the correct SQL query with the data you need, call it using a DataReader and create a datatable to Use as source for your gridview, and leave the standard select command to more specific uses. 我认为更好的方法是更改​​选择,或者更好,如果您需要数据的特定视图,只需使用所需的数据创建正确的SQL查询,使用DataReader调用它,然后创建一个数据表以用作源您的gridview,并保留标准的select命令以用于更多特定用途。 HTH 高温超导

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

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