Results 1 to 13 of 13

Thread: Working with datagridview

  1. #1

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Working with datagridview

    Hello,
    Trying to figure out a problem just hoping someone might be able to help or point me in the right direction.

    What I am trying to do is have a ComboBox's contents and some text boxes be supplied by cells within a datagridview.
    What I would like to do is if say column1 contents are = "GY" then that records column2 goes into the comboBox's items. I want it to then loop through the entire datagridview records and add every example of this to the comboBox's collection. From their I want to be able to select a item from the comboBox and then textBox1 and textBox2's contents will be provided from the same record but instead column 3 and 4 of same as the selected item in comboBox... any help would be appreciated. Thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Working with datagridview

    Can you rephrase the question? As it is right now, I'm having a hard time trying to guess what exactly it is that you want.
    Please clearly explain what you want to achieve, post some pictures if need to...

    Quote Originally Posted by jbailey01 View Post
    Hello,
    Trying to figure out a problem just hoping someone might be able to help or point me in the right direction.

    What I am trying to do is have a ComboBox's contents and some text boxes be supplied by cells within a datagridview.
    What I would like to do is if say column1 contents are = "GY" then that records column2 goes into the comboBox's items. I want it to then loop through the entire datagridview records and add every example of this to the comboBox's collection. From their I want to be able to select a item from the comboBox and then textBox1 and textBox2's contents will be provided from the same record but instead column 3 and 4 of same as the selected item in comboBox... any help would be appreciated. Thanks
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Lively Member
    Join Date
    May 2009
    Location
    EG
    Posts
    87

    Re: Working with datagridview

    Don't you have an example to show us !!!

  4. #4

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    Sorry i dont have an example because i dont know how to do this and haven't found anything regarding this in my various searches I have attached pictures
    Name:  program.jpg
