awilderbeast
asked on
c# .net search all columns in a datatable and display the results
hi all,
im trying to add a search to an ontextchanged event for my datatable but havent seemed to get it going yet, below is my attempt
i want to be able to search all the data in the datatable for anything in that box and then show the results.
Thanks
im trying to add a search to an ontextchanged event for my datatable but havent seemed to get it going yet, below is my attempt
i want to be able to search all the data in the datatable for anything in that box and then show the results.
Thanks
protected void txtSearch_TextChanged(object sender, EventArgs e)
{
lblOutput.Text = "Search";
DataRow sResults = dsUsers.Rows.Find("XP");
lvUserItems.DataSource = sResults;
lvUserItems.DataBind();
}
ASKER
i cant see in the above code were to list the current DataTable?
What is dsUsers? Is it the Datatable?
protected void txtSearch_TextChanged(object sender, EventArgs e)
{
lblOutput.Text = "Search";
DataRow[] dr = null;
foreach (DataColumn dc in dsUsers.Columns)
{
DataRow[] drTemp = dsUsers.Select(dc + " = 'XP'");
if (drTemp.Length > 0)
{
if (dr.Length < 0)
dr = drTemp;
else
dr.Concat(drTemp);
}
}
lvUserItems.DataSource = sResults;
lvUserItems.DataBind();
}
ASKER
getting the below error...
also, this search wont be case sensitive will it and will do a contains not an =?
also, this search wont be case sensitive will it and will do a contains not an =?
Both DataSource and DataSourceID are defined on 'lvUserItems'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'lvUserItems'. Remove one definition.
Source Error:
Line 99:
Line 100: lvUserItems.DataSource = dsUsers;
Line 101: lvUserItems.DataBind();
Line 102: }
Line 103:}
protected void txtSearch_TextChanged(object sender, EventArgs e)
{
lblOutput.Text = "Search";
string strSearch = txtSearch.Text;
DataRow[] dr = null;
foreach (DataColumn dc in dsUsers.Columns)
{
DataRow[] drTemp = dsUsers.Select(dc + " = '" + strSearch + "'");
if (drTemp.Length > 0)
{
if (dr.Length < 0)
dr = drTemp;
else
dr.Concat(drTemp);
}
}
lvUserItems.DataSource = dsUsers;
lvUserItems.DataBind();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
bah i just reaslised the datatable is for a userlist, my listview is controlled by an SQLDatasource in design view.
how would i query that in the same way
the sqldatasouce is "SQLDataSource1"
so find.searchtext(%strSearch %) in sqldatasource1.allcolumns
update sqldatasource1 with results
rebind sqldtasource1
Thanks
how would i query that in the same way
the sqldatasouce is "SQLDataSource1"
so find.searchtext(%strSearch
update sqldatasource1 with results
rebind sqldtasource1
Thanks
ASKER
changed to data table, this works
Something like.
Open in new window