Storing filtered rows of dataGridView in a DataTable

爷,独闯天下 提交于 2019-12-25 07:19:37

问题


I have a RadGridView control in my form. I bind DataGriView by one parameter by a search control. I use this code to store my selected rows in a DataTable and show in a report:

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();

This code works, but when I use filter of RadDataGridView for selecting rows, the above code doesn't work good and all rows are displayed. How can change this code for storing only filtered rows in a DataTable.


回答1:


Try making this change and you should get the desired result

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();

where strExpr should be the filtering expression that you want to use. Hope this works for you, Cheers



来源:https://stackoverflow.com/questions/38523692/storing-filtered-rows-of-datagridview-in-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!