Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello.

need a help.
i m doing a windows application where i need to search a value from text box in grid view after search button click.
the row containing the search value must b selected. if it is possible then please help me.


there is one button one text box and one grid view.
now in form load i am retrieving records from database.
now i want to search details of a consumer by giving his consumer id in text box.
now my requirement is after clicking search button i need the row containing this value should be highlighted.
please give solution or give any useful link.

thanks

regards
prajucta
Posted
Updated 15-Oct-18 21:17pm
v3
Comments
indrajeet jadhav 27-Sep-13 3:30am    
Will you post your code ??so we can help you better
Please search in Google and try something.
If you face issues, come back here and ask a Question.
Prajucta.Chatarjee 27-Sep-13 4:38am    
as i am a beginner i don't have any idea regarding this.
i have searched Google too many times but till now i haven't got any satisfactory solution.

there is one button one text box and one grid view.
now in form load i am retrieving records from database.
now i want to search details of a consumer by giving his consumer id in text box.
now my requirement is after clicking search button i need the row containing this value should be highlighted.
please give solution or give any useful link.
Check my answer.
Hi Prajucta,

You accepted my answer and then I guess you rejected it by mistake. Is there any problem?
Please feel free to ask questions, otherwise accept the answer.

Thanks,
Tadit.

This should help you.

Search Row in DataGridView in C#[^]

Regards..
 
Share this answer
 
Comments
Prajucta.Chatarjee 27-Sep-13 6:14am    
thanks it helped me .. thanks a lot.
Thanks7872 27-Sep-13 6:15am    
Its always good to accept the answers which helped you in order to let others know which worked as expected.
If you Want Values from textbox which under the gridview
May this will help you.

C#
Textbox tex= gridview1.findcontrol("your textbox name") as textbox;
string value=tex.text;
 
Share this answer
 
v2
Comments
Prajucta.Chatarjee 27-Sep-13 4:42am    
thanks for your reply but this is not the solution what i am looking for.

there is one button one text box and one grid view.
now in form load i am retrieving records from database.
now i want to search details of a consumer by giving his consumer id in text box.
now my requirement is after clicking search button i need the row containing this value should be highlighted.
please give solution or give any useful link.
Ashwani Gusain 27-Sep-13 4:50am    
please use this link may this will help

http://bytes.com/topic/c-sharp/answers/885319-search-value-datagridview
Prajucta.Chatarjee 27-Sep-13 6:13am    
thanks a lot.
i got it. :) i m done.. :)
thanks Ashwani gusain
thanks all..
Ashwani Gusain 27-Sep-13 7:10am    
if you got you answer please click on accept answer
Here Is Fully Working Codes.
where gvDetail is name of Grid view. 
On Search Button Click Write The Codes. 

searchString = textBox1.Text.Trim(); 
if (searchString == string.Empty || searchString.Length < 8)
  {
     MessageBox.Show("Enter Valid Consumer Number..!");
  }
  else
   {
     foreach (DataGridViewRow row in gvDetail.Rows)
   {                              if(row.Cells["SERVICENO"].Value.ToString().Contains(searchString))
    {
      gvDetail.CurrentRow.Selected = false;
      gvDetail.Rows[row.Index].Selected = true;
      int index = row.Index;
      gvDetail.FirstDisplayedScrollingRowIndex = index;
      break;
    }
  }
}
 
Share this answer
 
v2
Comments
Prajucta.Chatarjee 28-Sep-13 6:02am    
thanks Dibyaranjan.
it Works. :)
in search button write this code i have used fluxgird for testing u can use datagridview

C#
try
{
    FormatGrid();

    OleDbConnection con = new OleDbConnection(path);
    con.Open();
    //OleDbDataAdapter da = new OleDbDataAdapter("Select * from ItemMast where Icode like '" + txtICode.Text.Trim() + "%' order by Iname", con);
    OleDbDataAdapter da = new OleDbDataAdapter("Select * from ItemMast where IName like '" + txtICode.Text.Trim() + "%' order by IName", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "ItemMast");
    DataTable dt = ds.Tables["ItemMast"];
    if (dt.Rows.Count > 0)
    {
        int i = 1;
        MSGrid1.Rows = dt.Rows.Count + 1;
        foreach (DataRow dr in dt.Rows)
        {
            MSGrid1.set_TextMatrix(i, 0, i.ToString());
            MSGrid1.set_TextMatrix(i, 1, dr["ICode"].ToString());
            MSGrid1.set_TextMatrix(i, 2, dr["IName"].ToString());
            MSGrid1.set_TextMatrix(i, 3, dr["SubSubGroupName"].ToString());
            MSGrid1.set_TextMatrix(i, 4, dr["GroupName"].ToString());
            MSGrid1.set_TextMatrix(i, 5, dr["SubGroupName"].ToString());
            //MSGrid1.set_TextMatrix(i, 4, dr["ICode"].ToString());
            //MSGrid1.set_TextMatrix(i, 3, dr["AvlQty"].ToString());

            double st1 = double.Parse(dr["AvlQty"].ToString());

            if (CheckDecimal(st1) == false)
                MSGrid1.set_TextMatrix(i, 6, st1.ToString("0"));
            else
                MSGrid1.set_TextMatrix(i, 6, st1.ToString());     

            i = i + 1;
        }
    }
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
 
Share this answer
 
v2
Refer - [Solution 3] Different row color in Gridview[^]
Quote:
C#
foreach (GridViewRow gvrow in GridView1.Rows)
{
    if(gvrow.Cells["ID"].Value = 1)
    {
          gvrow.DefaultCellStyle.BackColor = Color.Red;
    }
    else
    {
          gvrow.DefaultCellStyle.BackColor = Color.Green;          
    }
}

Here it is checking ID value 1.

According to your requirements, you need to check the Customer ID like this and match that with the Searched ID (entered in the TextBox).

Also check - search for a value in a Datagridview[^]
 
Share this answer
 
v2
This Code Works . :)
gvDetail is the name of Grid view.


C#
searchString = textBox1.Text.Trim(); 
if (searchString == string.Empty || searchString.Length < 8)
  {
     MessageBox.Show("Enter Valid Consumer Number..!");
  }
  else
   {
     foreach (DataGridViewRow row in gvDetail.Rows)
   {                              if(row.Cells["SERVICENO"].Value.ToString().Contains(searchString))
    {
      gvDetail.CurrentRow.Selected = false;
      gvDetail.Rows[row.Index].Selected = true;
      int index = row.Index;
      gvDetail.FirstDisplayedScrollingRowIndex = index;
      break;
    }
  }
}
 
Share this answer
 
v2
//Just improving the previous solution (Solution7)

string searchString = Textbox1.Text.Trim();

{
foreach (DataGridViewRow row in Gridview.Rows)
{
if (row.Cells[1].Value.ToString().ToLower() == searchString.ToLower() || row.Cells[1].Value.ToString().ToLower().Contains( searchString.ToLower()) )
{
Gridview.ClearSelection();
Gridview.CurrentRow.Selected = false;
Gridview.Rows[row.Index].Selected = true;
int index = row.Index;
Gridview.FirstDisplayedScrollingRowIndex = index;
break;
}
}
}
 
Share this answer
 
Comments
CHill60 16-Oct-18 6:58am    
In what way is this an improvement?
You're clearing the grid selection multiple times and have removed the check on a search string being empty.
Dumping unformatted code without an explanation is not helpful

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