Question

I have a question. Let's say i have a checkboxlist with the ingredieints for a cake and i choose not just one but more ingredients (3-4). I want to save this in datagridview. Can datagridview show more than one ingredient or is this not possible ? You see i'm making an application for bakeries and for ordering cakes you will choose the type of cake and adding(toppings) and the number of cakes you want. Then I would like to save this to datagridview, or show this in dgv and then when I would finish with ordering i would just click on send button in dgv and all the orders i made would be sent to an email. I hope you understand what i want.

Was it helpful?

Solution

Even in its simplest uses a DataGridView is a 2-dimesional container; a 'grid', so you can have as many rows and columns as you want.

If you want to display them all in row you should first try to estimate the maximum number of ingredients. Then you add this number of columns to the target DGV.

Then add a row and fill its column cells with the ingredients.

In case you come across a cake with even more ingredients that would't be a problem either as you can always add more columns.

Empty cells are no problem, just make sure to always check for null values.

By the way, cells are quite powerful and you can have a tag for each single cell, so you can add as many data of any complexity to the cell beyond its mere value!

Here is some simple sample code:

    while (dataGridView1.ColumnCount < checkedListBox1.CheckedItems.Count)
           dataGridView1.Columns.Add("", "");
    int newRow = dataGridView1.Rows.Add();
    int item = 0;
    foreach ( object o in checkedListBox1.CheckedItems)
    {
        dataGridView1[item, newRow].Value = o.ToString();
        item ++;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top