Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式对datagridview中的行重新排序_C#_Asp.net_Datagridview - Fatal编程技术网

C# 以编程方式对datagridview中的行重新排序

C# 以编程方式对datagridview中的行重新排序,c#,asp.net,datagridview,C#,Asp.net,Datagridview,所以我有这样的想法: List<Customer> customers; customers = Customer.databaseproc(parameters); //databaseproc() is implemented using a data reader and I know it works. DataGridView dgv; dgv.DataSource = customers; customers =customers.OrderBy(x => x.C

所以我有这样的想法:

List<Customer> customers;
customers = Customer.databaseproc(parameters);
//databaseproc() is implemented using a data reader and I know it works.
DataGridView dgv;
dgv.DataSource = customers;
customers =customers.OrderBy(x => x.CustomerName).ToList(); // Sort the list
dgv.DataSource=customers ; // Re-assign the datasource
列出客户;
customers=Customer.databaseproc(参数);
//databaseproc()是使用数据读取器实现的,我知道它可以工作。
DataGridView dgv;
dgv.DataSource=客户;
这不是确切的问题,但我认为首先提供一些上下文可能很重要。如果我需要提供更多/更清晰的细节,我很乐意这样做,但在这一点上,我对SqlConnection以及windows窗体都是新手


问题是:如果我尝试实现自定义排序(在运行时通过用户确定的字段),是否有方法(在上述代码已经执行且gridview可见之后)以编程方式更改行的顺序以反映排序中的这些选择?

CustomerName
Customer
类中的一个属性,并且您希望根据CustomerName对列表进行排序(或者让它成为类中的任何其他属性)。然后,您可以对列表进行排序,然后再次分配数据源(就像@puropoix的注释一样)。它将是这样的:

List<Customer> customers;
customers = Customer.databaseproc(parameters);
//databaseproc() is implemented using a data reader and I know it works.
DataGridView dgv;
dgv.DataSource = customers;
customers =customers.OrderBy(x => x.CustomerName).ToList(); // Sort the list
dgv.DataSource=customers ; // Re-assign the datasource

CustomerName
Customer
类中的一个属性,您希望根据CustomerName对列表进行排序(或者让它成为类中的任何其他属性)。然后,您可以对列表进行排序,然后再次分配数据源(就像@puropoix的注释一样)。它将是这样的:

List<Customer> customers;
customers = Customer.databaseproc(parameters);
//databaseproc() is implemented using a data reader and I know it works.
DataGridView dgv;
dgv.DataSource = customers;
customers =customers.OrderBy(x => x.CustomerName).ToList(); // Sort the list
dgv.DataSource=customers ; // Re-assign the datasource

如果要求的是任何随机顺序,我会这样做

Random r = new Random();
customers =customers.OrderBy(x => r.Next(0,r.customers.Count())) // random order.
                    .ToList(); 
dgv.DataSource=customers ;                                       // Set new DataSource.
或者,您也可以使用
Guid
将列表随机化

customers =customers.OrderBy(x => Guid.NewGuid()) // random order.

检查此项

如果要求任何随机顺序,我会这样做

Random r = new Random();
customers =customers.OrderBy(x => r.Next(0,r.customers.Count())) // random order.
                    .ToList(); 
dgv.DataSource=customers ;                                       // Set new DataSource.
或者,您也可以使用
Guid
将列表随机化

customers =customers.OrderBy(x => Guid.NewGuid()) // random order.

选中此项

如果它已绑定,则可以对列表重新排序。顺便说一句,DataGridView是一个winforms项目,而web版本是GridView@Plutonix事实上,我使用的是winforms,但有什么主要区别吗?如果它是绑定的,则需要对列表重新排序。顺便说一句,DataGridView是一个winforms项目,而web版本是GridView@Plutonix事实上,我正在使用winforms,但有什么主要区别吗?这看起来真的是一个简单的解决方案,我可能应该在读到@puropoix的建议后立即调查/发现。。。我对c#中的库函数太无知了,仍然。。。但是是的。这似乎正是我要找的东西。谢谢。老实说,这似乎是一个简单的解决方案,我可能应该在读到@puropoix的建议后立即调查/发现。。。我对c#中的库函数太无知了,仍然。。。但是是的。这似乎正是我要找的东西。非常感谢。