[已解决]帮助更改datagridview上单元格的数据类型 [英] [Solved] Help changing the data type of cells on a datagridview

查看:453
本文介绍了[已解决]帮助更改datagridview上单元格的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我正在尝试将查询中的某些信息加载到datagridView中.
该查询返回一个整数和一些小数.

将信息加载到DataGridView中之后,我需要获取第一个数字并对其进行格式化(它是一个日期,因此我将其从(int)yyyymmdd格式更改为(string)dd/mm/yyyy,但是datagridview似乎在询问用于int32 ValueType.

我试图将Cell ValueType更改为String类型.更改它(并检查它现在是否真的是字符串类型)之后,它仍然要求输入int32格式的值.

知道发生了什么吗?

Hi.
I am trying to load some information from a query into a datagridView.
This query return an integer and some decimals.

After loading the information into the DataGridView , I need to take that first number and format it ( it is a date, so I change it from (int) yyyymmdd format to (string) dd/mm/yyyy , but the datagridview seems to ask for an int32 ValueType.

I tried to change the Cell ValueType to String type. After changing it (and checking if it really is string type now) it still asks for an int32 format value.

Any idea of what is happening?

DG.DataSource = //Function giving a data table             
DG.Columns[0].ValueType = typeof(String);
for (int i = 0; i < DG.Rows.Count; i++)
{
    string a = //Function returning date format string, dd/mm/yyyy
    DG.Rows[i].Cells[0].Value = a;
}



更新:
由OP自己解决.将答案作为以下解决方案之一发布.



UPDATE:
Resolved by OP himself. Posted the answer as one of the solutions below.

推荐答案

我通过稍微修改sql语句来解决了这个问题:


MYDATE(数据库中的INT TYPE)


而不是:

从表中选择日期

使得DataGridView上的字段成为Int32,我更改为:

从表中选择CAST(MYDATE AS VARCHAR(8))MYDATE

这样,DataGridView可以使该字段成为字符串类型,我可以毫无问题地对其进行更改.
I solved it changing a little the sql statement:


MYDATE (INT TYPE ON DATABASE)


Instead of doing :

SELECT MYDATE FROM TABLE

which made the field on the DataGridView be an Int32, I changed to:

SELECT CAST(MYDATE AS VARCHAR(8)) MYDATE FROM TABLE

This way, the DataGridView makes that field string type, and I can change it with no problem.


这篇关于[已解决]帮助更改datagridview上单元格的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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