Solved

populate a datagrid according to a combo box choice in vb.net

Posted on 2004-09-13
8
501 Views
Last Modified: 2006-11-17
i have created a datagrid in vb.net that is populated by data contained in a mysql database. On the interface,the user must select an item from a combo box , the datagrid will be populated with data of that type. For instance, the user selects option1 the datagrid will be populated with data of type option 1.
i appreciated any help
0
Comment
Question by:webusername
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 12042293
What are these options that u are taking about? is the combobox populated from a datatable? I need more info
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12042303
If it is VB.NET then...
on SelectedIndexChanged event of the ComboBox populate your grid, if it is to be populated from database then open your connection etc. and bind the grid.

If you are using ASP.NET then....
set the AutoPostBack Property of the comboBox to True, then do as above..


0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12042321
Hi webusername,

There are several ways to do this for instance use a dataview and rowfilter method (ds is the dataset)
dim dv as new dataview = ds.tables(0).defaultview
yourdatagrid.datasource =  dv

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        dv.rowfilter = "youroption = " & ComboBox1.Text
' or use the selectedvalue combobox1.selectedvalue or selectedtext
    End Sub

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!

 

Author Comment

by:webusername
ID: 12052281
Actually the combo box is not populated from the database, it is only the datagrid that is populated from the database. the combo box contains items like option 1,option 2, option 3. if the user selects option1 , the database will be populated by data of type option 1 , this is done the statement : select * from tableoption where choice =(user choice from combo)
as you see i dont know how to write a statement that gets the value selected by the user in the combo box
thanks
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12052330
Hi webusername,

well you are actually almost there

"select * from tableoption where choice = " & combobox1.text
0
 

Author Comment

by:webusername
ID: 12052337
Comment from RonaldBiemans  feedback
Date: 09/14/2004 01:49AM PDT
 Comment  


Hi webusername,

well you are actually almost there

"select * from tableoption where choice = " & combobox1.text

when you put combobox1.text , does it mean that it would take the value that is selected in combo box
thanks
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12052364
Yes
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12052392
1. Get all the data from the database in DataTable1

Then filter data from the datatable using a select option
In the selected Index Changed method

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 strExpr = "choice = option1"  //Forming a expression

Dim datatable2 as DataTable
dataRow2 = new DataTable

Dim dataRow2 as DataRow

  dataRow2  =  dataRow2.NewRow();
    ' Use the Select method to find all rows matching the filter.
  dataRow2 = _
        DataTable1.Select( strExpr, strSort, DataViewRowState.Added )
    End Sub

Then set
datagrid.DataSource = datatable2

2. For each index change of the combo box go to the database to get the value

string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection myConnection = new SqlConnection(cString);
              myConnection.Open();
      SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers where choice = ?",
      myConnection);
      myCommand.CommandType = CommandType.Text;
      myCommand.Paramters.Add("param1",string, "option1");
      ds = myCommand.ExecuteDataSet();
       
DataGrid.DataSource = ds.Tables[0]


         

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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