如何在GridView中显示之前更改数据? [英] how change data before showing in GridView?

查看:104
本文介绍了如何在GridView中显示之前更改数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个显示我的数据的gridView。

用于填充girdview,我使用实体框架。



hello. I have a gridView that shows my data.
for to fill girdview ,I use entity framework.

myGridView.DataSource = db.Books.ToList();



我的数据库中有一个具有日期时间类型的列。

因为datetime在sql server中键入不支持solar日期,

i想要从数据库获取数据时将其转换为日期,然后在我的gridView中显示

但是上面的代码没有为此工作。所以我使用了代码,但这次遇到错误:




I have a column in my database that has datetime type.
because datetime type in sql server not support solar date,
i want when i get data from database convert it to solar date and then show it in my gridView
but above code don't work for this. and so i used down code but this encounter to error :

var query = from book in db.Books
	    select new (libraryString.ConvrtToSolarDate(RegisterDate),...,...,...); 



(libraryString是我写的转换为日期的类)

有人可以帮我吗?



(执行我的英语不好)


(libraryString is a class that i wrote for convert to solar date)
can anybody help me?

(execuse me for my poor english)

推荐答案

Gridview有一个事件,即RowDataBound。您可以在此处执行所有操作,这将在加载Gridview之前执行。
There is one event of Gridview i.e. RowDataBound. You can do all your action here, this will execute before loading the Gridview.


我找到了另一种方法:

I found another way for that :
var query = from book in db.Books
            select book;
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("RegisterDate");
foreach (var item in query)
{
        dt.Rows.Add(item.Name,
        LibString.ConvrtToSolarDate(item.RegisterDate));
}
myDataGridView.DataSource = dt;


这篇关于如何在GridView中显示之前更改数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