开发者

Sorting a DataGridView by Number or Date in a Windows Form C#

开发者 https://www.devze.com 2022-12-12 01:07 出处:网络
I have a DataGridView on a C# Forms Application I\'m trying to sort. Originally I wanted to sort by the date, but when sorting it treats it like a string.

I have a DataGridView on a C# Forms Application I'm trying to sort.

Originally I wanted to sort by the date, but when sorting it treats it like a string. Example:

22/9/2009

14/4/2008

3/12/2007

Sorts to

14/4/2008

22/9/2009

3/12/2007

Because its only looking at the first character. Once I couldnt solve this I tried to sort by an ID column. Since my data is stored by date anyways I figured I could give them an ID when loading and sort off that, but the same issue occurs. 11 comes before 2 because it starts with a 1 kinds deal.

Any ideas how I can solve this?

Here is my code for reading in the data:

string[] rowArray = new string[] { arService[0], arService[1], arService[2], arService[3], arService[4], arService[5], arService[6] };
dgvSubmissions.Rows.Add(rowArray);

The array is read in from a c开发者_JAVA技巧omma delimited text file and split.

Here's my new code, but it's still providing the same results. What am I doing wrong now?

  int n = dgvSubmissions.Rows.Add();
  dgvSubmissions.Rows[n].Cells[0].Value = arService[0];
  dgvSubmissions.Rows[n].Cells[1].ValueType = typeof(DateTime);
  dgvSubmissions.Rows[n].Cells[1].Value = arService[1];
  dgvSubmissions.Rows[n].Cells[2].Value = arService[2];
  n++;


Your data is string. Convert them to DateTime.

DateTime.Parse(s)
0

精彩评论

暂无评论...
验证码 换一张
取 消