|
when I try to use mouse left button other than right button to trigger the events, the selection region in datagridview will automatically change with the mouse movement even after the mouse left button is released
|
|
|
|
|
'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
|
|
|
|
|
Very good article!
There is just one thing that can be better... I have a datagrid with around 100 columns and when i change the scroll bars or select many cells the control is flickering, blinking?
Can this be solved? I have read about double buffering but I don´t really understand how to use it?
Thanks in advance!
|
|
|
|
|
|
Great article uini.
But i have some problem in auto fill.
I have more than 100 columns. First column has value 5 when i use the auto fill option to fill the all column but gridview horizontal scroll bar is not scroll automatically while auto fill.
Please help me anyone. Little bit urgent.
Thanks in advance.
|
|
|
|
|
Hey,
Was wondering why the program doesnt load my database data (MS Access) after i bind all it to the datagrid/data source etc etc. It only shows my respective column headers but the records are blank and only 1 line of blank records.
Regards,
Whopper
|
|
|
|
|
|
Copy/paste fails with HTML special characters. Here's a fix:
Replace this:
cellContent = cellContent.Replace("& nbsp;", " ");
With this:
cellContent = HttpUtility.HtmlDecode(cellContent);
HttpUtility requires the System.Web reference.
There are two occurences of "cellContent.Replace", just search for them in MultiEditDataGridView.cs.
|
|
|
|
|
Thanks for this nice piece of code..
I have converted the Auti fill part of the code in VB.Net for VB users.. (Datagrid is named as "dgMain")
Private m_AutoFillRowStartIndex As Integer
Private m_AutoFillColumnStartIndex As Integer
Private m_AutoFillRowEndIndex As Integer
Private m_AutoFillColumnEndIndex As Integer
Private m_AutoFillMultipleCells As Boolean
Private m_AutoFillRowStartIndex2 As Integer
Private m_AutoFillColumnStartIndex2 As Integer
Private Sub dgMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgMain.MouseDown
Dim hitTest As DataGridView.HitTestInfo = dgMain.HitTest(e.X, e.Y)
If hitTest.Type = DataGridViewHitTestType.Cell And e.Button = MouseButtons.Right Then
Dim DragSize As Size = SystemInformation.DragSize
If dgMain.SelectedCells.Count > 1 Then
m_AutoFillMultipleCells = True
Dim pCell As DataGridViewCell
For Each pCell In dgMain.SelectedCells
If m_AutoFillRowStartIndex = -1 Or pCell.RowIndex <= m_AutoFillRowStartIndex Then
m_AutoFillRowStartIndex = pCell.RowIndex
End If
If m_AutoFillRowStartIndex2 = -1 Or pCell.RowIndex >= m_AutoFillRowStartIndex Then
m_AutoFillRowStartIndex2 = pCell.RowIndex
End If
If m_AutoFillColumnStartIndex = -1 Or pCell.ColumnIndex <= m_AutoFillColumnStartIndex Then
m_AutoFillColumnStartIndex = pCell.ColumnIndex
End If
If m_AutoFillColumnStartIndex2 = -1 Or pCell.ColumnIndex >= m_AutoFillColumnStartIndex2 Then
m_AutoFillColumnStartIndex2 = pCell.ColumnIndex
End If
m_AutoFillRowEndIndex = m_AutoFillRowStartIndex2
m_AutoFillColumnEndIndex = m_AutoFillColumnStartIndex2
Next
Else
m_AutoFillMultipleCells = False
m_AutoFillRowStartIndex = hitTest.RowIndex
m_AutoFillColumnStartIndex = hitTest.ColumnIndex
m_AutoFillRowEndIndex = hitTest.RowIndex
m_AutoFillColumnEndIndex = hitTest.ColumnIndex
End If
dgMain.DoDragDrop(dgMain.Rows(hitTest.RowIndex).Cells(hitTest.ColumnIndex), DragDropEffects.Copy)
Else
m_AutoFillMultipleCells = False
m_AutoFillRowStartIndex = -1
m_AutoFillColumnStartIndex = -1
m_AutoFillRowEndIndex = -1
m_AutoFillColumnEndIndex = -1
m_AutoFillColumnStartIndex2 = -1
m_AutoFillRowStartIndex2 = -1
End If
Me.OnMouseDown(e)
End Sub
Private Sub dgMain_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgMain.DragOver
If Not m_AutoFillColumnStartIndex = -1 And Not m_AutoFillRowStartIndex = -1 Then
e.Effect = DragDropEffects.Copy
Dim MousePos As Point = dgMain.PointToClient(New Point(e.X, e.Y))
Dim Column As Integer = dgMain.HitTest(MousePos.X, MousePos.Y).ColumnIndex
Dim Row As Integer = dgMain.HitTest(MousePos.X, MousePos.Y).RowIndex
If Not Column = -1 And Not Row = -1 Then
If Row = dgMain.RowCount - 1 Then
Row = Row - 1
End If
If Not m_AutoFillMultipleCells Then
m_AutoFillColumnEndIndex = Column
m_AutoFillRowEndIndex = Row
End If
Else
m_AutoFillColumnEndIndex = Column
m_AutoFillRowEndIndex = Row
If Column > m_AutoFillColumnStartIndex2 Or Column < m_AutoFillColumnStartIndex Then
m_AutoFillRowEndIndex = m_AutoFillRowStartIndex2
ElseIf Row > m_AutoFillRowStartIndex2 Or Column < m_AutoFillRowStartIndex Then
m_AutoFillColumnEndIndex = m_AutoFillColumnStartIndex2
End If
If Row >= m_AutoFillRowStartIndex And Row <= m_AutoFillRowStartIndex2 Then
m_AutoFillRowEndIndex = m_AutoFillRowStartIndex2
End If
If Column >= m_AutoFillColumnStartIndex And Column <= m_AutoFillColumnStartIndex2 Then
m_AutoFillColumnEndIndex = m_AutoFillColumnStartIndex2
End If
End If
dgMain.Invalidate()
End If
Me.OnDragOver(e)
End Sub
Private Sub dgMain_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgMain.DragDrop
If Not m_AutoFillMultipleCells Then
If Not m_AutoFillColumnStartIndex = -1 And Not m_AutoFillRowStartIndex = -1 And Not m_AutoFillColumnEndIndex = -1 And Not m_AutoFillRowEndIndex = -1 Then
Dim StartRow, EndRow, StartColumn, EndColumn As Integer
Dim SourceCellValue As Object = dgMain.Rows(m_AutoFillRowStartIndex).Cells(m_AutoFillColumnStartIndex).Value
If m_AutoFillColumnStartIndex > m_AutoFillColumnEndIndex Then
StartColumn = m_AutoFillColumnEndIndex
EndColumn = m_AutoFillColumnStartIndex
Else
StartColumn = m_AutoFillColumnStartIndex
EndColumn = m_AutoFillColumnEndIndex
End If
If m_AutoFillRowStartIndex > m_AutoFillRowEndIndex Then
StartRow = m_AutoFillRowEndIndex
EndRow = m_AutoFillRowStartIndex
Else
StartRow = m_AutoFillRowStartIndex
EndRow = m_AutoFillRowEndIndex
End If
Dim i As Integer = 0
Do Until i = EndRow - StartRow + 1
Dim j As Integer = 0
Do Until j = EndColumn - StartColumn + 1
Try
If Not dgMain.Rows(StartRow + i).Cells(StartColumn + j).ReadOnly Then
dgMain.Rows(StartRow + i).Cells(StartColumn + j).Value = Convert.ChangeType(SourceCellValue, dgMain.Rows(StartRow + i).Cells(StartColumn + j).ValueType)
End If
Catch
End Try
j = j + 1
Loop
i = i + 1
Loop
Else
If Not m_AutoFillColumnStartIndex = -1 And Not m_AutoFillRowStartIndex = -1 And Not m_AutoFillColumnEndIndex = -1 And Not m_AutoFillRowEndIndex = -1 Then
If m_AutoFillRowEndIndex > m_AutoFillRowStartIndex2 Then
Dim iNumRows As Integer = m_AutoFillRowStartIndex2 - m_AutoFillRowStartIndex + 1
Dim iRows As Integer = 0
For i As Integer = m_AutoFillRowStartIndex2 + 1 To i <= m_AutoFillRowEndIndex
For j As Integer = m_AutoFillColumnStartIndex To j <= m_AutoFillColumnStartIndex2
Dim SourceCellValue As Object = dgMain.Rows(m_AutoFillRowStartIndex + iRows).Cells(j).Value
Try
If Not dgMain.Rows(i).Cells(j).ReadOnly Then
dgMain.Rows(i).Cells(j).Value = Convert.ChangeType(SourceCellValue, dgMain.Rows(i).Cells(j).ValueType)
End If
Catch
End Try
j = j + 1
Next
If iRows >= iNumRows Then
iRows = 0
End If
iRows = iRows + 1
i = i + 1
Next
ElseIf m_AutoFillRowEndIndex < m_AutoFillRowStartIndex Then
Dim iNumRows As Integer = m_AutoFillRowStartIndex2 - m_AutoFillRowStartIndex + 1
Dim iRows As Integer = 0
For i As Integer = m_AutoFillRowStartIndex - 1 To i >= m_AutoFillRowEndIndex
For j As Integer = m_AutoFillColumnStartIndex To j <= m_AutoFillColumnStartIndex2
Dim SourceCellValue As Object = dgMain.Rows(m_AutoFillRowStartIndex2 - iRows).Cells(j).Value
Try
If Not dgMain.Rows(i).Cells(j).ReadOnly Then
dgMain.Rows(i).Cells(j).Value = Convert.ChangeType(SourceCellValue, dgMain.Rows(i).Cells(j).ValueType)
End If
Catch
End Try
j = j + 1
Next
If iRows >= iNumRows Then
iRows = 0
End If
iRows = iRows + 1
i = i - 1
Next
ElseIf m_AutoFillColumnEndIndex > m_AutoFillColumnStartIndex2 Then
Dim iNumColumns As Integer = m_AutoFillColumnStartIndex2 - m_AutoFillColumnStartIndex + 1
Dim iColumns As Integer = 0
For i As Integer = m_AutoFillColumnStartIndex2 + 1 To i <= m_AutoFillColumnEndIndex
For j As Integer = m_AutoFillRowStartIndex To j <= m_AutoFillRowStartIndex2
Dim SourceCellValue As Object = dgMain.Rows(m_AutoFillColumnStartIndex + iColumns).Cells(j).Value
Try
If Not dgMain.Rows(i).Cells(j).ReadOnly Then
dgMain.Rows(i).Cells(j).Value = Convert.ChangeType(SourceCellValue, dgMain.Rows(i).Cells(j).ValueType)
End If
Catch
End Try
j = j + 1
Next
If iColumns >= iNumColumns Then
iColumns = 0
End If
iColumns = iColumns + 1
i = i + 1
Next
ElseIf m_AutoFillColumnEndIndex < m_AutoFillColumnStartIndex Then
Dim iNumColumns As Integer = m_AutoFillColumnStartIndex2 - m_AutoFillColumnStartIndex + 1
Dim iColumns As Integer = 0
For i As Integer = m_AutoFillColumnStartIndex - 1 To i >= m_AutoFillColumnEndIndex
For j As Integer = m_AutoFillRowStartIndex To j <= m_AutoFillRowStartIndex2
Dim SourceCellValue As Object = dgMain.Rows(m_AutoFillColumnStartIndex2 - iColumns).Cells(j).Value
Try
If Not dgMain.Rows(i).Cells(j).ReadOnly Then
dgMain.Rows(i).Cells(j).Value = Convert.ChangeType(SourceCellValue, dgMain.Rows(i).Cells(j).ValueType)
End If
Catch
End Try
j = j + 1
Next
If iColumns >= iNumColumns Then
iColumns = 0
End If
iColumns = iColumns + 1
i = i - 1
Next
End If
End If
End If
End If
m_AutoFillMultipleCells = False
m_AutoFillRowStartIndex = -1
m_AutoFillColumnStartIndex = -1
m_AutoFillRowEndIndex = -1
m_AutoFillColumnEndIndex = -1
m_AutoFillColumnStartIndex2 = -1
m_AutoFillRowStartIndex2 = -1
dgMain.Invalidate()
dgMain.EndEdit()
Me.OnDragDrop(e)
End Sub
Private Sub dgMain_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgMain.CellPainting
If Not m_AutoFillMultipleCells Then
If Not m_AutoFillColumnStartIndex = -1 And Not m_AutoFillRowStartIndex = -1 And Not m_AutoFillColumnEndIndex = -1 And Not m_AutoFillRowEndIndex = -1 Then
Dim pen As Pen = New Pen(Color.Red, 1)
Dim pen2 As Pen = New Pen(Color.Blue, 2)
Dim StartRectangle As Rectangle = dgMain.GetCellDisplayRectangle(m_AutoFillColumnStartIndex, m_AutoFillRowStartIndex, False)
Dim EndRectangle As Rectangle = dgMain.GetCellDisplayRectangle(m_AutoFillColumnEndIndex, m_AutoFillRowEndIndex, False)
Dim Width, Height, X, Y As Integer
If (EndRectangle.X > StartRectangle.X) Then
Width = EndRectangle.X - StartRectangle.X + EndRectangle.Width
X = StartRectangle.X
Else
Width = StartRectangle.X - EndRectangle.X + StartRectangle.Width
X = EndRectangle.X
End If
If (EndRectangle.Y > StartRectangle.Y) Then
Height = EndRectangle.Y - StartRectangle.Y + EndRectangle.Height
Y = StartRectangle.Y
Else
Height = StartRectangle.Y - EndRectangle.Y + StartRectangle.Height
Y = EndRectangle.Y
End If
Dim AutoFillRectangle As Rectangle = New Rectangle(X, Y, Width, Height)
e.Graphics.DrawRectangle(pen, AutoFillRectangle)
e.Graphics.DrawRectangle(pen2, StartRectangle)
End If
Else
If Not m_AutoFillColumnStartIndex = -1 And Not m_AutoFillRowStartIndex = -1 And Not m_AutoFillColumnEndIndex = -1 And Not m_AutoFillRowEndIndex = -1 Then
Dim pen As Pen = New Pen(Color.Red, 1)
Dim pen2 As Pen = New Pen(Color.Blue, 2)
Dim StartRectangle As Rectangle = dgMain.GetCellDisplayRectangle(m_AutoFillColumnStartIndex, m_AutoFillRowStartIndex, False)
Dim StartRectangle2 As Rectangle = dgMain.GetCellDisplayRectangle(m_AutoFillColumnStartIndex2, m_AutoFillRowStartIndex2, False)
Dim EndRectangle As Rectangle = dgMain.GetCellDisplayRectangle(m_AutoFillColumnEndIndex, m_AutoFillRowEndIndex, False)
Dim Width, Height, X, Y As Integer
If (EndRectangle.X > StartRectangle.X) Then
Width = EndRectangle.X - StartRectangle.X + EndRectangle.Width
X = StartRectangle.X
Else
Width = StartRectangle.X - EndRectangle.X + StartRectangle.Width
X = EndRectangle.X
End If
If (EndRectangle.Y > StartRectangle.Y) Then
Height = EndRectangle.Y - StartRectangle.Y + EndRectangle.Height
Y = StartRectangle.Y
Else
Height = StartRectangle.Y - EndRectangle.Y + StartRectangle.Height
Y = EndRectangle.Y
End If
Dim AutoFillRectangle As Rectangle = New Rectangle(X, Y, Width, Height)
e.Graphics.DrawRectangle(pen, AutoFillRectangle)
e.Graphics.DrawRectangle(pen2, New Rectangle(StartRectangle.X, StartRectangle.Y, StartRectangle2.X - StartRectangle.X + StartRectangle2.Width, StartRectangle2.Y - StartRectangle.Y + StartRectangle2.Height))
End If
End If
End Sub
This is working perfect.
Thanks
Rakesh Sharma
|
|
|
|
|
Good Job, thanks for sharing.
|
|
|
|
|
When pasting you need to start the cell in the correct place
int startRow = templateGridCtl.Rows.Count;
int startColumn = templateGridCtl.Columns.Count;
foreach (DataGridViewCell cell in templateGridCtl.SelectedCells)
{
startRow = Math.Min(startRow, cell.RowIndex);
startColumn = Math.Min(startColumn, cell.ColumnIndex);
}
|
|
|
|
|
I still have the text goes on single line, even though I expand the row height.
Did I get something wrong?
|
|
|
|
|
Hai
Thanks for posting.
It is really good work.I was searching for long time.
I have a question.
Whenever copy one row and paste it in last row,it adds one more row.
For my work , it should not add new row .I try to change in code.But i could not.i don't know C#.
Can u help to get rid of this problem?
Nitha
|
|
|
|
|
set allowUsertoAddRows= false in gridview property
|
|
|
|
|
Hi,
I cannot thank you enough for the Excel-like datagrid behaviour and find and replace form. It has saved me tons of work. I really appreciate your efforts and ingenuity in solving this.
Thanks
Ashfaq
|
|
|
|
|
|
good work,I will try the work.
|
|
|
|
|
Thanks for posting this! Happened to be exactly what I was needing atm.
Regards
David
|
|
|
|
|
No problem! Glad you found it useful
|
|
|
|
|