Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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.

Hi 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 :
C#
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);
        connect.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            string x;
            while (reader.Read())
            {
                x = Convert.ToString(reader[0]);
                programs.Add(x);
            }

        }
        finally
        {
            reader.Close();
            connect.Close();
        }
        return programs;
    }


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



3)Finally, I'll Fill my [DropdownList]
C#
for (int i = 0; i &lt; program.Count; i++)
            {
                DropDownList_ProgramName.Items.Add(program[i]);
            }


Good lock....
 
Share this answer
 
from MSDN Forum[^]
C#
public partial class Form1 : Form
  {
    DataSet ds;
    public Form1()
    {
      InitializeComponent();

      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];
        table.Rows.Add(dr);
      }

      //up to here!

      //this is:
      ds.Tables.Add(table);
      dropdownlistid.DataSource = ds.Tables["MyTable"];
      dropdownlistid.DisplayMember = "name";
      dropdownlistid.ValueMember = "id";
    }
  }

or else try it :
http://shahed-kazi.blogspot.in/2009/03/bind-datatable-to-dropdownlist.html[^]
 
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