Tuesday, May 20, 2008

do not forget to Remove DataRowFilter value

If you are playing with datatable`s DefaultView view then don`t forget to change it to default mode by setting RowFilter property of DefaultView to string.Empty, otherwise you may get in to trouble.
Same happened with my one of colleague, and after 2 days debugging she came to me.

== here is full code

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DatatableTest
{
public class DataTableOperationWrapper
{
DataTable dtTable;

public bool ConstructDataTableAndFillData()
{
if (dtTable == null)
{
CreateTable();
}
else
{
dtTable.Rows.Clear();
dtTable.AcceptChanges();
}

DataRow dr = dtTable.NewRow();

for (int i = 0; i <>
{
dr["Name"] = "PersonName_" + i.ToString();
dr["Age"] = i; dtTable.Rows.Add(dr);
dr = dtTable.NewRow();
}

dtTable.AcceptChanges();
return true;
}

private void CreateTable()
{
dtTable = new DataTable();
dtTable.Columns.Add("Name", typeof(string));
dtTable.Columns.Add("Age", typeof(int));
}

public void ApplyDataViewRowFilter(string fileterCondition)
{
if (dtTable == null)
{
ConstructDataTableAndFillData();
}
dtTable.DefaultView.RowFilter = fileterCondition;
}
public DataTable GetResult()
{
if (dtTable == null)
{
new ApplicationException("DataTable is null");
}
return dtTable;
}
///
/// Remove the filters applied as DataRowFilter condition and get all the data from datatable.
///

public void RemoveDataViewRowFilter()
{
if (dtTable == null)
{
new ApplicationException("DataTable is null");
}
dtTable.DefaultView.RowFilter = string.Empty;
}
}
}

== till here

i just tried to set RowFilter value to string.Empty and it worked.
(Don`t get into design of class mentioned above.)

Thanks
Pradeep