Click here to Skip to main content
15,881,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Have a excel sheet "test.xlsx" , two sheets within

sheet name :Product1 has following data :

ID ParentID Title
1 0 P1_1
2 0 P1_2

sheet name :Product2 has following data :

ID ParentID Title
1 1 P2_1
2 1 P2_2
3 1 P2_3
4 1 P2_4
5 2 P2_5
6 2 P2_6

calling both sheets in WindowsFormsApplication in asp net ( C# ) as :


C#
 OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.xlsx" + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

 /////////////////////////////////////////////////////
           
            OleDbCommand command1 = new OleDbCommand
                    (
              "SELECT ID ,ParentID ,Title " +
            " FROM [Product1$]", conn
                    );
            DataSet ds1 = new DataSet();
             OleDbDataAdapter adaptor1 = new OleDbDataAdapter(command1);
             adaptor1.Fill(ds1);
            Product1.DataSource = ds1.Tables[0];
            Product1.ValueMember = "ID";
            Product1.DisplayMember = "Title";
            
/////////////////////////////////////////////////////

            OleDbCommand command2 = new OleDbCommand
            (
            "SELECT ID	,ParentID ,Title " +
             " FROM [Product2$] " , conn
           );
            DataSet ds2 = new DataSet();
            OleDbDataAdapter adaptor2 = new OleDbDataAdapter(command2);
            adaptor2.Fill(ds2);
            Product2.DataSource = ds2.Tables[0];
            Product2.ValueMember = "ID";
            Product2.DisplayMember = "Title";



i need to know how to create master-detail ( parent - child ) relation in both combobox so that when first combo box value change , 2nd combobox value too reflect changed by ParentID field ..
Posted
Updated 27-Apr-15 17:06pm
v2
Comments
Sascha Lefèvre 28-Apr-15 0:54am    
Is it ASP.NET or Windows Forms? You mention both but it can be only one of those ;-)
cookieburner 28-Apr-15 1:06am    
windows Forms

1) Append .DefaultView to these lines:
C#
Product1.DataSource = ds1.Tables[0].DefaultView;

Product2.DataSource = ds2.Tables[0].DefaultView;


2) Double-Click the Product1-ComboBox in the Form-Designer. It will add this to your Form-Class:
C#
private void Product1_SelectedIndexChanged(object sender, EventArgs e)
{

}


3) Add these two lines:
C#
private void Product1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Product1.ValueMember == "ID")
        ((DataView)Product2.DataSource).RowFilter = "ParentID = " + Product1.SelectedValue;
}


4) Double-Click into a free space on your Form in the Form-Designer. It will add this to your Form-Class:
C#
private void Form1_Load(object sender, EventArgs e)
{

}

(It might be something different than "Form1" for you.)

5) Add this line:
C#
private void Form1_Load(object sender, EventArgs e)
{
    Product1_SelectedIndexChanged(null, null);
}


That should be it.
 
Share this answer
 
Comments
cookieburner 28-Apr-15 16:47pm    
Thanks @sascha Lefevre , it works !!!
Sascha Lefèvre 28-Apr-15 16:51pm    
You're welcome, glad I could help! Thanks for the feedback! :)
 
Share this answer
 
Comments
cookieburner 27-Apr-15 23:35pm    
I am new to C# .
links given are not understandable to me .
i not using sql , using oledb connection to import excel data .

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