Click here to Skip to main content
15,886,806 members
Articles / Programming Languages / C#

Multiple Cell Edit Custom DataGridView

Rate me:
Please Sign up or sign in to vote.
4.85/5 (21 votes)
22 Jun 2009CPOL2 min read 149.5K   10.7K   112   19
Custom DataGridView with multi-cell copy/paste and Excel autofill drag and drop.

Introduction

This is a custom DataGridView that has some of Excel's useful behaviours. It features auto-fill, multiple cell copy and paste, and find and replace.

This custom DataGridView has been used in my applications that have both DataTable or BindingList as data binding sources, or with no data binding.

Using the code

MultiEditDataGridViewPanel

MultiEditDataGridViewPanel is a panel that contains a MultiEditDataGridView. It has a text bar on the top of the DataGridView for a cell editing bar similar to Excel's formula bar, as shown below:

datagridview_normal.jpg

The code snippet for the cell editing bar is shown below:

C#
/// <summary>
/// MultiEditDataGridViewPanel is a panel that contains a MultiEditDataGridView.
/// It has a text bar on the top of the data grid view,
/// for a cell editing bar similar to excel's formula bar,
/// </summary>
public partial class MultiEditDataGridViewPanel : UserControl
{
    public MultiEditDataGridViewPanel()
    {
        InitializeComponent();
    }

    /// <summary>
    /// When the current cell change, bind the current cell's value
    /// to CellTextBox (the cell editing bar) 
    /// so that updates to the CellTextBox will update the current cell values.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void DataGridView_CurrentCellChanged(object sender, System.EventArgs e)
    {
        CellTextBox.DataBindings.Clear();
        if (DataGridView.CurrentCell != null)
        {                
            CellTextBox.DataBindings.Add("Text", 
              DataGridView.CurrentCell, "Value", true, 
              DataSourceUpdateMode.OnPropertyChanged);
            CellTextBox.ReadOnly = DataGridView.CurrentCell.ReadOnly;            
        }
    }

    /// <summary>
    /// When the editing control is showing, add a listener
    /// to the control's text changed event,
    /// to update the CellTextBox when the current cell changes.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void DataGridView_EditingControlShowing(object sender, 
         System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
    {
        if ( DataGridView.CurrentCell != null )
        {
            e.Control.TextChanged += 
               new EventHandler(CurrentCell_TextChanged);
        }
    }

    /// <summary>
    /// Triggered when the current cell changes through the editing control
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void CurrentCell_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        CellTextBox.Text = tb.Text;
    }
}

MultiEditDataGridView

MultiEditDataGridView is the custom DataGridView with multiple cell copy/pasting and auto-filling functionality. It is optional to use MultiEditDataGridView by itself or embedded in MultiEditDataGridViewPanel.

Multiple Cell Copy/Paste and Clear

Multiple cells copy and paste across applications or within the DataGridView by simply using Ctrl-C and Ctrl-V; hitting Delete on selected cells will restore the cell to its default cell value. If appropriate, it will copy and paste data from the clipboard with "HTML format" so that the rows are intact (no extra rows are pasted resulting from new lines in multi-line cells). A listener to the DataGridView's PreviewKeyDown event is used to do this:

