none
how to use dataset for storing two column RRS feed

  • Question

  • create a dataset with two column called customer name and id .using databound control to bind that and customer name and customer id should be displayed in two dropdown list box ...can anyone provide c# code for that

    .....

    thanks in advance..


    sel.........

    Wednesday, May 9, 2012 11:04 AM

Answers

  • Hi Sa,

    Welcome to the MSDN Forum.

    I have made some changes based on Adavesh's code:

            DataTable table = new DataTable("Customer");
            protected void Page_Load(object sender, EventArgs e)
            {
                DataColumn id = new DataColumn("id");
                DataColumn name = new DataColumn("name");
                table.Columns.Add(id);
                table.Columns.Add(name);
    
                DataRow row = table.Rows.Add();
                row["id"] = 1;
                row["name"] = "Adavesh";
    
                row = table.Rows.Add();
                row["id"] = 2;
                row["name"] = "John";
    
                DataSet ds = new DataSet();
                ds.Tables.Add(table);
    
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList2.DataSource = DropDownList1.DataSource;
                DropDownList1.DataTextField = "name";
                DropDownList2.DataTextField = "id";
    
                DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
                DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
    
                DropDownList1.AutoPostBack = true;
                DropDownList2.AutoPostBack = true;
                if (!IsPostBack)
                {
                    DropDownList1.DataBind();
                    DropDownList2.DataBind();
                }
            }
    
            public void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList2.Text = table.Rows[ddl.SelectedIndex][0].ToString();
            }
    
    
            public void DropDownList2_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList1.Text = table.Rows[ddl.SelectedIndex][1].ToString();
            }

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by sa5000174 Thursday, May 10, 2012 1:44 PM
    Thursday, May 10, 2012 1:40 PM
    Moderator

All replies

  • Try this,

    DataTable table = new DataTable("Customer");
    DataColumn id   = new DataColumn("id");
    DataColumn name = new DataColumn("name");
    table.Columns.Add(id);
    table.Columns.Add(name);
    
    DataRow row = table.Rows.Add();
    row["id"] = 1;
    row["name"] = "Adavesh";
    
    row = table.Rows.Add();
    row["id"] = 2;
    row["name"] = "John";
    
    ...
    
    DataSet ds = new DataSet();
    ds.Tables.Add(table);
    
    dropDownList.DataSource = ds.Tables[0];
    dropDownList.DisplayMember = "name";
    dropDownList.ValueMember = "id";
    


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, May 9, 2012 12:44 PM
  • Hi sel,

    DataTable table1 = new DataTable("myDatatable");
    	table1.Columns.Add("name");
    	table1.Columns.Add("id");
    	table1.Rows.Add("sam", 1);
    	// Create a DataSet.
    	DataSet set = new DataSet("myDataset");
    	set.Tables.Add(table1); 
    dropDownList.DataSource = set.Tables[0];
    dropDownList.DisplayMember = "name";
    dropDownList.ValueMember = "id";

    Wednesday, May 9, 2012 12:58 PM
  • Hi, 

    Here is links you can see sample code,

    http://stackoverflow.com/questions/256832/c-sharp-fill-a-combo-box-with-a-datatable

    http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=65

    and

    http://msdn.microsoft.com/en-us/library/x8ybe6s2(v=VS.80).aspx

    BTW, please read this to chose right forum for question.

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Wednesday, May 9, 2012 12:59 PM
  • Hi,

    ok you created datatable and store the name and column but how to bind that name and id to dropdownlist1 and dropdownlist2...(in one dropdownbox i should get the "names" that are mention in that datatable and "id" on other dropdownlist box..)

    and after running if i select any name from dropdownlist1  box then corresponding id for that name should be displayed in dropdownlist2 box..can u provide c#code...

    thanks in advance. 


    sel.........

    Thursday, May 10, 2012 12:07 PM
  • ok you created datatable and store the name and column but how to bind that name and id of that table  to dropdownlist1 and dropdownlist2...(in one dropdownbox i should get the "names" that are mention in that datatable and "id" on other dropdownlist box..)

    and after running if i select any name from dropdownlist1  box then corresponding id for that name should be displayed in dropdownlist2 box..can u provide c#code...

    thanks in advance. 


    sel.........

    Thursday, May 10, 2012 12:08 PM
  • ok you created datatable and store the name and column but how to bind that name and id of that table  to dropdownlist1 and dropdownlist2...(in one dropdownbox i should get the "names" that are mention in that datatable and "id" on other dropdownlist box..)

    and after running if i select any name from dropdownlist1  box then corresponding id for that name should be displayed in dropdownlist2 box..can u provide c#code...

    thanks in advance. 



    sel.........

    Thursday, May 10, 2012 12:27 PM
  • Hi Sa,

    Welcome to the MSDN Forum.

    I have made some changes based on Adavesh's code:

            DataTable table = new DataTable("Customer");
            protected void Page_Load(object sender, EventArgs e)
            {
                DataColumn id = new DataColumn("id");
                DataColumn name = new DataColumn("name");
                table.Columns.Add(id);
                table.Columns.Add(name);
    
                DataRow row = table.Rows.Add();
                row["id"] = 1;
                row["name"] = "Adavesh";
    
                row = table.Rows.Add();
                row["id"] = 2;
                row["name"] = "John";
    
                DataSet ds = new DataSet();
                ds.Tables.Add(table);
    
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList2.DataSource = DropDownList1.DataSource;
                DropDownList1.DataTextField = "name";
                DropDownList2.DataTextField = "id";
    
                DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
                DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
    
                DropDownList1.AutoPostBack = true;
                DropDownList2.AutoPostBack = true;
                if (!IsPostBack)
                {
                    DropDownList1.DataBind();
                    DropDownList2.DataBind();
                }
            }
    
            public void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList2.Text = table.Rows[ddl.SelectedIndex][0].ToString();
            }
    
    
            public void DropDownList2_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList1.Text = table.Rows[ddl.SelectedIndex][1].ToString();
            }

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by sa5000174 Thursday, May 10, 2012 1:44 PM
    Thursday, May 10, 2012 1:40 PM
    Moderator