Adding data to datatable and filter dataview,show empty gridview

You can't filter data other than string in dataview,so i am giving u the idea how to filter all type of data using datatable.I am copying all datasource to a new datatable and then applying filter on dataview.

adding datasource to new datatable then apply filter it filter's all type of data.

public
void ApplyFilter(string colName, string Val)

{

DataTable dt = this.DataSource;

DataTable dt1 = new DataTable();

for (int i = 0; i < dt.Columns.Count; i++)

{

dt1.Columns.Add(dt.Columns[i].ColumnName);

}

for (int i = 0; i < dt.Rows.Count; i++)

{

dt1.Rows.Add(dt.Rows[i].ItemArray);

}

dt1.DefaultView.RowFilter = colName + " LIKE '" + Val + "%'";

if (dt1.DefaultView.Count == 0)

{

EmptyGridFix();

}

else

{

this.DataSource = dt1;

flag = true;

this.DataBind();

}

}

For displaying empty gridview with Header only
public
void EmptyGridFix()

{

DataTable dt = null;

if (this.DataSource is DataTable)

{

dt = ((DataTable)this.DataSource).Clone();

}

if (dt == null)

{

return;

}

dt.Rows.Add(dt.NewRow());// add empty row

this.DataSource = dt;

flag = true;

this.DataBind();

 

}

By smita shelar   Popularity  (3606 Views)