How do I manipulate the format on a DataGridView that is bound to a Data Source?
Posted
by Jack Johnstone
on Stack Overflow
See other posts from Stack Overflow
or by Jack Johnstone
Published on 2010-06-16T11:10:59Z
Indexed on
2010/06/16
11:12 UTC
Read the original article
Hit count: 164
visual-studio-2008
|datagridview
I´m using SQL Server 2005 and Visual Studio 2008, C#.
In the data source (the SQL Server data table) I use the DateTime format mm/dd/yyyy, however, in a forms overview (DataGridView) users would like to see a completely other format, with year, week number and day number of week (yyww,d) which is in string format. I´ve created an algorithm for the transformation between values (date to weekday), but can I populate the affected cells with yyww,d (string) instead of mm/dd/yyyy (DateTime)?
This is what I´ve been testing out, without success (and note, it´s on the last line the problem becomes obvious, as the cell value won´t accept a string on runtime - it still wants to be a DateTime...)
private void DataGridViewQueryFindRequests_CellFormatting(
object sender, DataGridViewCellFormattingEventArgs e)
{
string weekAndDay = "";
DataGridViewCell cell =
DataGridViewQueryFindRequests.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.ColumnIndex == 13 && cell.Value == null)
mEmptyRow = true;
if ((cell.ColumnIndex == 14 || cell.ColumnIndex == 15) && !mEmptyRow)
{
weekAndDay = ClassWeeksAndDates.dateToWeekNumber(Convert.ToDateTime(cell.Value));
cell.ValueType = typeof(string);
cell.Value = weekAndDay;
}
}
© Stack Overflow or respective owner