在DataTable中存储已过滤的dataGridView行

时间:2016-07-22 10:06:38

标签: c# datagridview filter datatable telerik

我的表单中有一个RadGridView控件。我通过搜索控件将DataGriView绑定到一个参数。我使用此代码将我选择的行存储在DataTable中并显示在报告中:

DataTable table = new DataTable("dt1");

        foreach (Telerik.WinControls.UI.GridViewDataColumn column in radGridView1.Columns)
        {
            table.Columns.Add(column.Name, typeof(string));
        }
        for (int i = 0; i < radGridView1.Rows.Count; i++)
        {
                table.Rows.Add();
                for (int j = 0; j < radGridView1.Columns.Count; j++)
                {
                    table.Rows[i][j] = radGridView1.Rows[i].Cells[j].Value;
                }                
        }

        DataSet ds = new DataSet();
        ds.Tables.Add(table);
        StiReport stiReport = new StiReport();
        stiReport.Load("Report.mrt");
        stiReport.RegData(table);
        StiOptions.Viewer.Windows.ShowPageDesignButton = false;
        StiOptions.Viewer.Windows.ShowOpenButton = false;
        //stiReport.Design();
        stiReport.Show();

此代码有效,但是当我使用RadDataGridView的过滤器来选择行时,上面的代码不能正常工作并显示所有行。如何更改此代码以仅在DataTable中存储已过滤的行。

1 个答案:

答案 0 :(得分:1)

尝试进行此更改,您应该获得所需的结果

DataTable table = new DataTable("dt1");

        foreach (Telerik.WinControls.UI.GridViewDataColumn column in radGridView1.Columns)
        {
            table.Columns.Add(column.Name, typeof(string));
        }
        for (int i = 0; i < radGridView1.Rows.Count; i++)
        {
                table.Rows.Add();
                for (int j = 0; j < radGridView1.Columns.Count; j++)
                {
                    table.Rows[i][j] = radGridView1.Rows[i].Cells[j].Value;
                }                
        }

        DataSet ds = new DataSet();
        ds.Tables.Add(table);
        var dv = ds.Tables[0].DefaultView;
        var strExpr = "ItemID = 1"; //Change accordingly
        dv.RowFilter = strExpr;
        var newDT = dv.ToTable();
        StiReport stiReport = new StiReport();
        stiReport.Load("Report.mrt");
        stiReport.RegData(newDT);
        StiOptions.Viewer.Windows.ShowPageDesignButton = false;
        StiOptions.Viewer.Windows.ShowOpenButton = false;
        //stiReport.Design();
        stiReport.Show();

其中strExpr应该是您要使用的过滤表达式。希望这适合你,干杯