Click here to Skip to main content
15,667,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
sumana shabeeb 9-Sep-10 4:47am    
i am using windows application

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

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

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

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">           
            <%# string.Format("{0:dd MMMM yyyy}",Eval("Date"))%>
Share this answer
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 :)
Share this answer
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
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900