简体   繁体   中英

Populate DataGridView Columns only from DataTable Dynamically

I am writing a stock system and the current interface I am working on allows users to add items to the database. There is a different table for each item type.

What I can currently do, is populate the DataGridView with the entire table and its content, which is obviously not ideal.

What I would like to happen is, a user selects the type of item they want to add to the database from a ComboBox this then populates a DataGridView with the columns from the corresponding table for that item, but not the content. The user can then add a new row and populate the fields.

here is the code behind the ComboBox , this also allows the user to select the sub type from a second ComboBox .

        var row = comboBox1.SelectedItem.ToString();
        comboBox2.Items.Clear();
        comboBox2.Text = "";

        if (comboBox1.SelectedItem.ToString() == "ARRAYS")
        {
            var nameDT = (from names in ARRAYS.AsEnumerable()
                          where names.Field<string>("Array Type") != null
                          select names.Array_Type);

            var cleanList = nameDT.Distinct().ToArray();
            foreach (var item in cleanList)
            {
                comboBox2.Items.Add(item);
            }
        }
        if (comboBox1.SelectedItem.ToString() == "BATT_CHARGER")
        {
            var nameDT = (from names in BATT_CHARGER.AsEnumerable()
                          where names.Field<string>("BATT_SIZE") != null
                          select names.BATT_SIZE);

            var cleanList = nameDT.Distinct().ToArray();
            foreach (var item in cleanList)
            {
                comboBox2.Items.Add(item);
            }
        }
        if (comboBox1.SelectedItem.ToString() == "BUFFERBOX")
        {
            var nameDT = (from names in BUFFERBOX.AsEnumerable()
                          where names.Field<string>("BufferBox Type") != null
                          select names.BufferBox_Type);

            var cleanList = nameDT.Distinct().ToArray();
            foreach (var item in cleanList)
            {
                comboBox2.Items.Add(item);
            }
        }

How can I set the DataGridView columns to the same as the tables they will be saving into, without filling it with all the row data?

ARRAYS

BUFFERBOX

BATT_CHARGER

are all table names, and Array Type, BATT_SIZE and BufferBox Type are column names.

I ended up doing this which works perfectly.

 if (comboBox1.SelectedItem.ToString() == "USB_RS232")
        {
            var nameDT = (from names in USB_RS232.AsEnumerable()
                          where names.Field<string>("MODEL") != null
                          select names.MODEL);

            datatable = USB_RS232;

            var cleanList = nameDT.Distinct().ToArray();
            foreach (var item in cleanList)
            {
                comboBox2.Items.Add(item);
            }
        }


        dataGridView1.Columns.Clear();
        foreach (DataColumn item in datatable.Columns)
        {
            var colname = item.ColumnName.ToString();
            dataGridView1.Columns.Add(colname, colname);
        }

The dataTable is selected when you pick from the comboBox then i just loop throught he column names in the dataTable and add them to the DGV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM