none
Returning the value of an attribute from a Data Grid RRS feed

  • Question

  • I have been trying to get acquainted with the Data Grid component, but I am having some trouble returning values from every attribute.

    What I want to perform is to return the value, not the name or type, of an attribute.

    In other words, returning the value of data items per record and output it to Notepad.

    Which property or method gets and sets the value of an attribute of a table from a data grid component?

    Regards,

    JohnDBCTX


    jp

    Thursday, August 3, 2017 12:06 PM

Answers

  • I think I have it now.

    Me.<DataSet Name>.<Table Name>.Item(0).<Column Name>.ToString

    Therefore Item(0) returns the first record of the table name, which is under the Data Set.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Thursday, August 3, 2017 6:15 PM
    Thursday, August 3, 2017 6:15 PM
  •     
     	Dim bClsP as string
    
            ' Write text to a file
            Dim file As New System.IO.StreamWriter("c:\test.txt")
            bClsP= Me.Build00001DataSet.Table01.Item(0).Field01.ToString
            file.WriteLine(bClsP)
            file.Close()
    
    
    
    

    This code snippet above could be an easier task.  This is following an automated Data Set configuration wizard.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Thursday, August 3, 2017 6:28 PM
    Thursday, August 3, 2017 6:28 PM
  • I have plugged in the parameters into an example code snippet below:

           Dim x, z As Integer
            Dim bClsP, bClsBackwd As String
      
          ' Write text to a file
            Dim file As New StreamWriter("c:\test01.txt")        'Forward
            For x = 0 To Me.Build00001DataSet.BXParameters.Rows.Count - 1
                bClsP = Me.Build00001DataSet.BXParameters.Rows(x).Item("BXParameter").ToString
                file.WriteLine(bClsP)
                file.WriteLine(vbNewLine)
            Next
            'Backward
            For z = Me.Build00001DataSet.BXParameters.Rows.Count To 0 Step -1
                bClsBackwd = Me.Build00001DataSet.BXParameters.Rows(z).Item("BXParameter").ToString
                file.WriteLine(bClsBackwd)
                file.WriteLine(vbNewLine)
            Next
    
            file.Close()
    

    During runtime, this generated the output in Notepad:

    ID


    scores


    numberScores

    numberScores


    scores


    ID

    The only difference was that I had to include the data set name followed by the table name and two parameters that denote the number of rows: one for forward, and one for backward.

    I do hope this answers my question.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Friday, August 4, 2017 3:43 AM
    Friday, August 4, 2017 3:43 AM

All replies

  • John,'

    What kind of DataGrid there are hundreds of those while the one in windows Forms is seldom used (or it should be the DataGridView).


    Success
    Cor

    Thursday, August 3, 2017 12:37 PM
  • It is the Data Grid View component.

    Which libraries that are part of the Data Grid View component?

    Is it any of the following that is accessible to the Data Grid View component libraries

    as they are listed below?

    My best guess is the Data Row and the Data Column properties of the Data Table class.

    Therefore:

    Data Table class

             Data Row property

             Data Column property

    I do hope I am correct.

    Regards,

    JohnDBCTX


    jp

    Thursday, August 3, 2017 5:04 PM
  • John,

    It sounds like you're confused.

    The DataGridView control just displays the data; the source of the data is up to you entirely.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, August 3, 2017 5:36 PM
  • I have been trying to get acquainted with the Data Grid component, but I am having some trouble returning values from every attribute.

    What I want to perform is to return the value, not the name or type, of an attribute.

    In other words, returning the value of data items per record and output it to Notepad.

    Which property or method gets and sets the value of an attribute of a table from a data grid component?

    Regards,

    JohnDBCTX


    jp

    Hi

    It is very hard to understand what you may be asking for.

    My best guess is something to do with open NotePad with text from a VB.NET application.

    If so, then here is one example that may help. This example uses a DataTable, DataGridView and some buttons. As it happens, there is a 'filter' option included - just ignore if of no use to you..

    Here is the code.

    ' This example needs a Form1 with
    ' a blank DataGridView1, 
    ' TextBox1 - filter text
    ' Button1 - Filter button
    ' Button2 - Store selected row
    ' Button3 - Send stored rows to NotePad
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim tb As New TextBox
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\MyTempText.txt"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
    
                ' just some test data
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
    
            ' just some formatting of DGV
            With DataGridView1
                .DataSource = myTable
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            DataGridView1.DataSource = view
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' filter selected column with TextBox1 text
            ' if TextBox1 is empty then clear filter
            If Trim(TextBox1.Text).Length < 1 Or DataGridView1.SelectedCells.Count < 1 Then
                view.RowFilter = Nothing
                Exit Sub
            End If
            Dim c As Integer = DataGridView1.SelectedCells(0).ColumnIndex
            Dim h As String = DataGridView1.Columns(c).Name
            Dim v As String = DataGridView1.SelectedCells(0).Value.ToString
            view.RowFilter = h & "='" & TextBox1.Text & "'"
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' store selected row ready to send to NotePad
            Dim s As String = Nothing
            For Each c As DataGridViewCell In DataGridView1.CurrentRow.Cells
                tb.AppendText(c.Value.ToString.PadLeft(12))
            Next
            tb.AppendText(vbCrLf)
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            ' send stored rows to Notepad
            IO.File.WriteAllText(path, tb.Text)
            Process.Start("notepad.exe", path)
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            ' delete Temp file when closing application
            IO.File.Delete(path)
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, August 3, 2017 6:04 PM
  • I think I have it now.

    Me.<DataSet Name>.<Table Name>.Item(0).<Column Name>.ToString

    Therefore Item(0) returns the first record of the table name, which is under the Data Set.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Thursday, August 3, 2017 6:15 PM
    Thursday, August 3, 2017 6:15 PM
  • I think I have it now.

    Me.<DataSet Name>.<Table Name>.Item(0).<Column Name>.ToString

    Therefore Item(0) returns the first record of the table name, which is under the Data Set.

    Regards,

    JohnDBCTX


    jp

    That has nothing to do with a DataGridControl (or any other control for that matter).

    I'll ask that you change the title of your thread as this is bound to be confusing to others in the future. You're dealing with - and your question was about - a DataTable. A DataTable consists of DataRows and DataColumns.

    The DataSet seems to be macro to that, but it's completely optional.

    I'm glad that you have your answer.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, August 3, 2017 6:26 PM
  •     
     	Dim bClsP as string
    
            ' Write text to a file
            Dim file As New System.IO.StreamWriter("c:\test.txt")
            bClsP= Me.Build00001DataSet.Table01.Item(0).Field01.ToString
            file.WriteLine(bClsP)
            file.Close()
    
    
    
    

    This code snippet above could be an easier task.  This is following an automated Data Set configuration wizard.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Thursday, August 3, 2017 6:28 PM
    Thursday, August 3, 2017 6:28 PM
  • For the record, if you were using the data wizards and had a DataSet named SimpleDataSet with a Customer table you would access the first record as

    SimpleDataSet.Customer.First.FirstName

    Or

    Console.WriteLine(SimpleDataSet.Customer(0).FirstName)

    Set it

    SimpleDataSet.Customer(0).FirstName = "Karen"

    See all values in a comma delimited list for the first row

    Console.WriteLine(String.Join(",", SimpleDataSet.Customer.First.ItemArray))

    And there usually is a BindingSource which permits accessing the current row of a DataGridView via

    CType(SomeBindingSource,DataRowView).Row.Field(Of String)("FirstName") or

    CType(SomeBindingSource,DataRowView).Row.SetField(Of String)("FirstName","Karen")

    We can get fancy and do all row with a LINQ statement and save to say a text file.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 3, 2017 6:52 PM
    Moderator
  • Okay, it makes sense.

    Is there a property or method, which is under the Data Grid View component library for example:

    User prints the following data to Notepad to print an outline.

    1.

       a.

       b.

    2.

        a.

        b.

    3.

        a.

        b.

    Already, I have mastered that task in VB6 and Microsoft Access VBA using the similar methods:

    Move Next, Move First, Open, Close

    And the Classes

    Recordset, Open Recordset, Database

    What are the equivalent syntaxes to those four methods and those three classes as I have mentioned them above?

    This is just to broaden my knowledge in regards to Reading data from tables and Writing data to another app such as Notepad.

    Regards,

    JohnDBCTX


    jp

    Friday, August 4, 2017 12:47 AM
  • Already, I have mastered that task in VB6 and Microsoft Access VBA using the similar methods:

    Please don't use terms like 'Data Grid View component' as that has nothing to do with your task and is very confusing.

    Move Next, Move First etc are not part of .Net data management. They simply aren't relevant. See, for example:
    https://support.microsoft.com/en-us/help/310372/datatable-and-dataview-do-not-include-movefirst--movelast--movenext--a

    If you want to follow up on those features, see:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager(v=vs.100).aspx

    but for your application indexing the rows is going to be sufficient.

    Friday, August 4, 2017 1:04 AM
  • I have plugged in the parameters into an example code snippet below:

           Dim x, z As Integer
            Dim bClsP, bClsBackwd As String
      
          ' Write text to a file
            Dim file As New StreamWriter("c:\test01.txt")        'Forward
            For x = 0 To Me.Build00001DataSet.BXParameters.Rows.Count - 1
                bClsP = Me.Build00001DataSet.BXParameters.Rows(x).Item("BXParameter").ToString
                file.WriteLine(bClsP)
                file.WriteLine(vbNewLine)
            Next
            'Backward
            For z = Me.Build00001DataSet.BXParameters.Rows.Count To 0 Step -1
                bClsBackwd = Me.Build00001DataSet.BXParameters.Rows(z).Item("BXParameter").ToString
                file.WriteLine(bClsBackwd)
                file.WriteLine(vbNewLine)
            Next
    
            file.Close()
    

    During runtime, this generated the output in Notepad:

    ID


    scores


    numberScores

    numberScores


    scores


    ID

    The only difference was that I had to include the data set name followed by the table name and two parameters that denote the number of rows: one for forward, and one for backward.

    I do hope this answers my question.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Friday, August 4, 2017 3:43 AM
    Friday, August 4, 2017 3:43 AM
  • I have plugged in the parameters into an example code snippet below:

           Dim x, z As Integer
            Dim bClsP, bClsBackwd As String
      
          ' Write text to a file
            Dim file As New StreamWriter("c:\test01.txt")        'Forward
            For x = 0 To Me.Build00001DataSet.BXParameters.Rows.Count - 1
                bClsP = Me.Build00001DataSet.BXParameters.Rows(x).Item("BXParameter").ToString
                file.WriteLine(bClsP)
                file.WriteLine(vbNewLine)
            Next
            'Backward
            For z = Me.Build00001DataSet.BXParameters.Rows.Count To 0 Step -1
                bClsBackwd = Me.Build00001DataSet.BXParameters.Rows(z).Item("BXParameter").ToString
                file.WriteLine(bClsBackwd)
                file.WriteLine(vbNewLine)
            Next
    
            file.Close()

    During runtime, this generated the output in Notepad:

    ID


    scores


    numberScores

    numberScores


    scores


    ID

    The only difference was that I had to include the data set name followed by the table name and two parameters that denote the number of rows: one for forward, and one for backward.

    I do hope this answers my question.

    Regards,

    JohnDBCTX


    jp

    I did not mean to confuse anyone that relates to that solved problem.  I find it more sufficient with the wizard generated Data Set library. Currently, I am now trying to learn how LINQ works which would be in future forum topics.


    jp

    Friday, August 4, 2017 3:50 AM