blah blah blah is here! blah blah » Close

up0down
link

Hi
I don't know if this is a big issue, but I don't seem to find a solution when I google around.
I have a gridview and I want the users to fill in what date they worked and also what time they start working and end working. I've used dataformatstring so it looks like this
Date - 2010-02-07
Start - 07:00
End - 16:00
My issue is that the row in my gridview is empty and you just have to know to fill in with - between 2010 and 02 and also - between 02 and 07. Same with the time you must know to fill in : between 07 and 00. Is there some easy way to solve it? I should only need to fill in 20100207 and for the time 0700 directly.
I'm working in VS2008 with asp.net in c#.
Thank you.

last answered 2 years ago

1 answers

up0down
link

I would handle the CellEndEdit event and do some cheking on the cell's value... Something along the lines of:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == 0) //Check ColumnIndex to only do this on your date-column
{
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != null && //Make sure something is there
dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString().Length == 8) //And make sure there's enough
{
string datestring = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
datestring = datestring.Insert(4, "-").Insert(7, "-").Insert(8, "-");
dataGridView1[e.ColumnIndex, e.RowIndex].Value = datestring;
}
else
dataGridView1[e.ColumnIndex, e.RowIndex].Value = null;
}
}


I'm sure this can be done with string formatting too, but I'll let someone else post that :)

Thank you very much.

Feedback