Solved

Refresh Data in datagrid view using a combobox

Posted on 2006-06-26
7
1,893 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

13 Experts available now in Live!

Get 1:1 Help Now