Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends,

here i want to make searchbox to my datagrid view, while typing the letters in textbox should display the user details are existing in the datagrid.


when i will type in a searchbox(textbox) by typing only it should filter the values in datagrid .

friends i have done this but getting an error : Null exception at dv.table=ds.table[0];

this code works fine in c# winform but not in Wpf .

please help me out.


 public DataView dv
       {
           get;
           set;
       }

OleDbCommand cmdd = new OleDbCommand("SELECT addimage,addname,addnumber1 FROM addcontact", conn);
          OleDbDataAdapter da = new OleDbDataAdapter(cmdd);
          DataTable dt = new DataTable();
          da.Fill(ds);
          dv.Table = ds.Tables[0];
          dataGrid1.DataContext = dv;
          dv.RowFilter = "addname like '%" + SearchBox.Text + "%'";
          dataGrid1.DataContext = dv;
Posted
Updated 18-Mar-14 20:50pm
v2
Comments
Naz_Firdouse 19-Mar-14 3:56am    
debug your code and make sure that dv is initialized before assignment and also ds is not null...
Mr.VJ 20-Mar-14 6:05am    
still getting same error


i tried this one ....

OleDbCommand cmdd = new OleDbCommand("SELECT addimage,addname,addnumber1 FROM addcontact", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmdd);

da.Fill(ds);
if (ds != null)
{
view.Table = ds.Tables[0];
dataGrid1.DataContext = view;
view.RowFilter = "addname like '%" + SearchBox.Text + "%'";
dataGrid1.DataContext = view;
}
else
{
MessageBox.Show(" dataset having null value");
}

yes i did myself ..... both , with name and number i can search :)


C#
DataTable dt = new DataTable();
           
            OleDbCommand cmdd = new OleDbCommand("SELECT addimage,addname,addnumber1 FROM addcontact", conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmdd);

            da.Fill(ds);
            
                view.Table = ds.Tables[0];
                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^[a-zA-Z ]+$");
           if(r.IsMatch(SearchBox.Text))
           {
                view.RowFilter = "addname like '%" + SearchBox.Text + "%'";
               dataGrid1.ItemsSource = view;
           }
           else
           {
                view.RowFilter = "addnumber1 like '%" + SearchBox.Text + "%'";
                dataGrid1.ItemsSource = view;
           }
 
Share this answer
 
first create your dataView like

DataView view = dt.DefaultView;

then assigne value to dataview
 
Share this answer
 
Comments
Mr.VJ 20-Mar-14 4:40am    
thank u jayant for replying , but getting an error by putting a DataView view = dt.DefaultView;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900