Search and Select on Datagrid

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Search and Select on Datagrid

 
0
  #1
Oct 20th, 2009
Hi All

I am having a problem on finding a solution regarding the ff problem

I need to do a search using a textbox and a datagrid, that means type in a text on the textbox and press enter and If a record is found the datagrid should point to the found record. I want to emphasize that a record has been found in the datagrid by highlighting the entire row

Can anyone help me with the code or how can I achieve that

Thanks in advance
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
Oct 20th, 2009
Here is an example of searching a DataGrid: How to implement a searchable DataGrid...

To select the row, of course: dataGrid1.Select(int rowId);
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #3
Oct 20th, 2009
Here is a crude example I built from that link I gave:

  1. public partial class Form_DataGrid : Form
  2. {
  3. DataTable dt;
  4. DataView dv;
  5. CurrencyManager CM;
  6.  
  7. public Form_DataGrid()
  8. {
  9. InitializeComponent();
  10. }
  11.  
  12. private void Form_DataGrid_Load(object sender, EventArgs e)
  13. {
  14. dt = BuildDynamicTableArrayExample();
  15. dv = new DataView(dt);
  16. dataGrid1.DataSource = dv;
  17.  
  18. dv.Sort = "FieldName1"; // sort field required for search (Find)...
  19.  
  20. // Initialize CurrencyManager to hold an instance of the form's CurrencyManager.
  21. // Needed to manipulate the row pointer...
  22. CM = (CurrencyManager)dataGrid1.BindingContext[dv];
  23. }
  24.  
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. int row = dv.Find(textBox1.Text); // find the text...
  28.  
  29. if (row >= 0)
  30. {
  31. CM.Position = row; // position the row pointer...
  32. dataGrid1.Select(row); // if wanting to highlight the row too...
  33. }
  34. }
  35.  
  36. public static DataTable BuildDynamicTableArrayExample()
  37. {
  38. // Create new DataTable object...
  39. DataTable dt = new DataTable();
  40. dt.TableName = "MyDataTable"; // Optional unless serializing datatables
  41.  
  42. // Add some columns to the table...
  43. dt.Columns.Add(new DataColumn("FieldName1", typeof(string)));
  44. dt.Columns.Add(new DataColumn("FieldName2", typeof(int)));
  45. dt.Columns.Add(new DataColumn("FieldName3", typeof(double)));
  46.  
  47. // Optional primary key field specifier...
  48. dt.Columns["FieldName1"].Unique = true;
  49.  
  50. // generate some rows of data...
  51. for (int r = 0; r < 10; r++)
  52. {
  53. // create a row object of table object's type defined above
  54. DataRow dr = dt.NewRow();
  55.  
  56. // Set each column value in the row...
  57. dr["FieldName1"] = "data " + r;
  58. dr["FieldName2"] = r;
  59. dr["FieldName3"] = (double)r;
  60.  
  61. // add the row to the table
  62. dt.Rows.Add(dr);
  63. }
  64. return dt;
  65. }
  66.  
  67. }

EDIT--NOTE: I do not UnSelect(int rowID) any rows and although it will move the row pointer every time, it will also leave the previous row highlighted.
Last edited by DdoubleD; Oct 20th, 2009 at 2:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 21st, 2009
Thanks man that should solve my problem
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC