如何在C#中将行过滤的DataGridView设置为DataTable [英] how to set Row Filtered DataGridView to DataTable in C#

查看:85
本文介绍了如何在C#中将行过滤的DataGridView设置为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridview过滤了其中的某些行,我需要将新的数据源保存到新的DataTable中,由于某种原因,我的当前代码无法正常工作,这里是我尝试转换它的方法.

I have DataGridview that I filtered some of it's row, I need to save the new datasource to a new DataTable, for some reason my current code don't work, here how I'm trying to convert it.

(LogGridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Type IN({0}) AND Date >= {1} AND Date <= {2} AND Content {3} '%{4}%'", typeFilter, startDate, endDate, likeQuery,keywordFilter.Text);
        this.LogGridView.Sort(this.LogGridView.Columns[0], ListSortDirection.Ascending);
FilteredTable =  LogGridView.DataSource as DataTable;

 public DataTable FilteredTable
    {
        get;
        set;
    }

任何想法,为什么它不起作用

any Idea why it is not working

谢谢

推荐答案

您在这里看到的是,在应用过滤器和排序之后,尽管 DataGridView,源 DataTable 仍未更改会按预期显示.这是设计使然.所以打电话:

What you have seen here is that after applying a filter and a sort, the sourced DataTable is unchanged though the DataGridView displays as expected. This is by design. So calling:

FilteredTable =  LogGridView.DataSource as DataTable;

只需将 FilteredTable 设置为与原始表相同.

Just sets FilteredTable to the same as the original table.

相反,我们将创建一种方法:

Instead, we'll create a method to:

  1. 用相同的列创建一个新表.
  2. 使用与 DataGridView 排序相同的过滤器字符串和等效的排序字符串从原始表中选择行.
  3. 对于每个选定的行,克隆项目并将它们作为新行添加到新表中.
  4. 返回新表.
  1. Create a new table with the same columns.
  2. Select the rows from the original table using the same filter string and equivalent sort string as the DataGridView sort.
  3. For each selected row, clone the items and add them as a new row into the new table.
  4. Return the new table.

如此处所示:

private DataTable CloneAlteredDataTableSource(DataGridView dgv)
{
    DataTable dt = dgv.DataSource as DataTable;

    if (dt == null)
    {
        return null;
    }

    DataTable clone = new DataTable();

    foreach (DataColumn col in dt.Columns)
    {
        clone.Columns.Add(col.ColumnName, col.DataType);
    }

    string order = string.Empty;

    switch (dgv.SortOrder)
    {
        case SortOrder.Ascending: order = "ASC"; break;
        case SortOrder.Descending: order = "DESC"; break;
    }

    string sort = dgv.SortedColumn == null ? string.Empty : string.Format("{0} {1}", dgv.SortedColumn.Name, order);

    DataRow[] rows = dt.Select(dt.DefaultView.RowFilter, sort);

    foreach (DataRow row in rows)
    {
        object[] items = (object[])row.ItemArray.Clone();
        clone.Rows.Add(items);
    }

    return clone;
}

和用法:

(this.LogGridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Type IN({0}) AND Date >= {1} AND Date <= {2} AND Content {3} '%{4}%'", typeFilter, startDate, endDate, likeQuery,keywordFilter.Text);
this.LogGridView.Sort(this.LogGridView.Columns[0], ListSortDirection.Ascending);
this.FilteredTable =  this.CloneAlteredDataTableSource(this.LogGridView);

这篇关于如何在C#中将行过滤的DataGridView设置为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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