944,079 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2156
  • C# RSS
Oct 20th, 2009
0

Search and Select on Datagrid

Expand Post »
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
Similar Threads
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Oct 20th, 2009
0
Re: Search and Select on Datagrid
Here is an example of searching a DataGrid: How to implement a searchable DataGrid...

To select the row, of course: dataGrid1.Select(int rowId);
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 20th, 2009
0
Re: Search and Select on Datagrid
Here is a crude example I built from that link I gave:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 21st, 2009
0
Re: Search and Select on Datagrid
Thanks man that should solve my problem
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: merge sort and quick sort
Next Thread in C# Forum Timeline: getting error for this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC