Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 6:35am
Comments
Member 9581488 at 17-Dec-12 12:27pm
   
U mean cell value? as you mentioned first row and second column data.
Akbar Ali Hussain at 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 at 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 at 19-Dec-12 10:39am
   
Thanks a lot.Its worked for me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 object)
3)Now you can fill the [DropdownList] by items in your List
 
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 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);
        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
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++)
            {
                DropDownList_ProgramName.Items.Add(program[i]);
            }
 
Good lock....
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

from MSDN Forum[^]
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[^]
  Permalink  

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

  Print Answers RSS
0 DamithSL 280
1 BillWoodruff 170
2 Zoltán Zörgő 170
3 OriginalGriff 134
4 Sergey Alexandrovich Kryukov 120
0 OriginalGriff 7,760
1 DamithSL 5,874
2 Sergey Alexandrovich Kryukov 5,444
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
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