First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I use DataView RowFilter to filter the Datagrid row, I want to restore the previous Datagrid information if the user removes the filter.
So before applying the filter in DataGrid, I need its data.
I used the following method to convert DataGrid, this method converts datagrid to datatable but does not transfer its data in DataGrid:
public static DataTable DataGridtoDataTable(DataGrid dg)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
dg.UnselectAllCells();
String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
string[] Lines = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
for (int i = 0; i < Cols; i++)
dt.Columns.Add(Fields[i].ToUpper(), typeof(string));
DataRow Row;
for (int i = 1; i < Lines.GetLength(0)-1; i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt.NewRow();
for (int f = 0; f < Cols; f++)
{
Row[f] = Fields[f];
}
dt.Rows.Add(Row);
}
return dt;
}
This is my method:
PreviousMemberDT = MemberDT.Clone();
public DataTable Filter(DataTable dataTable,DataTable previousDataTable, string column, TextBox textBox,
DataGrid dataGrid)
{
previousDataTable = DataGridtoDataTable(dataGrid);
OleDbCommand OleDCmd = new OleDbCommand("Select * From MemberTable", OleDbConnect);
OleDCmd.CommandType = CommandType.Text;
OleDbConnect.Open();
OleDbDataAdapter DA;
DA = new OleDbDataAdapter(OleDCmd);
dataTable.Clear();
DA.Fill(dataTable);
DataView DV = new DataView(dataTable);
DV.RowFilter = "[" + column + "]='" + textBox.Text.Trim() + "'";
MemberDT = DV.ToTable();
dataGrid.ItemsSource = MemberDT.DefaultView;
OleDbConnect.Close();
return previousDataTable;
}
I also tried the following code:
DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();
Thanks
