Solved

Refresh Data in datagrid view using a combobox

Posted on 2006-06-26
7
1,894 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now