-1

How to display column name of my table (database) into combobox?

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268

1 Answers1

3

You can try the following to bind the column name into combo box. Here I have used INFORMATION_SCHEMA.COLUMNS to get the column name.

public Form1()
        {
            InitializeComponent();
            BindColumnnameToComboBox();
        }
        public void BindColumnnameToComboBox()   
        {
            DataRow dr;


            SqlConnection con = new SqlConnection(@"Data Source=NiluNilesh;Initial Catalog=mynewdata;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS i where i.TABLE_NAME = 'Mark'", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "--Select--" };
            dt.Rows.InsertAt(dr, 0);

            comboBox1.ValueMember = "COLUMN_NAME";

            comboBox1.DisplayMember = "COLUMN_NAME";
            comboBox1.DataSource = dt;

            con.Close();


        }
Suraj Kumar
  • 5,290
  • 8
  • 18
  • 37