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);

Dani AI

Generated

Brief answer for : the combobox code is treating the control like a multi-select and using an invalid check (there’s no SelectedValue(i)). A ComboBox is single-select; for multiple columns use a CheckedListBox or a multi-select ListBox. Also your generated SQL needs spaces and proper quoting (e.g. SELECT [ColName] FROM dbo.Info). Avoid instantiating a new form just to call the loader method — call your loader on the same form or make it a reusable helper.

Single-column example (ComboBox):

// assume allowedColumns is a HashSet<string> you populated when reading the schema
string col = cboComponents.SelectedItem as string;
if (string.IsNullOrWhiteSpace(col) || !allowedColumns.Contains(col)) return;

string sql = "SELECT [" + col + "] FROM dbo.Info;";

using (var conn = new SqlConnection(connString))
using (var da = new SqlDataAdapter(sql, conn))
{
    var dt = new DataTable();
    da.Fill(dt);
    dgcomponents.DataSource = dt;
}

Multiple-columns (use a CheckedListBox):

var cols = checkedListBox.CheckedItems.Cast<string>()
             .Where(c => allowedColumns.Contains(c))
             .Select(c => "[" + c + "]")
             .ToArray();
if (cols.Length == 0) return;

string sql = "SELECT " + string.Join(", ", cols) + " FROM dbo.Info;";

using (var conn = new SqlConnection(connString))
using (var da = new SqlDataAdapter(sql, conn))
{
    var dt = new DataTable();
    da.Fill(dt);
    dgcomponents.DataSource = dt;
}

Practical notes: build allowedColumns when you read the table schema and validate every selected name against it (column names cannot be parameterized, so validation prevents injection). Wrap column names in brackets to handle spaces/special chars. Use using blocks to manage connections, and call your compload/loader on the active form instead of creating a new instance. ’s links about DataGrid basics are useful for formatting and binding details.

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.