Link to home
Start Free TrialLog in
Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

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

    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        lblOutput.Text = "Search";
        DataRow sResults = dsUsers.Rows.Find("XP");

        lvUserItems.DataSource = sResults;
        lvUserItems.DataBind();
    }

Open in new window

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Rows.Find will find a primary key column. For find in any column in the datatable, you need to loop through all the columns.

Something like.

DataTable dt = new DataTable();

            DataRow[] dr = null;

            foreach (DataColumn dc in dt.Columns)
            {
                DataRow[] drTemp = dt.Select(dc + " = 'XP'");
                if (drTemp.Length > 0)
                {
                    if (dr.Length < 0)
                        dr = drTemp;
                    else
                        dr.Concat(drTemp);
                }
            }
            lvUserItems.DataSource = dr;
            lvUserItems.DataBind();

Open in new window

Avatar of awilderbeast

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();
    }

Open in new window

getting the below error...

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:}
 

Open in new window


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();
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
changed to data table, this works