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

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
webusernameAsked:
Who is Participating?
 
RonaldBiemansCommented:
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
 
123654789987Commented:
What are these options that u are taking about? is the combobox populated from a datatable? I need more info
0
 
arif_eqbalCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
webusernameAuthor Commented:
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
 
RonaldBiemansCommented:
Hi webusername,

well you are actually almost there

"select * from tableoption where choice = " & combobox1.text
0
 
webusernameAuthor Commented:
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
 
RonaldBiemansCommented:
Yes
0
 
123654789987Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.