添加新行排序datagridview c# [英] Add new row to sorted datagridview c#

查看:132
本文介绍了添加新行排序datagridview c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向datagridview添加一个新行。

I'm trying to add a new row to my datagridview

DataRow dr = new dt.NewRow();
dt.Rows.InsertAt(dr,5);

上面有几行我有这一行代码

And a few lines above I have this line of code

Datagridview1.Sort(datagridview1.Columns[6],ListSortDirection.Ascending);

问题是我的datagridview在添加新行后再次排序。我的行出现在第5行第1行

The problem is that my datagridview sorted again after im adding new row. And my row appearing at row 1 instmi of row 5

我可以做些什么来解决?

What can i do to fix it?


好的,我会向你解释我的最终目标。我有一个datagridview
排序像我说的。而我想添加一个夏天的行,我需要它
在datagridview的结尾。我该怎么做?

Ok I will explain you my final target. I have a datagridview that sorted like i said. And i want to add a summery row and i need it to be at the end of the datagridview. How can i do it?


推荐答案

这是它的应用方式。您告诉网格根据该列对行进行排序。因此,如果要根据任何其他首选项显示行,请告诉网格。

It is the way it's supposed to be. You told the grid to sort rows based on that column. So if you want to show rows based on any other preference, tell that to the grid.


  • 如果您需要在第一次加载时排序行,只需将其从数据存储中加载,然后不要设置任何排序为网格。这样,您可以在最后插入摘要行。

  • 如果要让用户在拥有摘要行时对网格进行排序,您可以自己处理网格的排序。 li>
  • If you need the rows to be sorted only at first load, just load them sorted from data store and then don't set any sort for grid. This way you can insert your summary row at the end.
  • If you want to let the user also sort the grid while having a summary row, you can handle sorting of grid yourself.

示例

我个人更喜欢创建一个报表来满足这样的要求。此外,摘要可以在网格下的一些文本框中显示。

I personally prefer to create a report to satisfy such requirement. Also the summary can be shown in some text boxes under the grid.

但是,对于您真的需要执行此类任务的情况,以下是一个示例,说明如何让用户对网格进行排序,而您有一个摘要行。它对网格进行排序,而不更改摘要行位置。

But for cases that you really need to do a task like this, here is an example that shows you how to let the user sort grid, while you have a summary row. It sorts the grid without changing the summary row position.

这是表单加载事件。我们加载数据,设置网格的数据源,并将列排序模式设置为程序化

Here is the form load event. We load data, set data source of grid and also set columns sort mode to Programmatic:

DataTable originalData;
private void Form1_Load(object sender, EventArgs e)
{
    GetData();
    SetDataSource();

    //Set all columns sort mode to Programmatic
    this.dataGridView1.Columns.Cast<DataGridViewColumn>().ToList()
        .ForEach(c => { c.SortMode = DataGridViewColumnSortMode.Programmatic; });

    this.dataGridView1.ColumnHeaderMouseClick += dataGridView1_ColumnHeaderMouseClick;
}

处理列标题和排序数据的点击事件。阅读代码的注释:

Handle click event of column headers and sort data. Read comments of code:

void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var grid = (DataGridView)sender;

    //Removes sort glyph 
    this.dataGridView1.Columns.Cast<DataGridViewColumn>()
        .Except(new[] { grid.Columns[e.ColumnIndex] }).ToList()
        .ForEach(c => { c.HeaderCell.SortGlyphDirection = SortOrder.None; });

    //Sort descending if currently sorted ascending
    if (grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection == SortOrder.Ascending)
    {
        originalData.DefaultView.Sort = 
            string.Format("{0} DESC", grid.Columns[e.ColumnIndex].DataPropertyName);
        SetDataSource();
        grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Descending;
    }
    //Sort ascending if currently sorted ascending or not sorted
    else
    {
        originalData.DefaultView.Sort = 
            string.Format("{0} ASC", grid.Columns[e.ColumnIndex].DataPropertyName);
        SetDataSource();
        grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
    }
}

以下是设置网格数据源的方法。在这里,您应该将摘要列添加到原始数据表的副本中:

Here is the method to set data source of grid. Here you should add your summary column to the copy of original data table:

void SetDataSource()
{
    //Copies rows of oroginal data table to a new one based on ordering
    //Then adds a summary row
    //Sets the result as DataSource of grid
    var copy = originalData.DefaultView.ToTable();
    copy.Rows.Add("", originalData.AsEnumerable().Sum(x => x.Field<int>("Value")));
    this.dataGridView1.DataSource = copy;
}

这里是加载数据的方法。您从数据库加载数据,我只是添加一些数据进行测试:

Here is the method to load data. You load data from database and I just add some data to test:

void GetData()
{
    //You load data from database and this data is just for test
    originalData = new DataTable();
    originalData.Columns.Add("Name", typeof(string));
    originalData.Columns.Add("Value", typeof(int));
    originalData.Rows.Add("a", 10);
    originalData.Rows.Add("b", 30);
    originalData.Rows.Add("c", 20);
}

这篇关于添加新行排序datagridview c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