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

private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateEditStyle();
        }

        private void UpdateEditStyle()
        {
            if ((dataGridView.CurrentCell.ColumnIndex == 5) && (dataGridView.CurrentCell.RowIndex == 0))
            {
                dataGridView.ReadOnly = false;
                dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
                dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
            }
            else
            {
                dataGridView.EditingControlShowing -= new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
                dataGridView.ReadOnly = true;
            }
        }

        private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            textbox = (TextBox)e.Control;
            textbox.TextChanged += new EventHandler(textbox_TextChanged);
            
        }

        private void textbox_TextChanged(object sender, EventArgs e)
        {
            try
            {

                textbox.TextChanged -= new EventHandler(textbox_TextChanged);
               
                string searchString = sender.ToString();

                // remove the row 
                if (mZeroRowDeleted == false)
                {
                    DataTable tsble = (DataTable)dataGridView.DataSource;
                    tsble.Rows.RemoveAt(0);
                    mZeroRowDeleted = true;
                    mEmptyRowAdded = false;
                    dataGridView.DataSource = tsble;
                }
                UnRegisterEventsForGridView();
                DataTable dt = (DataTable)dataGridView.DataSource;
                DataView dv = ((DataTable)dataGridView.DataSource).DefaultView;

                dv.RowFilter = string.Format("TextMessage Like '{0}*'", searchString);
                
               
                
            }
            catch
            { }
        }

the exception i get is as following

System.IndexOutOfRangeException was unhandled
  Message="Index -1 does not have a value."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
       at System.Windows.Forms.CurrencyManager.get_Current()
       at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
       at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
       at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
       at System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection, Boolean forceCurrentCellSelection)
       at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean includeNewRow)
       at System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean recreateNewRow, Boolean allowSettingCurrentCell, CollectionChangeAction cca, DataGridViewRow dataGridViewRow, Int32 rowIndex)
       at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
       at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)
       at System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
       at System.Windows.Forms.DataGridView.AddNewRow(Boolean createdByEditing)
       at System.Windows.Forms.DataGridView.OnCurrentCellDirtyStateChanged(EventArgs e)
       at System.Windows.Forms.DataGridView.set_IsCurrentCellDirtyInternal(Boolean value)
       at System.Windows.Forms.DataGridView.NotifyCurrentCellDirty(Boolean dirty)
       at System.Windows.Forms.DataGridViewTextBoxEditingControl.NotifyDataGridViewOfValueChange()
       at System.Windows.Forms.DataGridViewTextBoxEditingControl.OnTextChanged(EventArgs e)
       at System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m)
       at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
       at System.Windows.Forms.TextBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmKeyChar(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
       at System.Windows.Forms.TextBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at NewLogViewer.Program.Main() in D:\Snapviews\ing11560_CT_Serviceability_Dev_view\CT_SW_ServiceTools\Src\Applications\LogViewer\Prototype\NewLogViewer\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Recommended Answers

All 4 Replies

>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 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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.