Generic data viewer with filter

Data.DataTable ThisDataTable = new Data.DataTable();
ThisDataTable.ReadXml("C:\\TableData.xml");
//This code requires a DropDownList called ColumnsDropDownList
if (ColumnsDropDownList.Items.Count < 1) {
 foreach (Data.DataColumn ThisColumn in ThisDataTable.Columns) {
  ColumnsDropDownList.Items.Add(ThisColumn.ColumnName);
 }
}
Data.DataView FilteredDataView = ThisDataTable.DefaultView;
//This code requires a TextBox called SearchTextBox
if (!string.IsNullOrEmpty(SearchTextBox.Text)) {
 string FilterText = string.Format("convert([{0}],'System.String') like '%{1}%'", ColumnsDropDownList.SelectedValue, SearchTextBox.Text);
 FilteredDataView = new Data.DataView(ThisDataTable, FilterText, null, Data.DataViewRowState.CurrentRows);
}
GridView1.DataSource = FilteredDataView;
GridView1.DataBind();

No comments:

Post a Comment