Search code examples
c#listwinformsdatagridviewdatagridviewcolumn

How to insert columns and rows with data in Lists into DataGridView (C# , Windows Forms)


This project is based on Windows Forms (.NET Framework) , C#.

Let's say that I have 3 Lists with values respectively named;

List<string> ListAlternatives = new List<string>();
List<string> ListParameters = new List<string>();
List<int> ListWeight = new List<int>();

Which are already populated with data. My goals is to insert data from those lists to DataGridView; into two columns Parameters(Passing, Speed, ...) and Weight(1, 4, ...) and one row Alternatives( Player1, Player2, ... ) as presented below. Numbers under the Alternatives will be inserted manually later to the DataGridView( clicking with mouse on the cell and writing the number ) which will be used to calculate Total score using the Weight parameter.

+------------+--------+---------+---------+---------+
| Parameters | Weight | Player1 | Player2 | Player3 |
+------------+--------+---------+---------+---------+
| Passing    | 1      | 2       | 3       | 10      |
| Speed      | 4      | 8       | 10      | 2       |
| Age        | 3      | 1       | 9       | 3       |
| Shooting   | 8      | 4       | 1       | 4       |
+------------+--------+---------+---------+---------+
| Total      |--------|         |         |         |
+------------+--------+---------+---------+---------+

I've figured that I will be able to do calculations by manipulating the Lists, but I haven't been able to insert the data. The problem is that I've tried importing data using the code below but had no success due to index issues.

        dataGridView1.Columns.Add("ParameterColumn", "Parameters");

        for (int i = 0; i < ListParameters.Count(); i++)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.CreateCells(dataGridView1);
            row.Cells[0].Value = ListParameters[i];
            dataGridView1.Rows.Add(row);
        }

        foreach (var item in ListAlternatives)
        {
            dataGridView1.Columns.Add(item, item);
        }

Extra questions;

Will be there any issues if I wanted to manually change the Weight atributes(clicking on them in DataGridView and writing new value) after I already have the data in the table? My goal is to create a few charts consisting of many differents values. I've been thinking if I just loop the DataGridView and put data into the new lists - will I be able to efficiently create charts with them? Thank you for all answers


Solution

  • Please try the below code and revert back for any errors.

       dataGridView1.Columns.Add("ParameterColumn", "Parameters");
       dataGridView1.Columns.Add("WeightColumn", "Weight");
    
        for (int i = 0; i < ListAlternatives.Count(); i++)
        {
            dataGridView1.Columns.Add("Player" + i.ToString() + "Column", "Player " + i.ToString());
        }
    
        for (int i = 0; i < ListParameters.Count(); i++)
        {
            dataGridView1.Rows.Add(ListParameters[i], ListWeight[i]);
        }
    
        dataGridView1.Rows.Add("Total");
    
        dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[1].Value = ListWeight.Sum();//This is for getting sum of weights
    

    EDIT:

    Add the following line outside event, for e.g. above private void button1_click:

    List<int> multiples = new List<int>();

    Enter this code to the click event of the button which you want to calculate the values:

            dataGridView1.AllowUserToAddRows = false;
            foreach (DataGridViewColumn col in dataGridView1.Columns.Cast<DataGridViewColumn>().Skip(2))
            {
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    int multiple = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value) * Convert.ToInt32(dataGridView1.Rows[i].Cells[col.Index].Value);
                    multiples.Add(multiple);
                    multiple = 0;
                }
                dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[col.Index].Value = multiples.Sum();
                multiples.Clear();
            }