• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1083
  • Last Modified:

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

0
awilderbeast
Asked:
awilderbeast
  • 4
  • 3
1 Solution
 
informaniacCommented:
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
 
informaniacCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now