Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI Guys,

I need a help I think this might be a dumb question,


I have a window form application and I have a database of tables Course and Students. I am just trying to do some filter in my windows application to be clear I have a combobox and grid. I am able to display all the data but If I want to see only student who is enrolled in pirticular course. Course are displayed in Combobox and details in GridView. as soon as if we select an course in comobox, student details should be refreshed in that pirticular course.
Posted
Comments
Dalek Dave 19-Nov-10 19:53pm    
There are no dumb questions, only dumb answers!

you can try this code:

string course = cboCourse.SelectedItem.ToString();
           //You do your Selection of Student from your table Student
           //you Set the Data Source of the GridView
           string connectionstring = "Data Source=Serveur\\INSTANCESQL;Initial Catalog=Student;Integrated Security=True";
           SqlConnection cnx = new SqlConnection(connectionstring);
           cnx.Open();
           string query = "SELECT [Student] FROM [User].[dbo].[Student] WHERE course = @course";
           SqlCommand cmd = new SqlCommand(query,cnx);
           SqlParameter p1 = new SqlParameter("@course", SqlDbType.Char, 10);
           p1.Value = course;
           cmd.Parameters.Add(p1);

           cmd.ExecuteNonQuery();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);

           dgStudents.DataSource = ds.Tables[0];
           cnx.Close();
 
Share this answer
 
Steps:
1. Bind the courses in your course combobox
2. Attach an event handler of combobox selectedindex change to this course combobox
3. In this change event, find the current selected value
4. Based on the current selected value, either fetch data related to it from DB(using a WHERE clause in query) or filter the dataset based on it
5. Bind the data filtered/fetched to grid for display

Try!
 
Share this answer
 
Comments
Gautham Gorla 19-Nov-10 10:58am    
can you Please elobrate step 4 and 5 Please,I am trying to write combobox.selectedindex in form load...will you please elobrate it....Thanks for reply
Sandeep Mewara 19-Nov-10 11:02am    
Well here you go: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx
If you use a BindingSource[^] to bind your data to the DataGridView, you can then use the Filter[^] property of the BindingSource to filter your data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900