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

LVL 1
awilderbeastAsked:
Who is Participating?
 
Obadiah ChristopherConnect With a Mentor Commented:
Do you have set the lvUserItems.DataSourceID property in design mode? If yes remove it.

Also change the code to
DataRow[] drTemp = dsUsers.Select(dc + " = '%" + strSearch + "%'");

Open in new window

0
 
Obadiah ChristopherCommented:
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

0
 
awilderbeastAuthor Commented:
i cant see in the above code were to list the current DataTable?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Obadiah ChristopherCommented:
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

0
 
awilderbeastAuthor Commented:
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

0
 
awilderbeastAuthor Commented:
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
0
 
awilderbeastAuthor Commented:
changed to data table, this works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.