Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to display record that meet my search criteria to datagridview only. I have tried grd1.CurrentCell = grd1.Rows[i].Cells[0]; but does not worked.
Please help.
Following are my code:

//Click to query
C#
public void button1_Click(object sender, EventArgs e)
        {
            string cstr;
            cstr = "Data Source=P1L3ITN024\\SQLEXPRESS; Integrated Security = True; ";
            SqlConnection con1 = new SqlConnection(cstr);
            con1.Open();
            SqlCommand com1 = new SqlCommand();
            com1.Connection = con1;
            com1.CommandType = CommandType.Text;
            com1.CommandText = "select * from dbo.spt_values";
            DataSet ds1 = new DataSet();
            SqlDataAdapter adp1 = new SqlDataAdapter(com1);
            adp1.Fill(ds1,"values");
            grd1.DataSource = ds1;
            grd1.DataMember = "values";
            con1.Close();  
        }
//Click to Search
C#
public void btnSearch_Click(object sender, EventArgs e)
      {
          grd1.ClearSelection();

          string search = txtSearch.Text;

          for (int i = 0; i < grd1.Rows.Count - 1; i++)
          {
              for (int j = 0; j < grd1.Columns.Count; j++)
              {
                 
                  if (grd1.Rows[i].Cells[0].Value.ToString().ToLower().StartsWith(search.ToLower()))
                  {
                      grd1.Rows[i].Selected = true;

                                           if (grd1.SelectedRows.Count > 0)
                      {

//What should i do here to get the selected record display into datagridview??

                                              }

                                      }

              }
          }
Posted

C#
public void btnSearch_Click(object sender, EventArgs e)
{
  grd1.ClearSelection(); 
  string search = txtSearch.Text;
 
  for (int i = 0; i < grd1.Rows.Count - 1; i++)
  {
      for (int j = 0; j < grd1.Columns.Count; j++)
      {                 
        if(grd1.Rows[i].Cells[0].Value.ToString().ToLower().Contains(search.ToLower()))    
        {
          break; 
        }
        if(j == (grd1.Columns.Count-1))
        {
           grd1.Rows[i].Visible = false;
        }
      }
 
  }
}
 
Share this answer
 
Thanks Mohd. Mukhtar!

I get "Row associated with the currency manager's position cannot be made invisible" error.
The reason for this particular error is that we cannot make modifications to a row at runtime which is bound to datasource, unless we suspened the binding on all rows using CurrencyManager object by adding grd1.CurrentCell = null.

C#
public void btnSearch_Click(object sender, EventArgs e)
{
  grd1.ClearSelection();
  string search = txtSearch.Text;

  for (int i = 0; i < grd1.Rows.Count - 1; i++)
  {
      for (int j = 0; j < grd1.Columns.Count; j++)
      {
        if(grd1.Rows[i].Cells[0].Value.ToString().ToLower().Contains(search.ToLower()))
        {
          break;
        }
        if(j == (grd1.Columns.Count-1))
        {
           //add this
           grd1.CurrentCell = null;           
           grd1.Rows[i].Visible = false;
        }
      }

  }
}
 
Share this answer
 

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