Solved

Refresh Data in datagrid view using a combobox

Posted on 2006-06-26
7
1,897 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
  • 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 500 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

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!

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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