Solved

Refresh Data in datagrid view using a combobox

Posted on 2006-06-26
7
1,896 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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