Let's say i have a table with the column names : 'One' , 'Two' , 'Three' ...
Column 'One' has values 'A' , 'B' and 'C'
Column 'Two' has values 'D' , 'E' and 'F'
Column 'Three' has values 'G' , 'H' and 'I'

In my code(im not sure if i did it right) i was able to populate the combobox with the table so the column names 'One' , 'Two' and 'Three' are shown.. However i am stuck on this and it has been hours and it's making me mad.. I want to be able to display the values on the datagrid as per corresponding to it's column name .. is there anything wrong i did with my code? Here's my code below :

  private void compdata()
    {
        command.CommandText = "SELECT column_name FROM information_schema.columns WHERE table_name='subcomponents' ORDER BY ordinal_position";
        connection.Open();
        dr = command.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                cboComponents.Items.Add(dr[0].ToString());
            }
        }
        dr.Close();
        connection.Close();
    }


    public void compload(string qry)
    {
        DataSet compdataa = new DataSet();
        SqlDataAdapter compDA = new SqlDataAdapter(qry, connection);
        compDA.Fill(compdataa);
        dgcomponents.DataSource = compdataa.Tables[0];
        dgcomponents.Dock = DockStyle.Fill;
    }

      private void cboComponents_SelectedIndexChanged(object sender, EventArgs e)
    {


        Teacher_Load tchload = new Teacher_Load();
        int f = 0;
        string qry = "";
        for (int i = 0; i < cboComponents.Items.Count; i++)
        {


            if (cboComponents.SelectedValue(i))
            {
                if (f == 1)
                {
                    qry = qry + "," + cboComponents.Items[i].ToString();
                }
                if (f == 0)
                {
                    qry = cboComponents.Items[i].ToString();
                }
            }
        }
        string newq = "select" + qry + "from info";
        tchload.compload(newq);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.