Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Refresh Data in datagrid view using a combobox

Posted on 2006-06-26
7
Medium Priority
?
1,902 Views
Last Modified: 2010-08-05
Hello All:

I have a form that displays a datagridview based upon selection made in a combo box. The datagridview uses a stored procedure to retrieve data and the combobox value is given as a parameter for the stored procedure. I instatiate and bind the datagrid via code in the leave event of the combobox. It works fine on the initial value but I want the datagrid to update when the combox value changes. Below is the code for the leave event of the combobox. I am using a tag as a flag to see if I have instantiated the grid or not.

How do I make the datagrid update after the combobox selection changes?

        If Me.txtDescription.Tag = "0" Then
            adpModel = New SqlDataAdapter
            cmdGetModel = New SqlCommand("SW_GET_PROC_MODEL", dbConn)
            dsModel = New DataSet
            dgvModel = New DataGridView
            With cmdGetModel
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@PROCESS_ID", Me.cboProcID.Text)
            End With
            adpModel.SelectCommand = cmdGetModel
            adpModel.Fill(dsModel, "MODEL")
            With dgvModel
                .DataSource = dsModel.Tables("MODEL")
                .Location = New Point(15, 67)
                .Size = New Size(585, 158)
            End With
            Me.Controls.Add(dgvModel)
            Me.txtDescription.Tag = "1"
        Else
            cmdGetModel.Parameters("@PROCESS_ID").Value = Me.cboProcID.Text
            adpModel.Fill(dsModel, "MODEL")
        End If


Thank you,

Sean
0
Comment
Question by:SEANWYATT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16985486
Sean,
What does this for?

    If Me.txtDescription.Tag = "0" Then

To me, it looks like the culprit.

Bob
0
 

Author Comment

by:SEANWYATT
ID: 16985554
That if what tells me if I have already instatiated the datagridview. Basically, the first time a selection has been made, the datagridview is created and added to the form. I want to reuse the datagrid and just refresh the data inside. So only the code after the ELSE statement should run once the datagrid exists. I am using Me.txtDescription.Tag.value as a flag to tell wheteher the grid already exists. If the value is "0" then I have not created the grid. if the value is "1" (or not "0") then I have created the grid and I just need to update it.

Does this make sense? See how the THEN block instatiates everything?
0
 

Author Comment

by:SEANWYATT
ID: 16985575
I set the valule of Me.txtDescription.Tag to "0" in the design window so it should alwyays start with the value of "0". It works the first time a selection is made so I just need to fix the code in the ELSE block (I think).

Thanks,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16985658
1) How many tables will dsModel have?

2) If it is one, you can fill a DataTable instead, and the syntax will be a little easier

3) When you call the Fill method, it normally appends data to the table, but doesn't clear the existing rows.  It might be adding rows to the end, and leaving the initial rows, so it looks like nothing is happening.  Count the number of rows in the table to verify.

Bob
0
 

Author Comment

by:SEANWYATT
ID: 16985743
Bob:

1-2. Just one table, so yes suggestions for using a datatable would be helpful.
3. You are right! The data is appending, so I inserted dsModel.Clear() as the first line of the ELSE block. and it is now working as expected.

If you would post your suggestions for using a datatable I would appreciate it.

Thanks,

Sean
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16985801
Where you are using a DataSet, just replace it with a DataTable:

Dim dsModel As DataSet = Nothing
dsModel = New DataSet
dgvModel.DataSource = dsModel.Tables("MODEL")

   to

Dim dtModel As DataTable
dtModel = New DataTable("Model")
dgvModel.DataSource = dtModel

Bob

0
 

Author Comment

by:SEANWYATT
ID: 16985815
Thanks for your help and the quick reply!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question