设置数据源后更改datagridview列的数据类型 [英] Changing datatype of datagridview column after setting data source

查看:266
本文介绍了设置数据源后更改datagridview列的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我在C#.NET工作。我有一个datagridview,数据表已被设置为其数据源。数据表是select query(sql)的输出,并且有一个名为date的列,其数据类型为datetime。现在设置数据源后我需要将相应列格的数据类型更改为字符串。有人知道吗?



提前谢谢

Hi I am working in C#.NET. I have a datagridview and a data table has been set as its data source. Data table is the output of select query (sql) and there is a column named date whose data type is datetime. Now after setting data source I needed to change the data type of corresponding column of grid to string. Anyone know about it?

Thanks in advance

推荐答案

我假设您将AutoGenerateColumns设置为false在gridview中。



你可以有两列。

1.
I assume that you are setting AutoGenerateColumns to false in the gridview.

You can have two columns.
1.
<asp:boundfield datafield="Date" dataformatstring="{0:dd MMMM yyyy}" headertext="Date" xmlns:asp="#unknown" />





2.



2.

<asp:templatefield xmlns:asp="#unknown">
  <HeaderTemplate>
   Date</HeaderTemplate>
  <ItemTemplate>
  <asp:Label ID="lblDate" runat="server" ></asp:Label></ItemTemplate>
  </asp:TemplateField></asp:templatefield>



在gridview的DataBound事件中为所有行运行foreach循环,使第一列的visible = false,使用以下内容将值赋给模板列绑定到第一列的值。



或者看看以下单列是否适用于你。


In the DataBound event of the gridview run a foreach loop for all rows, make the first column's visible = false, assign the value to template column using the value bound to the first column.

Alternatively see if following single column work for you.

<asp:templatefield xmlns:asp="#unknown">           
           <ItemTemplate>
            <%# string.Format("{0:dd MMMM yyyy}",Eval("Date"))%>
           </ItemTemplate>                            
         </asp:TemplateField></asp:templatefield>


嗨sumana shabeeb,



我认为数据绑定后不会改变datagridview中的数据类型。

在我的建议中,只需在数据绑定后在datagridview中创建一个数据类型的新列。然后传递删除旧列后,将值放入该新列。



干杯:)
Hi sumana shabeeb,

I think after data binding don't change the data type in datagridview.
In my suggestion just create a new column with data type in datagridview after data binding .Then to pass the value into that new column after delete the old column.

Cheers :)


假设需要的上下文是底层数据库有一个字段数据类型更改 - 需要在datagridview中反映和容纳此更新及其相关的插入和更新命令...



我找到的最简单的方法(使用Visual Studio 2010)是:

转到项目的数据源;

右键单击并选择创建新数据集;

选择所需的表格;

让进程自动生成select / insert / update查询。



此时,您应该拥有一个与原始数据集同名的数据集,其名称后缀为1。删除原始数据集。通过删除1后缀重命名新数据集。此修订后的数据集仍将连接到您的datagridview,但是已修改数据类型。



更改数据集设计器中的数据类型对我不起作用。更新和插入查询必须在封面下进行一些类型检查。



希望这能节省一些时间。



John Cunha
Assuming that the context of the need is that the underlying database had a field data type change - and the need is to have this update reflected and accommodated in the datagridview along with its associated insert and update commands...

The easiest way I have found (using Visual Studio 2010) is to:
Go to your project's datasources;
Right-click and select create new dataset;
Select the desired table;
Let the process auto-generate select/insert/update queries.

At this point you should have a dataset with the same name as your original dataset with a "1" suffix in its name. Delete your original dataset. Rename your new dataset by removing the "1" suffix. This revised dataset will still be wired up to your datagridview, but with revised datatypes.

Changing the datatype in the dataset designer did not work for me. The update and insert queries must do some type checking beneath the covers.

Hope this saves someone some time.

John Cunha


这篇关于设置数据源后更改datagridview列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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