将dataGridView的筛选行存储在DataTable中

本文关键字:DataTable 存储 dataGridView 筛选 | 更新日期: 2023-09-27 17:58:08

我的表单中有一个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中仅存储筛选的行。

将dataGridView的筛选行存储在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);
        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应该是要使用的筛选表达式。希望这对你有用,干杯