DataGridView combobox column binding

1.6k views Asked by At

OK so I have an SQL Server database.

I'm building a very simple frontend for it that consists of a combobox where I pick the table, a checkedlistbox where I choose which fields to show, and a datagridview that shows the data.

The problem I'm having (which is probably a very simple one for most people, but I'm pretty new to databases in general) is that I have a column with a relationship to another, and the datagridview just shows the ID of the fields value instead of the actual value.

To clarify, I have one table (called "ItemTypes") with fields: ID Item Type CAT1 CAT2

and another table (called CAT1s) with the fields:

ID CAT1

You can see where I'm going with this. In the datagridview I'm importing all the data from the ItemTypes table, but I want to make the CAT1 column a combobox to be populated from the CAT1s table's CAT1 field. Except at the moment it's showing the ID field from CAT1s - a meaningless number to the user.

Here is the code I have to import the data into the DGV:

    private void GetData(string selectCommand)
    {
        dataGridView2.DataSource = bindingSource2;

        try
        {

            String connectionString = sConnection;
            dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            DataTable table = new DataTable();
            dataAdapter.Fill(table);
            bindingSource2.DataSource = table;
            dataGridView2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }
        catch (SqlException)
        {
    }

Any help would be mucho appreciated.

Cheers

1

There are 1 answers

1
Ranhiru Jude Cooray On

To show data from two or more tables, you would have to JOIN them together. I suggest you read up on table JOINS in SQL.

Here is a place where you can start
http://w3schools.com/sql/sql_join.asp