C#
/// <summary>
/// Capture Ctrl+C, Ctrl+V, and delete for multiple cell copy and paste
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MultiEditDataGridView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // Delete, restore cell's default row value
    // If you have a custom cell, override object DefaultNewRowValue { get; }
    if (e.KeyCode == Keys.Delete)
    {
        if (SelectedCells.Count > 0)
        {
            foreach (DataGridViewCell cell in SelectedCells)
            {
                cell.Value = cell.DefaultNewRowValue;
            }
        }
    }
    // Copy
    if (e.Control && e.KeyCode == Keys.C)
    {
        DataObject d = this.GetClipboardContent();                
        Clipboard.SetDataObject(d);                
    }
    // Paste
    else if (e.Control && e.KeyCode == Keys.V)
    {
        // Try to process as html format (data from excel) since
        // it keeps the row information intact, instead of assuming
        // a new row for every new line if we just process it as text
        String HtmlFormat = Clipboard.GetData("HTML Format") as String;
        List<List<string>> rowContents = new List<List<string>>();
        if (HtmlFormat != null)
        {
            // Remove html tags to just extract row information
            // and store it in rowContents
            System.Text.RegularExpressions.Regex TRregex = 
              new System.Text.RegularExpressions.Regex(@"<( )*tr([^>])*>", 
              System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex TDregex = 
              new System.Text.RegularExpressions.Regex(@"<( )*td([^>])*>", 
              System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match trMatch = TRregex.Match(HtmlFormat);
            while (!String.IsNullOrEmpty(trMatch.Value))
            {
                int rowStart = trMatch.Index + trMatch.Length;
                int rowEnd = HtmlFormat.IndexOf("</tr>", rowStart, 
                             StringComparison.InvariantCultureIgnoreCase);
                System.Text.RegularExpressions.Match tdMatch = 
                       TDregex.Match(HtmlFormat, rowStart, rowEnd - rowStart );
                List<string> rowContent = new List<string>();
                while ( !String.IsNullOrEmpty(tdMatch.Value) )
                {
                    int cellStart = tdMatch.Index + tdMatch.Length;
                    int cellEnd = HtmlFormat.IndexOf("</td>", 
                        cellStart, StringComparison.InvariantCultureIgnoreCase);
                    String cellContent = 
                        HtmlFormat.Substring(cellStart, cellEnd - cellStart);
                    cellContent = System.Text.RegularExpressions.Regex.Replace(cellContent, 
                        @"<( )*br( )*>", "\r\n", 
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    cellContent = System.Text.RegularExpressions.Regex.Replace(cellContent, 
                        @"<( )*li( )*>", "\r\n", 
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    cellContent = System.Text.RegularExpressions.Regex.Replace(cellContent, 
                        @"<( )*div([^>])*>", "\r\n\r\n", 
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    cellContent = System.Text.RegularExpressions.Regex.Replace(cellContent, 
                        @"<( )*p([^>])*>", "\r\n\r\n", 
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    cellContent = cellContent.Replace(" "," ");
                    rowContent.Add(cellContent);
                    tdMatch = tdMatch.NextMatch();
                }
                if (rowContent.Count > 0)
                {
                    rowContents.Add(rowContent);
                }
                trMatch = trMatch.NextMatch();
            }
        }
        else
        {
            // Clipboard is not in html format, read as text
            String CopiedText = Clipboard.GetText();
            String[] lines = CopiedText.Split('\n');                   
            foreach (string line in lines)
            {                        
                List<string> rowContent = 
                      new List<string>(line.Split('\t'));
                if (rowContent.Count > 0)
                {
                    rowContents.Add(rowContent);
                }
            }
        }
        int iRow = this.CurrentCell.RowIndex;
        // DataGridView's rowCount has one extra row (the temporary new row)
        if (iRow + rowContents.Count > this.Rows.Count - 1)
        {
            int iNumNewRows = iRow + rowContents.Count - this.Rows.Count + 1;
            // Simply add to a new row to the datagridview
            // if it is not binded to a datasource
            if (this.DataSource == null)
            {
                this.Rows.Add(iNumNewRows);
            }
            // Otherwise, add rows to binded data source
            else
            {
                try
                {                            
                    BindingSource bindingSource = this.DataSource as BindingSource;
                    if (bindingSource != null)
                    {
                        // This is important!!  
                        // Cancel Edit before adding new entries into bindingsource
                        // If the UI is currently adding a new line
                        // (you have your cursor on the last time)
                        // You will System.InvalidOperationException
                        bindingSource.CancelEdit();
                        for (int i = 0; i < iNumNewRows; i++)
                        {
                            Object obj = bindingSource.AddNew();
                        }
                    }
                }
                catch
                {
                    // failed adding row to binding data source
                    // It was okay for my application to ignore the error
                }
            }
        }
        // paste the data starting at the current cell
        foreach ( List<String> rowContent in rowContents)
        {
            int iCol = this.CurrentCell.ColumnIndex;
            foreach (String cellContent in rowContent)
            {
                try
                {
                    if (iCol < this.Columns.Count)
                    {
                        DataGridViewCell cell = this[iCol, iRow];
                        if ( !cell.ReadOnly )
                        {
                            cell.Value = Convert.ChangeType(cellContent, cell.ValueType);
                        }
                    }
                }
                catch
                {

                }
                iCol++;
            }
            iRow++;
            if (iRow >= this.Rows.Count)
            {
                break;
            }
        }
    }
}

Auto-Filling

The image below shows auto-filling a selected cell across the columns. This can be done by right-clicking on the selected cells, then drag and drop.

datagridview_autofill1.jpg

The image below shows auto-filling the selected cells across the columns. This can be done by right-clicking on the selected cells, then drag and drop.

datagridview_autofill2.jpg

The implementation of auto filling is fairly simple. The starting row and column indexes is stored on OnMouseDown and the drag and drop begins. The ending row and column indexes are updated OnDragOver. In OnDragDrop, I loop through the cells within the red box and copy the template cells' values to them. OnCellPainting is overridden to display the red and blue boxes during the drag and drop operation.

FindAndReplaceForm

FindAndReplaceForm is a form that contains find and replace functionality that works with any DataGridView. The form is shown in the image below:

datagridview_findandreplace.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwhat about the mouse left button Pin
Chandler Gao29-Dec-16 4:11
Chandler Gao29-Dec-16 4:11 
Questionfind and replace form by vb.net & you very poor programmer because you didn't answer for anyone and you Failure Pin
ahamedgad19-Jan-14 8:33
ahamedgad19-Jan-14 8:33 
'you very poor programmer because you didn't answer for anyone and you Failure


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Public Class FindAndReplaceForm
Private m_DataGridView As DataGridView

Private m_SearchStartRow As Integer

Private m_SearchStartColumn As Integer

Private m_SearchSelectionIndex As Integer

Private m_SelectedCells As List(Of DataGridViewCell)


Public Sub New(ByVal datagridview As DataGridView)
MyBase.New()
m_SelectedCells = New List(Of DataGridViewCell)
InitializeComponent()
InitializeForm(datagridview)
FindWhatTextBox1.Text = String.Empty
FindWhatTextBox2.Text = String.Empty

End Sub

Public Sub InitializeForm(ByVal datagridview As DataGridView)
' If (m_DataGridView <> datagridview) Then
m_DataGridView = datagridview

If (Not (m_DataGridView) Is Nothing) Then
'm_DataGridView.MouseClick = (m_DataGridView.MouseClick - DataGridView_MouseClick())
'End If
m_DataGridView = datagridview
AddHandler m_DataGridView.MouseClick, AddressOf Me.DataGridView_MouseClick
AddHandler m_DataGridView.SelectionChanged, AddressOf Me.DataGridView_SelectionChanged
End If
If (m_DataGridView.SelectedCells.Count > 1) Then
Me.LookInComboBox1.SelectedIndex = 1
Else
Me.LookInComboBox1.SelectedIndex = 0
End If
If (Not (m_DataGridView.CurrentCell) Is Nothing) Then
m_SearchStartRow = m_DataGridView.CurrentCell.RowIndex
m_SearchStartColumn = m_DataGridView.CurrentCell.ColumnIndex
If (Not (m_DataGridView.CurrentCell.Value) Is Nothing) Then
Me.FindWhatTextBox1.Text = m_DataGridView.CurrentCell.Value.ToString
End If
End If
SelectionCellsChanged()
End Sub

Private Sub DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
SelectionCellsChanged()
End Sub

Private Sub SelectionCellsChanged()
m_SearchSelectionIndex = 0
m_SelectedCells.Clear()
For Each cell As DataGridViewCell In m_DataGridView.SelectedCells
m_SelectedCells.Add(cell)
Next
End Sub

Private Sub DataGridView_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
If (e.Button = MouseButtons.Left) Then
Dim hitTest As DataGridView.HitTestInfo = m_DataGridView.HitTest(e.X, e.Y)
If (hitTest.Type = DataGridViewHitTestType.Cell) Then
m_SearchStartRow = hitTest.RowIndex
m_SearchStartColumn = hitTest.ColumnIndex
End If
End If
End Sub






Private Function FindAndReplaceInSelection(ByVal bReplace As Boolean, ByVal bStopOnFind As Boolean, ByVal replaceString As String) As DataGridViewCell
' Search criterions
Dim sFindWhat As String = Me.FindWhatTextBox1.Text
Dim bMatchCase As Boolean = Me.MatchCaseCheckBox1.Checked
Dim bMatchCell As Boolean = Me.MatchCellCheckBox1.Checked
Dim bSearchUp As Boolean = Me.SearchUpCheckBox1.Checked
Dim iSearchMethod As Integer = -1
' No regular repression or wildcard
If Me.UseCheckBox1.Checked Then
iSearchMethod = Me.UseComboBox1.SelectedIndex
End If
' Start of search
Dim iSearchIndex As Integer = m_SearchSelectionIndex
If bSearchUp Then
iSearchIndex = (m_SelectedCells.Count _
- (m_SearchSelectionIndex - 1))
End If

While (m_SearchSelectionIndex < m_SelectedCells.Count)
m_SearchSelectionIndex = (m_SearchSelectionIndex + 1)
' Search end of search
Dim FindCell As DataGridViewCell = Nothing
If FindAndReplaceString(bReplace, m_SelectedCells(iSearchIndex), sFindWhat, replaceString, bMatchCase, bMatchCell, iSearchMethod) Then
FindCell = m_SelectedCells(iSearchIndex)
End If
If (bStopOnFind _
AndAlso (Not (FindCell) Is Nothing)) Then
If (m_SearchSelectionIndex >= m_SelectedCells.Count) Then
m_SearchSelectionIndex = 0
End If
Return FindCell
End If
If bSearchUp Then
iSearchIndex = (m_SelectedCells.Count _
- (m_SearchSelectionIndex - 1))
Else
iSearchIndex = m_SearchSelectionIndex
End If

End While
If (m_SearchSelectionIndex >= m_SelectedCells.Count) Then
m_SearchSelectionIndex = 0
End If
Return Nothing
End Function

Private Function FindAndReplaceInTable(ByVal bReplace As Boolean, ByVal bStopOnFind As Boolean, ByVal replaceString As String) As DataGridViewCell
If (m_DataGridView.CurrentCell Is Nothing) Then
Return Nothing
End If
' Search criterions
Dim sFindWhat As String = Me.FindWhatTextBox1.Text
Dim bMatchCase As Boolean = Me.MatchCaseCheckBox1.Checked
Dim bMatchCell As Boolean = Me.MatchCellCheckBox1.Checked
Dim bSearchUp As Boolean = Me.SearchUpCheckBox1.Checked
Dim iSearchMethod As Integer = -1
' No regular repression or wildcard
If Me.UseCheckBox1.Checked Then
iSearchMethod = Me.UseComboBox1.SelectedIndex
End If
' Start of search
Dim iSearchStartRow As Integer = m_DataGridView.CurrentCell.RowIndex
Dim iSearchStartColumn As Integer = m_DataGridView.CurrentCell.ColumnIndex
Dim iRowIndex As Integer = m_DataGridView.CurrentCell.RowIndex
Dim iColIndex As Integer = m_DataGridView.CurrentCell.ColumnIndex
If bSearchUp Then
iColIndex = (iColIndex - 1)
If (iColIndex < 0) Then
iColIndex = (m_DataGridView.ColumnCount - 1)
iRowIndex = (iRowIndex - 1)
End If
Else
iColIndex = (iColIndex + 1)
If (iColIndex >= m_DataGridView.ColumnCount) Then
iColIndex = 0
iRowIndex = (iRowIndex + 1)
End If
End If
If (iRowIndex >= m_DataGridView.RowCount) Then
iRowIndex = 0
ElseIf (iRowIndex < 0) Then
iRowIndex = (m_DataGridView.RowCount - 1)
End If

While Not ((iRowIndex = iSearchStartRow) _
AndAlso (iColIndex = iSearchStartColumn))
' Search end of search
Dim FindCell As DataGridViewCell = Nothing
If FindAndReplaceString(bReplace, m_DataGridView(iColIndex, iRowIndex), sFindWhat, replaceString, bMatchCase, bMatchCell, iSearchMethod) Then
FindCell = m_DataGridView(iColIndex, iRowIndex)
End If
If (bStopOnFind _
AndAlso (Not (FindCell) Is Nothing)) Then
Return FindCell
End If
If bSearchUp Then
iColIndex = (iColIndex - 1)
Else
iColIndex = (iColIndex + 1)
End If
If (iColIndex >= m_DataGridView.ColumnCount) Then
iColIndex = 0
iRowIndex = (iRowIndex + 1)
ElseIf (iColIndex < 0) Then
iColIndex = (m_DataGridView.ColumnCount - 1)
iRowIndex = (iRowIndex - 1)
End If
If (iRowIndex >= m_DataGridView.RowCount) Then
iRowIndex = 0
ElseIf (iRowIndex < 0) Then
iRowIndex = (m_DataGridView.RowCount - 1)
End If

End While
If FindAndReplaceString(bReplace, m_DataGridView(iColIndex, iRowIndex), sFindWhat, replaceString, bMatchCase, bMatchCell, iSearchMethod) Then
Return m_DataGridView(iColIndex, iRowIndex)
End If
Return Nothing
End Function

Private Function FindAndReplaceInColumnHeader() As DataGridViewCell
' Search parameters
Dim sFindWhat As String = Me.FindWhatTextBox1.Text
Dim bMatchCase As Boolean = Me.MatchCaseCheckBox1.Checked
Dim bMatchCell As Boolean = Me.MatchCellCheckBox1.Checked
Dim bSearchUp As Boolean = Me.SearchUpCheckBox1.Checked
Dim iSearchMethod As Integer = -1
' No regular repression or wildcard
If Me.UseCheckBox1.Checked Then
iSearchMethod = Me.UseComboBox1.SelectedIndex
End If
' Start of search
Dim iSearchStartColumn As Integer = m_DataGridView.CurrentCell.ColumnIndex
Dim iColIndex As Integer = m_DataGridView.CurrentCell.ColumnIndex
' search one cell back
If bSearchUp Then
iColIndex = (iColIndex - 1)
If (iColIndex < 0) Then
iColIndex = (m_DataGridView.ColumnCount - 1)
End If
Else
iColIndex = (iColIndex + 1)
If (iColIndex >= m_DataGridView.ColumnCount) Then
iColIndex = 0
End If
End If

While Not (iColIndex = iSearchStartColumn)
' Search end of search
Dim FindCell As DataGridViewCell = Nothing
If FindString(m_DataGridView.Columns(iColIndex).Name, sFindWhat, bMatchCase, bMatchCell, iSearchMethod) Then
FindCell = m_DataGridView(iColIndex, 0)
Return FindCell
End If
If bSearchUp Then
iColIndex = (iColIndex - 1)
Else
iColIndex = (iColIndex + 1)
End If
' Search down
If (iColIndex >= m_DataGridView.ColumnCount) Then
iColIndex = 0
End If
' Search up
If (iColIndex < 0) Then
iColIndex = (m_DataGridView.ColumnCount - 1)
End If

End While
Return Nothing
End Function

Private Function FindString(ByVal SearchString As String, ByVal FindStringvar As String, ByVal bMatchCase As Boolean, ByVal bMatchCell As Boolean, ByVal iSearchMethod As Integer) As Boolean
' Regular string search
If (iSearchMethod = -1) Then
' Match Cell
If bMatchCell Then
If Not bMatchCase Then
If (SearchString.ToLowerInvariant = FindStringvar.ToLowerInvariant) Then
Return True
End If
ElseIf (SearchString = FindStringvar) Then
Return True
End If
End If
' No Match Cell
Dim bFound As Boolean = False
Dim strCompare As StringComparison = StringComparison.InvariantCulture
If Not bMatchCase Then
strCompare = StringComparison.InvariantCultureIgnoreCase
End If
bFound = (SearchString.IndexOf(FindStringvar, 0, strCompare) <> -1)
Return bFound
Else
' Regular Expression
Dim RegexPattern As String = FindStringvar
' Wildcards
If (iSearchMethod = 1) Then
' Convert wildcard to regex:
RegexPattern = ("^" _
+ (System.Text.RegularExpressions.Regex.Escape(FindStringvar).Replace("\\*", ".*").Replace("\\?", ".") + "$"))
End If
Dim strCompare As System.Text.RegularExpressions.RegexOptions = System.Text.RegularExpressions.RegexOptions.None
If Not bMatchCase Then
strCompare = System.Text.RegularExpressions.RegexOptions.IgnoreCase
End If
Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(RegexPattern, strCompare)
If regex.IsMatch(SearchString) Then
Return True
End If
Return False
End If
Return False
End Function

Private Function FindAndReplaceString(ByVal bReplace As Boolean, ByVal SearchCell As DataGridViewCell, ByVal FindString As String, ByVal ReplaceString As String, ByVal bMatchCase As Boolean, ByVal bMatchCell As Boolean, ByVal iSearchMethod As Integer) As Boolean
Dim SearchString As String = SearchCell.FormattedValue.ToString
' Regular string search
If (iSearchMethod = -1) Then
' Match Cell
If bMatchCell Then
If Not bMatchCase Then
If (SearchString.ToLowerInvariant = FindString.ToLowerInvariant) Then
If bReplace Then
SearchCell.Value = Convert.ChangeType(ReplaceString, SearchCell.ValueType)
End If
Return True
End If
ElseIf (SearchString = FindString) Then
If bReplace Then
SearchCell.Value = Convert.ChangeType(ReplaceString, SearchCell.ValueType)
End If
Return True
End If
End If
' No Match Cell
Dim bFound As Boolean = False
Dim strCompare As StringComparison = StringComparison.InvariantCulture
If Not bMatchCase Then
strCompare = StringComparison.InvariantCultureIgnoreCase
End If
If bReplace Then
Dim NewString As String = Nothing
Dim strIndex As Integer = 0

While (strIndex <> -1)
Dim nextStrIndex As Integer = SearchString.IndexOf(FindString, strIndex, strCompare)
If (nextStrIndex <> -1) Then
bFound = True
NewString = (NewString + SearchString.Substring(strIndex, (nextStrIndex - strIndex)))
NewString = (NewString + ReplaceString)
nextStrIndex = (nextStrIndex + FindString.Length)
Else
NewString = (NewString + SearchString.Substring(strIndex))
End If
strIndex = nextStrIndex

End While
If bFound Then
SearchCell.Value = Convert.ChangeType(NewString, SearchCell.ValueType)
End If
Else
bFound = (SearchString.IndexOf(FindString, 0, strCompare) <> -1)
End If
Return bFound
Else
' Regular Expression
Dim RegexPattern As String = FindString
' Wildcards
If (iSearchMethod = 1) Then
' Convert wildcard to regex:
RegexPattern = ("^" _
+ (System.Text.RegularExpressions.Regex.Escape(FindString).Replace("\\*", ".*").Replace("\\?", ".") + "$"))
End If
Dim strCompare As System.Text.RegularExpressions.RegexOptions = System.Text.RegularExpressions.RegexOptions.None
If Not bMatchCase Then
strCompare = System.Text.RegularExpressions.RegexOptions.IgnoreCase
End If
Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(RegexPattern, strCompare)
If regex.IsMatch(SearchString) Then
If bReplace Then
Dim NewString As String = regex.Replace(SearchString, ReplaceString)
SearchCell.Value = Convert.ChangeType(NewString, SearchCell.ValueType)
End If
Return True
End If
Return False
End If
Return False
End Function

Private Sub FindButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton1.Click
Dim FindCell As DataGridViewCell = Nothing
If (Me.LookInComboBox1.SelectedIndex = 0) Then
FindCell = FindAndReplaceInTable(False, True, Nothing)
ElseIf (Me.LookInComboBox1.SelectedIndex = 1) Then
FindCell = FindAndReplaceInSelection(False, True, Nothing)
ElseIf (Me.LookInComboBox1.SelectedIndex = 2) Then
FindCell = FindAndReplaceInColumnHeader()
End If
If (Not (FindCell) Is Nothing) Then
Dim cells() As DataGridViewCell = m_SelectedCells.ToArray

Dim iSearchIndex As Integer = m_SearchSelectionIndex
m_DataGridView.CurrentCell = FindCell
' restore cached selection variables that was changed by setting CurrentCell
m_SearchSelectionIndex = iSearchIndex
m_SelectedCells.Clear()
m_SelectedCells.AddRange(cells)
End If
End Sub

Private Sub ReplaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceButton.Click
Dim FindCell As DataGridViewCell = Nothing
If (Me.LookInComboBox1.SelectedIndex = 0) Then
FindCell = FindAndReplaceInTable(True, True, Me.ReplaceWithTextBox.Text)
ElseIf (Me.LookInComboBox1.SelectedIndex = 1) Then
FindCell = FindAndReplaceInSelection(True, True, Me.ReplaceWithTextBox.Text)
End If
If (Not (FindCell) Is Nothing) Then
Dim cells() As DataGridViewCell = m_SelectedCells.ToArray

Dim iSearchIndex As Integer = m_SearchSelectionIndex
m_DataGridView.CurrentCell = FindCell
' restore cached selection variables that was changed by setting CurrentCell
m_SearchSelectionIndex = iSearchIndex
m_SelectedCells.Clear()
m_SelectedCells.AddRange(cells)
End If
End Sub

Private Sub ReplaceAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceAllButton.Click
Dim FindCell As DataGridViewCell = Nothing
If (Me.LookInComboBox1.SelectedIndex = 0) Then
FindCell = FindAndReplaceInTable(True, False, Me.ReplaceWithTextBox.Text)
ElseIf (Me.LookInComboBox1.SelectedIndex = 1) Then
FindCell = FindAndReplaceInSelection(True, False, Me.ReplaceWithTextBox.Text)
End If
If (Not (FindCell) Is Nothing) Then
Dim cells() As DataGridViewCell = m_SelectedCells.ToArray

Dim iSearchIndex As Integer = m_SearchSelectionIndex
m_DataGridView.CurrentCell = FindCell
' restore cached selection variables that was changed by setting CurrentCell
m_SearchSelectionIndex = iSearchIndex
m_SelectedCells.Clear()
m_SelectedCells.AddRange(cells)
End If
End Sub

Private Sub FindButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton2.Click
FindButton1_Click(sender, e)
End Sub

Private Sub UseComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UseComboBox1.SelectedIndexChanged
Me.UseComboBox2.SelectedIndex = Me.UseComboBox1.SelectedIndex
End Sub

Private Sub UseComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UseComboBox2.SelectedIndexChanged
Me.UseComboBox1.SelectedIndex = Me.UseComboBox2.SelectedIndex
End Sub

Private Sub UseCheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UseCheckBox2.CheckedChanged
Me.UseCheckBox1.Checked = Me.UseCheckBox2.Checked
If Me.UseCheckBox2.Checked Then
Me.UseComboBox2.Enabled = True
Else
Me.UseComboBox2.Enabled = False
Me.UseComboBox2.SelectedItem = Nothing
End If
End Sub

Private Sub UseCheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UseCheckBox1.CheckedChanged
Me.UseCheckBox2.Checked = Me.UseCheckBox1.Checked
If Me.UseCheckBox1.Checked Then
Me.UseComboBox1.Enabled = True
Else
Me.UseComboBox1.Enabled = False
Me.UseComboBox1.SelectedItem = Nothing
End If
End Sub

Private Sub SearchUpCheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchUpCheckBox1.CheckedChanged
Me.SearchUpCheckBox2.Checked = Me.SearchUpCheckBox1.Checked
End Sub

Private Sub SearchUpCheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchUpCheckBox2.CheckedChanged
Me.SearchUpCheckBox1.Checked = Me.SearchUpCheckBox2.Checked
End Sub

Private Sub MatchCellCheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchCellCheckBox1.CheckedChanged
Me.MatchCellCheckBox2.Checked = Me.MatchCellCheckBox1.Checked
End Sub

Private Sub MatchCellCheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchCellCheckBox2.CheckedChanged
Me.MatchCellCheckBox1.Checked = Me.MatchCellCheckBox2.Checked
End Sub

Private Sub MatchCaseCheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchCaseCheckBox1.CheckedChanged
Me.MatchCaseCheckBox2.Checked = Me.MatchCaseCheckBox1.Checked
End Sub

Private Sub MatchCaseCheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchCaseCheckBox2.CheckedChanged
Me.MatchCaseCheckBox1.Checked = Me.MatchCaseCheckBox2.Checked
End Sub

Private Sub LookInComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookInComboBox2.SelectedIndexChanged
Me.LookInComboBox1.SelectedIndex = Me.LookInComboBox2.SelectedIndex
End Sub

Private Sub LookInComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookInComboBox1.SelectedIndexChanged
Me.LookInComboBox2.SelectedIndex = Me.LookInComboBox1.SelectedIndex
End Sub

Private Sub FindWhatTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindWhatTextBox1.TextChanged
Me.FindWhatTextBox2.Text = Me.FindWhatTextBox1.Text
End Sub

Private Sub FindWhatTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindWhatTextBox2.TextChanged
Me.FindWhatTextBox1.Text = Me.FindWhatTextBox2.Text
End Sub
End Class
QuestionFlickering? Pin
olssoninc20-Aug-13 21:02
olssoninc20-Aug-13 21:02 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey27-Mar-12 21:30
professionalManoj Kumar Choubey27-Mar-12 21:30 
Questionhorizontal scroll is not working while auto-fill Pin
kamsri15322-Mar-12 2:47
kamsri15322-Mar-12 2:47 
QuestionAwesome stuff. But i got a question. Pin
whoppwhopp21-Mar-12 19:53
whoppwhopp21-Mar-12 19:53 
GeneralMy vote of 5 Pin
kamsri15320-Mar-12 3:11
kamsri15320-Mar-12 3:11 
QuestionHtmlDecode fix Pin
karxex8-Jan-12 23:03
karxex8-Jan-12 23:03 
GeneralVB .Net conversion for Auto fill Datagrid. Pin
Rakesh_Sharma232-Feb-11 20:31
Rakesh_Sharma232-Feb-11 20:31 
GeneralMy vote of 5 Pin
JunfengGuo21-Dec-10 15:06
JunfengGuo21-Dec-10 15:06 
GeneralNeed to alter the Start Row for pasting Pin
Smurf IV (Simon Coghlan)19-May-10 1:36
Smurf IV (Simon Coghlan)19-May-10 1:36 
GeneralIt doesn't work properly for me Pin
synbari2-Mar-10 9:12
professionalsynbari2-Mar-10 9:12 
GeneralHai Pin
nitharsan5-Feb-10 12:23
nitharsan5-Feb-10 12:23 
GeneralRe: Hai Pin
kamsri15320-Mar-12 3:09
kamsri15320-Mar-12 3:09 
GeneralFantastic job Pin
ash20935-Feb-10 0:50
ash20935-Feb-10 0:50 
Generalgood work~ Pin
Member 402346621-Oct-09 21:37
Member 402346621-Oct-09 21:37 
GeneralGood work Pin
hijack52022-Sep-09 19:57
hijack52022-Sep-09 19:57 
GeneralMultiple Cell Edit Custom DataGridView Pin
incognitodave24-Jul-09 8:44
incognitodave24-Jul-09 8:44 
GeneralRe: Multiple Cell Edit Custom DataGridView Pin
uini24-Jul-09 17:15
uini24-Jul-09 17:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.