Views: 764
Size:  103.5 KB

    what i want to do exactly is if the first column contains the words TP8 then it looks into the Store Number column... From their i want my drop down box to contain all the different store numbers and use the store number as a record selector so it also bring the city, state, etc. to the textboxes...does this kinda clear it up?

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Working with datagridview

    That's better but still somewhat unclear. How is the datagridview populated? Is it databound? Where is the combobox? Is it in a different form? How is it populated? Is it databound? If so, what's the datasource?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6
    Hyperactive Member
    Join Date
    Aug 2011
    Posts
    272

    Re: Working with datagridview

    I believe what he's wanting to do is something along these lines:

    Column A contains a code of some sort. When some unspecified act takes place (clicking on a row, selecting a radio button, hard coded, who knows), the Combobox would be populated with Column B for every row that contains the particular code in Column A. After that, whenever a selection is made in said ComboBox, the values of Columns C & D would be displayed in a pair of TextBoxes.

    The only unknown is how the particular code used to populate the ComboBox is selected in the first place. (And also, what the purpose of all of this is because it sounds like it might be simpler to filter the DataGridView.)

  7. #7

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    The program runs a series of survey's. The datagridview is populated when the user signs in on the login screen that sends them to the welcome screen. I found it easier to do this because the list changes for every user that logs in to show them their corresponding access database. The user will never be able to edit or change their database. It's basically the users daily tasks.

    Code:
     If txt_Username.Text = reader.Item("username").ToString And txt_Password.Text = reader.Item("password").ToString Then
                            MessageBox.Show("Login successful")
                            WelcomeForm.Show()
                            Me.Hide()
    
                            callList = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Jbailey\Documents\CallerLists\" + txt_Username.Text + ".mdb"
    
    
                            Dim connectionString As String = callList
                            Dim sql As String = "SELECT * FROM CallList"
                            Dim connection As New OleDbConnection(connectionString)
                            Dim dataadapter As New OleDbDataAdapter(sql, connection)
                            Dim ds As New DataSet()
                            connection.Open()
                            dataadapter.Fill(ds, "CallList_table")
                            connection.Close()
                            WelcomeForm.CallerDataGridView.DataSource = ds
                            WelcomeForm.CallerDataGridView.DataMember = "CallList_table"
    
    
                            WelcomeForm.CallerDataGridView.Columns(0).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(1).Visible = True
                            WelcomeForm.CallerDataGridView.Columns(2).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(3).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(4).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(5).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(6).Visible = True
                            WelcomeForm.CallerDataGridView.Columns(7).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(8).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(9).Visible = True
                            WelcomeForm.CallerDataGridView.Columns(10).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(11).Visible = True
                            WelcomeForm.CallerDataGridView.Columns(12).Visible = True
                            WelcomeForm.CallerDataGridView.Columns(13).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(14).Visible = False
                            WelcomeForm.CallerDataGridView.Columns(15).Visible = False
    
                            If txt_Username.Text = "Jbailey" Then
                                WelcomeForm.AdministrationToolStripMenuItem.Enabled = True
                            Else
                                WelcomeForm.AdministrationToolStripMenuItem.Enabled = False
                            End If

  8. #8

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    The form housing the little information I need from the datagridview is exported into another database. The database that I link the grid to changes everyday. So within their tasks on the form that will show the comboBox with the StoreNumber I want it also to show the city, state, and phone number for this. And the ComboBox can only show StoreNumbers that in their Column1 have a certain value for example for this form only stores that have TP8 in the first column will appear in the combobox.. I hope im helping here. Thanks for your guy's time!

  9. #9
    Hyperactive Member
    Join Date
    Aug 2011
    Posts
    272

    Re: Working with datagridview

    I still don't understand where you're getting the code from, but to populate the ComboBox, you would do something like this:

    vb Code:
    1. Dim i As Integer
    2. For i = 0 To DataGridView1.RowCount - 1
    3.  
    4.      If DataGridView1.Rows(i).Cells("WhateverTheCodeColumnIsNamed").Value.ToString = "TP8" Then
    5.           ComboBox1.Items.Add(DataGridView1.Rows(i).Cells("StoreNumber").Value.ToString)
    6.      End If
    7. Next

    And then when a selection is made in the ComboBox you could iterate through the DataGridView again until you find the matching row and then set the value of the two TextBoxes to the value of the corresponding DataGridView cells.

    There's probably a better way to do it, but that would work.
    Last edited by samuelk; Aug 1st, 2012 at 12:14 PM. Reason: hard to proofread code while the code box is 1 line high

  10. #10

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    Thank you i will give it a shot.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Working with datagridview

    In regards to your connection to the database, I noticed you have a hard coded path which if running on another PC will fail if the high level folder does not exists as per C:\Users\Jbailey\Documents so if this is the case a better method would be dynamic for the first part of the path as shown below assuming CallerList is under the current user profile My Documents folder.

    Code:
    Dim Builder As New OleDbConnectionStringBuilder With
        {
            .DataSource = _
            IO.Path.Combine(System.Environment.GetFolderPath(
                            Environment.SpecialFolder.MyDocuments),
                        "CallerLists\" & txt_Username.Text & ".mdb"),
            .Provider = "Microsoft.Jet.OLEDB.4.0"
        }
    
    Dim connection As New OleDbConnection(Builder.ConnectionString)

  12. #12

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    Quote Originally Posted by samuelk View Post
    I still don't understand where you're getting the code from, but to populate the ComboBox, you would do something like this:

    vb Code:
    1. Dim i As Integer
    2. For i = 0 To DataGridView1.RowCount - 1
    3.  
    4.      If DataGridView1.Rows(i).Cells("WhateverTheCodeColumnIsNamed").Value.ToString = "TP8" Then
    5.           ComboBox1.Items.Add(DataGridView1.Rows(i).Cells("StoreNumber").Value.ToString)
    6.      End If
    7. Next

    And then when a selection is made in the ComboBox you could iterate through the DataGridView again until you find the matching row and then set the value of the two TextBoxes to the value of the corresponding DataGridView cells.

    There's probably a better way to do it, but that would work.
    Works like a charm! Thanks. And how would I have the StoreNumber refernced in the ComboBox bring the associated City and State with it?

  13. #13

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with datagridview

    Is there any way to use the store number combobox that has been filled as a "row selector" and auto fill the city, state based on the StoreNumber selected?

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
  •  



Click Here to Expand Forum to Full Width