i have a datagridview which has a column name of tableNumber. i would like to fill this column from the code.

this is my method which i call in formload

public void GetAndFillReservationTables()
        {
            ManagerTableReservation mgrTableReservation = new ManagerTableReservation();

            for (int a = 0; a < dgvReservation.Rows.Count; a++)
            {
                
                int reservationID = Convert.ToInt32(dgvReservation.Rows[a].Cells[0].Value.ToString());

                List<int> listOfReservationTables = mgrTableReservation.GetReservationTablesByReservationID(reservationID);

                
                string tables = "";

                foreach (int t in listOfReservationTables)
                {
                    tables = tables + "," + t.ToString();
                }

                dgvReservation.Rows[a].Cells[7].Value = tables;

            }
        }

it works correctly but it does not show the table value in the datagridview when i run my application

Edited 5 Years Ago by xanawa: n/a

Ar you sure your 8th column shows this value? Because you stated it as column index no. 7.
And one more think: what is it doing this loop:

foreach (int t in listOfReservationTables)
                {
                    tables = tables + "," + t.ToString();
                }

This for sure is not summing up the int values, becuase you cannot sum values of type string.

You might want to do it like this (I said might, because I am not sure about what "listOfReservationTables" include).

for (int a = 0; a < dgvReservation.Rows.Count; a++)
            { 
                int reservationID = Convert.ToInt32(dgvReservation[0, a].Value); //add ToString() if necessary.
                List<int> listOfReservationTables = mgrTableReservation.GetReservationTablesByReservationID(reservationID); 
                dgvReservation[7, a].Value = listOfReservationTables.Count; 
            }

I am not sure what value would like to show in column 8, so if this solution is not it, let me know what is would be, so I can help you out with the correct value.

Ar you sure your 8th column shows this value? Because you stated it as column index no. 7.
And one more think: what is it doing this loop:

foreach (int t in listOfReservationTables)
                {
                    tables = tables + "," + t.ToString();
                }

This for sure is not summing up the int values, becuase you cannot sum values of type string.

forget about the loop.. i removed it and entered my name as a string.. still it didnt work and ans the index of the cells is right :(

Do you get any value into variable "listOfReservationTables" ?
Or is it null?
Put a break point to see if the method returns any value.

Do you get any value into variable "listOfReservationTables" ?
Or is it null?
Put a break point to see if the method returns any value.

i did actually and even the string is not null!! everything works perfectly but it does not show the value in the textbox cell

If you do:

dataGridView1[7,a].Value = "Some Value";

if all the conditions:
- 8th column at index 7 exists
- row and index a (some integer) exists

The value = "Some text" HAS TO write into that cell. There is no other way.

One more question: How do you create your dataGridView?
If there is only a text in 8th column, you actually dont need to create it.
You can only do:

dataGridView1.Columns.Add("column8", "Acutal column name");
This article has been dead for over six months. Start a new discussion instead.