Solved

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

Posted on 2004-09-13
8
496 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
 

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
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 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

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

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