Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a search button on a form

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
I have a form that is used for quoting and I would like to be able to search for a specific quote number using a button but I can't seem to get it to change the records on the form. Here is my code. Thanks in advance.
private void searchBN_Click(object sender, EventArgs e)
{
string searchFor = estsearchnoTB.Text;
int results = 0;
DataRow[] returnedRows;
returnedRows = estimatorDataSet.Tables["QuoteCalcHeader"].Select("estimateno = '" + searchFor + "'");
results = returnedRows.Length;
if (results > 0)
{
//RECORD FOUND
DataRow dr1;
dr1 = returnedRows[0];



}
else
{
MessageBox.Show("No such Record");
}


}
 
Now that you have the DataRow you want, just set the text property of your controls. The DataRow is like an object[] so you need to access each item using the indexer. Please read MSDN for DataRow class.
 
I have many fields on this form do I have to call each one out and assign it or is there a quicker way.
 
you will have to build the search. while you will have to write the code to do this, there are ways to minimize the amount of client code you need to write.

some examples:
Code:
public IEnumerable<DataRow> find_rows_containing(object value)
{
   //rows are passed into the ctor
   foreach(var row in rows)
   {
      if(row["column a"] == value) yield return row;
      if(row["column b"] == value) yield return row;
      ....
   }
}
Code:
public class Builder
{
   private readonly List<string> filters;

   public Builder(List<string> filters)
   {
       this.filters = filters;
   }

   public Builder for_column_a(object value)
   {
       filters.Add("column_a = " + value);
       return new Builder(filters);
   }

   public Builder for_column_b(object value)
   {
       filters.Add("column_b = " + value);
       return new Builder(filters);
   }

   public string build()
   {
      return string.Join(" and ", filters.ToArray()); 
   }
}
and then there are more advanced techniques, but that's beyond the scope of this post.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top