我正在使用ExcelDataReader将数据加载到DataSet
中,该数据最终将被加载到WPF中的DataGrid
中。我有一些特定的列,其标题为SLNO
和FY
,但我不想包括在内。如何使用here中提到的FilterColumn
忽略那些列?
答案 0 :(得分:1)
如果要在显示时过滤这些列,请使用以下代码:
gridview.Columns["ColumnName"].Visible = false;
或:
DataView view = new DataView(DataSet.table[0]);
DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn", "ThirdColumn");
GridDataView.DataSource = Table2;
答案 1 :(得分:0)
您可以处理AutoGeneratingColumn
事件:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Cancel = e.PropertyName == "SLNO" || e.PropertyName == "FY";
}
答案 2 :(得分:0)
FilterColumn
的工作方式
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
List<string> skipColumns = new List<string> { "SLNO", "FY" };
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true,
FilterColumn = (rowReader, columnIndex) =>
{
return !skipColumns.Contains((rowReader[columnIndex].ToString()));
}
}
});
}
}