PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 How to print all the columns of a selected row in datagrid view?-VBForums
Results 1 to 3 of 3

Thread: How to print all the columns of a selected row in datagrid view?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    How to print all the columns of a selected row in datagrid view?

    Hi,

    I am new to VB. I have several tables (like table 1,2,3 and such) with different number of columns in each table.
    I am trying to write a code to print ALL the columns of a selected row in datagrid view as a string to a log file.
    I want to keep the code minimal without having to modify it for each table.

    Here is what I have so far..

    Code:
     Dim row1 As Integer = (DataGridView1.CurrentCell.RowIndex)
                        Dim col1 As Integer = (DataGridView1.CurrentCell.ColumnIndex)
                        Dim X As String = ComboBox1.Text
    
                        Dim filename As String = "C:\Log Files" & "\Logfile.txt"
                        Dim data1 As String = " "
                        Dim columncount As Integer = dt.Columns.Count
                        For i As Integer = 0 To columncount
                            data1 = DataGridView1.SelectedRows(0).Cells(i).Value.ToString                    
                            i = i + 1
                       Next
                        sw.WriteLine(Now() & " " & My.Computer.Name & " " & My.User.Name & " " & ComboBox1.Text 
                        & " Where Updated ID = " & txtID.Text & " " & data1)
    This code fails to return the required result. Can someone pls help?

    Thanks!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,947

    Re: How to print all the columns of a selected row in datagrid view?

    You can use the String.Join method to combine values from a list into a single String with delimiters between them, e.g.
    vb.net Code:
    1. Dim fields = String.Join(", ", DataGridView1.SelectedRows(0).Cells.Cast(Of DataGridViewCell)().Select(Function(dgvc) dgvc.Value))
    Also, don't use concatenation when calling WriteLine when WriteLine provides its own composite formatting:
    vb.net Code:
    1. sw.WriteLine("{0} {1} {2}", Date.Now, My.Computer.Name, My.User.Name)

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    Resolved Re: How to print all the columns of a selected row in datagrid view?

    Quote Originally Posted by jmcilhinney View Post
    You can use the String.Join method to combine values from a list into a single String with delimiters between them, e.g.
    vb.net Code:
    1. Dim fields = String.Join(", ", DataGridView1.SelectedRows(0).Cells.Cast(Of DataGridViewCell)().Select(Function(dgvc) dgvc.Value))
    This works great. This is what I wanted..
    Thanks jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width