将DataGridView的已过滤行存储在DataTable中 [英] Storing filtered rows of dataGridView in a DataTable
问题描述
DataTable table = new DataTable(dt1 );
foreach(radGridView1.Columns中的Telerik.WinControls.UI.GridViewDataColumn列)
{
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]。
}
}
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中的过滤行。
尝试进行此更改,您应该获得所需的结果
DataTable table = new DataTable(dt1);
foreach(radGridView1.Columns中的Telerik.WinControls.UI.GridViewDataColumn列)
{
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]。
}
}
DataSet ds = new DataSet();
ds.Tables.Add(table);
var dv = ds.Tables [0] .DefaultView;
var strExpr =ItemID = 1; //相应更改
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应该是要使用的过滤表达式。希望这适合你,干杯
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.
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
这篇关于将DataGridView的已过滤行存储在DataTable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!