Answered Question about dataGridView

  • Thursday, August 09, 2012 6:20 PM
     
     
    A- How can I paste from clipboard to datagridview
    B- How can I copy selected row to clipboard
    C- How can I remove rows which not starts with "http://"
    D- How can I remove duplicate rows

    I had 10 problems like that, I figured out 6 of them.
    I did much research about these 4 issues, but I can't find any useful information.
    Finally I realized that; dataGridView is the most hard to use control in C# visual studio.

    Help me plss. I really need help urgently. Thanks.
    • Edited by handsupsnake Thursday, August 09, 2012 6:22 PM
    • Moved by CoolDadTxMVP Friday, August 10, 2012 3:15 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Friday, August 10, 2012 3:04 PM
     
     
    Nobody helps me. Why_? You can reply to only one question of these 4 questions. Cheers
  • Friday, August 10, 2012 7:01 PM
     
     Answered Has Code
    Nobody helps me. Why_? You can reply to only one question of these 4 questions. Cheers

    Hello, in regards to A, you would obtain the type of data in the Windows ClipBoard, parse the data then place into the DataGridView. It all depends on the data in the ClipBoard.

    The example below we are looking for data from an Excel worksheet which in this case you would select a range and copy it to the Windows Clipboard. Lets say we select A1 to D5, keep things simple and there is data in this range.

    Pressing cmdRun button data is taken from the clipboard into a DataTable which is made the DataSource of a DataGridView. Pressing cmdCopyRowToClipboard button we obtain the current row in the DataGridview into a comma delimited string. Immedately the data is taken off the Clipboard and split by comma then placed into a StringBuilder object which is shown via MessageBox.Show.

    This code gives you the knowledge to do A and B.

    Public Class Form1
       Private Sub Form1_Load_1( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
          DataGridView1.AllowUserToAddRows = False
          Dim ClipboardData As IDataObject = Clipboard.GetDataObject()
          cmdRun.Enabled = False
          If Not ClipboardData Is Nothing Then
             If (ClipboardData.GetDataPresent(DataFormats.CommaSeparatedValue)) Then
                cmdRun.Enabled = True
             End If
          End If
       End Sub
       Private Sub cmdRun_Click( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles cmdRun.Click
          DataGridView1.DataSource = Nothing
          Try
             Dim ClipboardData As IDataObject = Clipboard.GetDataObject()
             If Not ClipboardData Is Nothing Then
                If (ClipboardData.GetDataPresent(DataFormats.CommaSeparatedValue)) Then
                   Dim ClipboardStream As New IO.StreamReader( _
                      CType(ClipboardData.GetData(DataFormats.CommaSeparatedValue), IO.Stream))
                   Dim FormattedData As String = ""
                   Dim Table As New DataTable With {.TableName = "ExcelData"}
                   While (ClipboardStream.Peek() > 0)
                      Dim SingleRowData As Array
                      Dim LoopCounter As Integer = 0
                      FormattedData = ClipboardStream.ReadLine()
                      SingleRowData = FormattedData.Split(",".ToCharArray)
                      If Table.Columns.Count <= 0 Then
                         For LoopCounter = 0 To SingleRowData.GetUpperBound(0)
                            Table.Columns.Add()
                         Next
                         LoopCounter = 0
                      End If
                      Dim rowNew As DataRow
                      rowNew = Table.NewRow()
                      For LoopCounter = 0 To SingleRowData.GetUpperBound(0)
                         rowNew(LoopCounter) = SingleRowData.GetValue(LoopCounter)
                      Next
                      LoopCounter = 0
                      Table.Rows.Add(rowNew)
                      rowNew = Nothing
                   End While
                   ClipboardStream.Close()
                   DataGridView1.DataSource = Table
                Else
                   MessageBox.Show("Clipboard data does not seem to be copied from Excel!")
                End If
             Else
                MessageBox.Show("Clipboard is empty!")
             End If
          Catch exp As Exception
             MessageBox.Show(exp.Message)
          End Try
       End Sub
       Private Sub cmdCopyRowToClipboard_Click( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles cmdCopyRowToClipboard.Click
          If DataGridView1.DataSource IsNot Nothing Then
             ' Set data from current row of DataGridView
             Clipboard.SetText( _
                String.Join(","c, _
                   Array.ConvertAll( _
                   ( _
                      From cell As DataGridViewCell In _
                      DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells.Cast(Of DataGridViewCell)() _
                      Select cell.Value).ToArray, Function(o) o.ToString)))
             ' Assuming the data just set is there we now split by comma as we just sent the 
             ' data comma delimited we now split by comma into a string array then for displaying
             ' the data back we place each element into a StringBuilder object.
             Dim ReturningData As String() = Clipboard.GetText.Split(",".ToCharArray)
             Dim sb As New System.Text.StringBuilder
             For Each item In ReturningData
                sb.AppendLine(item)
             Next
             MessageBox.Show(sb.ToString)
          End If
       End Sub
    End Class


    KSG

  • Sunday, August 12, 2012 9:52 PM
     
     
    sorry but I cant understand about vb.net I Know only C#. Thanks
  • Sunday, August 12, 2012 11:40 PM
     
     
    sorry but I cant understand about vb.net I Know only C#. Thanks

    Free language converters, Google search I use the 5th one down.


    KSG

  • Thursday, August 16, 2012 8:03 AM
    Moderator
     
     Answered

    Hi handsupsnake,

    About C, you can using foreach to loop the rows to get the cell's Value, convert the Value to string type. And see if the string is StartWith "http://". If not, using RemoveAt of the DataGridView.

    For D, do you mean duplicate rows are all cells having the same value of two rows? If so, I'm afraid you have to loop each row and column to compare each cell.

    http://stackoverflow.com/questions/8717898/datagridview-find-duplicate-rows-and-update-existing-data

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.