Click here to Skip to main content
15,895,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a tabcontrol in a windows form and a SQL database like below.

Database[^]

Form[^]

<pre lang="c#">
private void CreateTabPages() // Create Tab Pages for each ProductType
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT DISTINCT ProductType, Description FROM TblProductType", con);
DataTable dt = new DataTable();
sda.Fill(dt);

foreach (DataRow dr in dt.Rows)
{
tabControl1.TabPages.Add(dr["ProductType"].ToString(),dr["Description"].ToString());
}

con.Close();
}


private void AddProductsToTabbedPanel() // Add Products to Tab Pages
{

foreach (TabPage tp in tabControl1.TabPages)
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT DISTINCT Description FROM TblProduct", con);
DataTable dt = new DataTable();
sda.Fill(dt);

FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;

foreach (DataRow dr in dt.Rows)
{
Button b = new Button();
b.Size = new Size(100, 100);
b.Text = dr["Description"].ToString();
flp.Controls.Add(b);
}

tp.Controls.Add(flp);
con.Close();
}


}

Now I want to filter products in each tabpage according to their product type, without displaying every product in my datatable in all tab pages. How do I do this?
Posted
Updated 16-Aug-13 19:17pm
v3

You can use "RowFilter" property of "DataView" Class. Like this :-

C#
DataTable tbl = GetAllProducts();

DataView dv = tbl.DefaultView;

// For Tab 1 [Product='Cycle']

dv.RowFilter = "Product='Cycle'";
DataTable tblCycle = dv.ToTable();

//---------------------------------

// For Tab 2 [Product='MotorCycle']

dv.RowFilter = "Product='MotorCycle'";
DataTable tblMotorCycle = dv.ToTable();

//---------------------------------

/*Do same for all products you have*/
 
Share this answer
 
Comments
crysis1990 17-Aug-13 2:09am    
Thank you! Will try this and let you know the result.
Dineshshp 17-Aug-13 2:15am    
If your problem has been solved using this code, please give stars to increase my points...
crysis1990 17-Aug-13 2:16am    
Is there any other method to filter products without filtering products according to each tab page?
You can use "Select" method of "DataTable" Class. Like this :-

C#
DataTable tbl = GetAllProducts();

// For Tab 1 [Product='Cycle']

DataRow[] drCycle = tbl.Select("Product='Cycle'")
 
//---------------------------------

// For Tab 2 [Product='MotorCycle']

DataRow[] drMotorCycle = tbl.Select("Product='MotorCycle'")
 
//---------------------------------

/*Do same for all products you have*/
 
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