Solved

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

Posted on 2004-09-13
8
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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