Click here to Skip to main content
13,000,127 members (55,786 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a datatable which contains the data like three rows and three coloumns.But I would like to populate the only first row second coloumn data to the dropdownlist from datable.I dont want to populate all the data which is in datatable.Can you help?
Posted 17-Dec-12 5:35am
Member 9581488 17-Dec-12 12:27pm
U mean cell value? as you mentioned first row and second column data.
Akbar Ali Hussain 17-Dec-12 13:04pm
If you want to show value of only one cell (first row second column), why you are using a Dropdownlist? I guess your requirement is adding values to a dropdownlist, not binding a table. If yes, use dropdownlist.items.add()
priya9826 19-Dec-12 9:58am
Yes.You are exactly correct.I would like to aading the only one cell value to dropdownlist instead of binding the table.Can you send the exact code to me?
priya9826 19-Dec-12 10:39am
Thanks a lot.Its worked for me.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Firstly, you must choose what is your DBMS type (Access or oracle or sql server)
Secondly, you must work in three steps:
1)Select your data from the table in your database
2)put your selected data in a list (List<string> object)
3)Now you can fill the [DropdownList] by items in your List<string>

As example,I used SQL server as DBMS and I have in my database a [Table] named program contains two columns [Id,Name] :
1) Firstly,I'll fill the List<string> by using static Method :
static public List<string> SelectPrograms(List<string> programs)
        string ConnectionString =      ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection connect = new SqlConnection(ConnectionString);
        string query = "select program_name from Program;";
        SqlCommand command = new SqlCommand(query, connect);
        SqlDataReader reader = command.ExecuteReader();
            string x;
            while (reader.Read())
                x = Convert.ToString(reader[0]);
        return programs;

2)Now I'll use the last method after I take a List<string> variable
List<string> program = new List<string>();
            program = [Put_ClassName].SelectPrograms(program);

3)Finally, I'll Fill my [DropdownList]
for (int i = 0; i &lt; program.Count; i++)

Good lock....
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

from MSDN Forum[^]
public partial class Form1 : Form
    DataSet ds;
    public Form1()
      ds = new DataSet();
      //I will create and populate dataTable manually, 
      //you will do it with sqlDataAdapter Fill() method:
      //so this is not for you
      DataTable table = new DataTable("MyTable");
      table.Columns.Add("id", typeof(int));
      table.Columns.Add("name", typeof(string));
      string[] names = { "A", "B", "C", "A", "B" };
      DataRow dr;
      for (int i = 0; i < names.Length; i++)
        dr = table.NewRow();
        dr["id"] = i + 1;
        dr["name"] = names[i];
      //up to here!
      //this is:
      dropdownlistid.DataSource = ds.Tables["MyTable"];
      dropdownlistid.DisplayMember = "name";
      dropdownlistid.ValueMember = "id";

or else try it :[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100