Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 109,296 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,235 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



changing the value of a text box

 
Reply to this topicStart new topic

changing the value of a text box, based on a datagrid view row

MrWobbles
post 5 Aug, 2008 - 10:32 AM
Post #1


D.I.C Head

**
Joined: 11 Apr, 2008
Posts: 54


My Contributions


CODE
Private Sub fillTextBoxes()
        Dim columnX As DataGridViewColumn
        Dim rowX As DataGridViewRow

        For Each columnX In dgView.Columns

            rowX = dgView.CurrentRow

            Me.Controls.Item(columnX.Name).Text = rowX.Cells(columnX.Name).Value.ToString

        Next
    End Sub


What I am trying to do is change the value of textboxes that were created based on the fields in a database that also
supplies the datagridview. The column name and the names of the text boxes are the same. My text boxes are nested in a tab page, a split container and a group box (allows the user to move the controls around on the form via another class) the datagrid view is also nested in a tab, and a second panel. What I want to do is when the user selects on cell from one row the text boxes fill with the contents from that row. The datagridview itself will also be editable, but the text boxes are there so the user can more easily see which data they are dealing with. I have looked quite a bit for a solution to this, but with the code I have currently I get the following error:

Object reference not set to an instance of an object.

on line 9.

~Mr.Wobbles
User is offlineProfile CardPM

Go to the top of the page


MrWobbles
post 6 Aug, 2008 - 09:56 AM
Post #2


D.I.C Head

**
Joined: 11 Apr, 2008
Posts: 54


My Contributions


While looking through the sticky's in this forum, i realized that I am in fact using vb.net, however I figured that there wouldn't be much difference so I will go ahead and post what I figured out to this problem here, and direct all future questions to the .NET forum.

While I feel like I am bludgeoning the problem this is the solution I found:

CODE

Private Sub fillTextBoxes()
        Dim columnX As DataGridViewColumn
        Dim rowX As DataGridViewRow
        Dim i As Integer = 0

        While i < Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Count
            For Each columnX In dgView.Columns
                rowX = dgView.CurrentRow

                If Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Item(i).Name = columnX.Name Then
                    Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Item(i).Controls.Item(0).Text = rowX.Cells(columnX.Name).Value.ToString
                    Exit For
                End If

            Next
            i = i + 1
        End While
    End Sub


If you can see a more elegant way of accomplishing the same thing, then I am all ears.

By the way - portTabs is my tab page holder, the first Controls.Item(o) would be the split container, the next is the
first panel, then the group box (has the name) and finally I change the value of the text box within the if statement because for some odd reason every other value returned from the expression is the tab page name... or something else that is not actually in the panel.

Any other questions that anyone may have I will be happy to answer.
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 6 Aug, 2008 - 10:57 AM
Post #3


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,322



Thanked 22 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Moved to VB.NET.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 09:34AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month