How to pass values from textbox to datagridview in VB 2008?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How to pass values from textbox to datagridview in VB 2008?

  1. #1
    Join Date
    Feb 2009
    Posts
    6

    How to pass values from textbox to datagridview in VB 2008?

    i used a split container, in the 1st panel, i put the textbox for the user to input the values. In the 2nd panel, i put the datagridview to output the values that the user inputted. Now, i don't know and i cannot find way how to print the values into the datagridview from the textbox. please help me. thanks!

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How to pass values from textbox to datagridview in VB 2008?

    Create a class object to hold the information for the Datagridview.. create a variable as a List Of (object) and set it as the Datasource for the Datagridview..

    Every time the text entry is completed add it to the list and update/Refresh the Datagridview ....

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Feb 2009
    Posts
    6

    Re: How to pass values from textbox to datagridview in VB 2008?

    Can you please show me a sample code? So i can understand more because i'm a newbie and cannot understood some terms. Thanks!

  4. #4
    Join Date
    Feb 2009
    Posts
    6

    Re: How to pass values from textbox to datagridview in VB 2008?

    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim num1, num2, num3 As Integer

    num1 = TextBox2.Text
    num2 = TextBox3.Text
    num3 = num2 * num1
    TextBox4.Text = num3

    End Sub

    --->This is my input code

    Public Property DataSource() As Object
    Get

    End Get

    Set(ByVal value As Object)

    End Set

    End Property

    ---> This is my output code. Can you please help me to fill up?

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How to pass values from textbox to datagridview in VB 2008?

    Your part of the way there .... so lets fill up ...

    Code:
        Public Class DataGridData
            Private _Text As String
    #Region "Properties"
            Public Property Text() As String
                Get
                    Return _Text
                End Get
                Set(ByVal Value As String)
                    _Text = Value
                End Set
            End Property
    #End Region
        End Class
    Above is you class object that you can now use to add lists of text to the datagridview...

    Next we need to declare a few items...

    Code:
    Private DGVdata As List(Of DataGridData)
    
    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim num1, num2, num3 As Integer
        num1 = TextBox2.Text
        num2 = TextBox3.Text
        num3 = num2 * num1
        TextBox4.Text = num3
        TmpDGVData.Text = TextBox4.Text 
    
        'Create a new entry
        Dim TmpDGVData As New DataGridData
        DGVdata.Add (TmpDGVData)
    
        'Update the Datagridview...
        RefreshDGV()
    End Sub
    
    Private Sub RefreshDGV()
        DataGridView1.DataSource = DGVdata
    End Sub
    This i think should help you get started...

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Feb 2009
    Posts
    6

    Re: How to pass values from textbox to datagridview in VB 2008?

    I tried to run it but when i clicked the button but there's an error. "NullReferenceException was unhandled. Object reference not set to an instance of an object." Please help me. Thanks!

  7. #7
    Join Date
    Feb 2009
    Posts
    6

    Re: How to pass values from textbox to datagridview in VB 2008?

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ComboBox1.Items.Add("CD/DVD Burning".ToString)
    ComboBox1.Items.Add("Downloads".ToString)
    ComboBox1.Items.Add("Printing".ToString)

    ComboBox1.Width = 100

    End Sub

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub SplitContainer1_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel1.Paint

    End Sub

    Private Sub Transaction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub

    Public Class DataGridData
    Private _Text As String
    #Region "Properties"
    Public Property Text() As String
    Get
    Return _Text
    End Get
    Set(ByVal Value As String)
    _Text = Value
    End Set
    End Property
    #End Region
    End Class

    Private DGVdata As List(Of DataGridData)

    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Create a new entry
    Dim TmpDGVData As New DataGridData
    DGVdata.Add(TmpDGVData)

    Dim quantity, price, totalAmount As Integer
    quantity = TextBox2.Text
    price = TextBox3.Text
    totalAmount = quantity * price
    TextBox4.Text = totalAmount
    TmpDGVData.Text = TextBox4.Text

    'Update the Datagridview...
    RefreshDGV()
    End Sub

    Private Sub RefreshDGV()
    DataGridView1.DataSource = DGVdata
    End Sub



    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
    End Class

    --->This is my whole code. Still gets this error when i run it. "A first chance exception of type 'System.NullReferenceException' occurred in ic.exe" Please help me. Thanks a lot!

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: How to pass values from textbox to datagridview in VB 2008?

    Please go back and use CODE TAGS around your code
    Code:
    ' so we CAN READ IT
    '    the way      you  wrote     IT!
    also, highlight the line that crashes, and give the exact error message
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How to pass values from textbox to datagridview in VB 2008?

    Like i said i gave you enough to get started...

    all that is missing is in the form load sub add...
    Code:
    DGVdata = New List(Of DataGridData)
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  10. #10
    Join Date
    Feb 2009
    Posts
    6

    Re: How to pass values from textbox to datagridview in VB 2008?

    Okay. Thanks a lot!

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center