943,096 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2151
  • C# RSS
Mar 11th, 2010
0

Problem with DataGridView quick filter

Expand Post »
Hi Experts ,
I have a datagridview which is bound to a data source,I modify the data source and add one empty row at the beginning,this is to provide a quick search,user can click on a cell and filter the datagridview. But the problem is I am facing an exception and get to the root of it,please refer the code below

C# Syntax (Toggle Plain Text)
  1. private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  2. {
  3. UpdateEditStyle();
  4. }
  5.  
  6. private void UpdateEditStyle()
  7. {
  8. if ((dataGridView.CurrentCell.ColumnIndex == 5) && (dataGridView.CurrentCell.RowIndex == 0))
  9. {
  10. dataGridView.ReadOnly = false;
  11. dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
  12. dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
  13. }
  14. else
  15. {
  16. dataGridView.EditingControlShowing -= new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
  17. dataGridView.ReadOnly = true;
  18. }
  19. }
  20.  
  21. private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  22. {
  23. textbox = (TextBox)e.Control;
  24. textbox.TextChanged += new EventHandler(textbox_TextChanged);
  25.  
  26. }
  27.  
  28. private void textbox_TextChanged(object sender, EventArgs e)
  29. {
  30. try
  31. {
  32.  
  33. textbox.TextChanged -= new EventHandler(textbox_TextChanged);
  34.  
  35. string searchString = sender.ToString();
  36.  
  37. // remove the row
  38. if (mZeroRowDeleted == false)
  39. {
  40. DataTable tsble = (DataTable)dataGridView.DataSource;
  41. tsble.Rows.RemoveAt(0);
  42. mZeroRowDeleted = true;
  43. mEmptyRowAdded = false;
  44. dataGridView.DataSource = tsble;
  45. }
  46. UnRegisterEventsForGridView();
  47. DataTable dt = (DataTable)dataGridView.DataSource;
  48. DataView dv = ((DataTable)dataGridView.DataSource).DefaultView;
  49.  
  50. dv.RowFilter = string.Format("TextMessage Like '{0}*'", searchString);
  51.  
  52.  
  53.  
  54. }
  55. catch
  56. { }
  57. }

the exception i get is as following
text Syntax (Toggle Plain Text)
  1. System.IndexOutOfRangeException was unhandled
  2. Message="Index -1 does not have a value."
  3. Source="System.Windows.Forms"
  4. StackTrace:
  5. at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
  6. at System.Windows.Forms.CurrencyManager.get_Current()
  7. at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
  8. at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
  9. at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
  10. at System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection, Boolean forceCurrentCellSelection)
  11. at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean includeNewRow)
  12. at System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean recreateNewRow, Boolean allowSettingCurrentCell, CollectionChangeAction cca, DataGridViewRow dataGridViewRow, Int32 rowIndex)
  13. at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
  14. at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)
  15. at System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
  16. at System.Windows.Forms.DataGridView.AddNewRow(Boolean createdByEditing)
  17. at System.Windows.Forms.DataGridView.OnCurrentCellDirtyStateChanged(EventArgs e)
  18. at System.Windows.Forms.DataGridView.set_IsCurrentCellDirtyInternal(Boolean value)
  19. at System.Windows.Forms.DataGridView.NotifyCurrentCellDirty(Boolean dirty)
  20. at System.Windows.Forms.DataGridViewTextBoxEditingControl.NotifyDataGridViewOfValueChange()
  21. at System.Windows.Forms.DataGridViewTextBoxEditingControl.OnTextChanged(EventArgs e)
  22. at System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m)
  23. at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
  24. at System.Windows.Forms.TextBox.WndProc(Message& m)
  25. at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  26. at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  27. at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  28. at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  29. at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
  30. at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
  31. at System.Windows.Forms.Control.WmCommand(Message& m)
  32. at System.Windows.Forms.Control.WndProc(Message& m)
  33. at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
  34. at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  35. at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  36. at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  37. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  38. at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
  39. at System.Windows.Forms.Control.DefWndProc(Message& m)
  40. at System.Windows.Forms.Control.WmKeyChar(Message& m)
  41. at System.Windows.Forms.Control.WndProc(Message& m)
  42. at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
  43. at System.Windows.Forms.TextBox.WndProc(Message& m)
  44. at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  45. at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  46. at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  47. at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  48. at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
  49. at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  50. at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  51. at System.Windows.Forms.Application.Run(Form mainForm)
  52. at NewLogViewer.Program.Main() in D:\Snapviews\ing11560_CT_Serviceability_Dev_view\CT_SW_ServiceTools\Src\Applications\LogViewer\Prototype\NewLogViewer\Program.cs:line 17
  53. at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
  54. at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  55. at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  56. at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  57. at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  58. at System.Threading.ThreadHelper.ThreadStart()
Last edited by adatapost; Mar 11th, 2010 at 5:40 am. Reason: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SamTheDevGuru is offline Offline
5 posts
since Mar 2010
Mar 11th, 2010
0
Re: Problem with DataGridView quick filter
>I modify the data source and add one empty row at the beginning,this is to provide a quick search,user can click on a cell and filter the datagridview.

Your way to filter is not good. You have to use extra textboxes.
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008
Mar 11th, 2010
0
Re: Problem with DataGridView quick filter
Click to Expand / Collapse  Quote originally posted by adatapost ...
>I modify the data source and add one empty row at the beginning,this is to provide a quick search,user can click on a cell and filter the datagridview.

Your way to filter is not good. You have to use extra textboxes.
I am putting one textbox in the cell [column index 5], I checked with just deleting and then adding one empty row back with out any problem, the prob arrises when I try to use the filter on the dataview
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SamTheDevGuru is offline Offline
5 posts
since Mar 2010
Mar 11th, 2010
0
Re: Problem with DataGridView quick filter
Have a look at CodeProject article - http://www.codeproject.com/KB/grid/gridextensions.aspx
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008
Mar 11th, 2010
0
Re: Problem with DataGridView quick filter
Not the source of your problem, but you are using sender.ToString(). For controls that usually returns something like "System.Windows.Forms.TextBox, Text: <textbox.text here>".
Its not in your code, but im assuming textbox is a class level variable to store a reference to the textbox, you should use textbox.Text to get the string entered by the user, otherwise you will never have any matches to your filter
Last edited by Ryshad; Mar 11th, 2010 at 8:56 am.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

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: Syntax For create Procedure in SQL Server
Next Thread in C# Forum Timeline: Run a file at that date.





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


Follow us on Twitter


© 2011 DaniWeb® LLC