如何对datagridview进行排序

如何对datagridview进行排序

本文介绍了如何对datagridview进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我不知道,我尝试对datagridview进行排序。我想按时间排序。

我有4列,其中3列为字符串,1列为DataTime。

我想对列进行排序日期

低于我的代码。

Hello,

I do not know and I try to sort datagridview. I want to sort by "Time".
I have 4 columns with 3 columns in string and and 1 in DataTime.
I want to sort column Date
Below my code.

 <pre>
var test = d.DocumentNode.SelectNodes("//td");
			string patt = "HOST\\=(.*)\\&\\;SERVICE\\=(.*)...IMG(.*)title\\=\"(.*):(.*):(.*)\"\\salt\\=\"(.*):(.*):(.*)\"\\ssrc(.*)border";
			Regex regex = new Regex(patt, RegexOptions.IgnoreCase);

			List<Powiadomienie> powiadomienia = new List<Powiadomienie>();
			foreach (var item in test)
			{

					Match match = regex.Match(item.InnerHtml);


				if (match.Groups.Count > 1)

				{
					Powiadomienie powiadomienie = new Powiadomienie();


					powiadomienie.Host = match.Groups[1].Value;
					powiadomienie.Service = match.Groups[2].Value;
					powiadomienie.Time = match.Groups[6].Value;
					powiadomienie.Colour = match.Groups[8].Value;
					powiadomienie.AltValueTime = match.Groups[9].Value;

					if (!powiadomienie.AltValueTime.Contains('m'))
					{
						powiadomienie.AltValueTime = "0m" + powiadomienie.AltValueTime;
					}

					if (!powiadomienie.AltValueTime.Contains('h'))
					{
						powiadomienie.AltValueTime = "0h" + powiadomienie.AltValueTime;
					}

					if (!powiadomienie.AltValueTime.Contains('d'))
					{
						powiadomienie.AltValueTime = "0d" + powiadomienie.AltValueTime;
					}

					if (!powiadomienie.AltValueTime.Contains('s'))
					{
						powiadomienie.AltValueTime = powiadomienie.AltValueTime + "0s";
					}


					var temp = powiadomienie.AltValueTime.Replace('d', ':').Replace('h', ':').Replace('m', ':');
					var tempSplitted = temp.Split(':');
					powiadomienie.Date = DateTime.Now;
					powiadomienie.Date = powiadomienie.Date.AddDays(0 - Convert.ToInt32(tempSplitted[0]))
															.AddHours(0 - Convert.ToInt32(tempSplitted[1]))
															.AddMinutes(0 - Convert.ToInt32(tempSplitted[2]));


					powiadomienia.Add(powiadomienie);
				}

			}





我尝试过:



我尝试使用它:



What I have tried:

I try use this:

dataGridView1.Sort(dataGridView1.Columns["Date"], ListSortDirection.Ascending);



但收到错误。

我不知道怎么排序。


But recieved error.
I do not have any idea how can I sort.

推荐答案

DataGridView control must be bound to an IBindingList object to be sorted.

你直接使用列表作为DGV的DataSource的是哪个。

最简单的解决方案是将List转换为DataTable,并使用它作为来源。

这很痛苦,所以我写了这个: []这样做...

然后它是微不足道的:

Which mans that you have used the list directly as the DataSource for the DGV.
The easiest solution is to convert the List to a DataTable, and use that as the source.
That's a pain, so I wrote this: Converting a List to a DataTable[^] to do just that...
Then it's trivial:

            List<MyClass> list = new List<MyClass>();
            list.Add(new MyClass() { Name = "DDDD", Date = DateTime.Now });
            list.Add(new MyClass() { Name = "BBBB", Date = DateTime.Now.AddDays(-1) });
            list.Add(new MyClass() { Name = "CCCC", Date = DateTime.Now.AddDays(-2) });
            list.Add(new MyClass() { Name = "AAAA", Date = DateTime.Now.AddDays(-3) });
            DataTable dt = list.ToDataTable();
            dataGridView1.DataSource = dt;
            }
        private void SortByName_Click(object sender, EventArgs e)
            {
            dataGridView1.Sort(dataGridView1.Columns["Name"], ListSortDirection.Ascending);
            }

        private void SortByDate_Click(object sender, EventArgs e)
            {
            dataGridView1.Sort(dataGridView1.Columns["Date"], ListSortDirection.Ascending);
            }
        }
...
    public class MyClass
        {
        public string Name { get; set; }
        public DateTime Date { get; set; }
        }


这篇关于如何对datagridview进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:02