views:

6066

answers:

10

I have a datagridview with several columns created. I add some rows and they get added correctly, but when i click on the cells, the content disappears... (?!) Edit: This is WinForms

What am I doing wrong?

The code is as follows:

foreach (SaleItem item in this.Invoice.SaleItems)
                {

                    DataGridViewRow row = new DataGridViewRow();

                    gridViewParts.Rows.Add(row);

                    DataGridViewCell cellQuantity = new DataGridViewTextBoxCell();
                    cellQuantity.Value = item.Quantity;
                    row.Cells["colQuantity"] = cellQuantity;

                    DataGridViewCell cellDescription = new DataGridViewTextBoxCell();
                    cellDescription.Value = item.Part.Description;
                    row.Cells["colDescription"] = cellDescription;

                    DataGridViewCell cellCost = new DataGridViewTextBoxCell();
                    cellCost.Value = item.Price;
                    row.Cells["colUnitCost1"] = cellCost;

                    DataGridViewCell cellTotal = new DataGridViewTextBoxCell();
                    cellTotal.Value = item.Quantity * item.Price;
                    row.Cells["colTotal"] = cellTotal;

                    DataGridViewCell cellPartNumber = new DataGridViewTextBoxCell();
                    cellPartNumber.Value = item.Part.Number;
                    row.Cells["colPartNumber"] = cellPartNumber;

                }

Thanks!

+1  A: 

It could be trying to let you edit the cell. Do you have a click event defined for the GridView?

Ian Jacobs
Nothing. No events at all. That's the first thing I thought...
ign
I thought cells were editable by default when you make a new DataGridView object.
Jared Updike
A: 

No... as I go clicking the cells, they get deleted... It's really weird. I think it has something to do with already created columns and the way I'm adding rows and cells.

ign
A: 

I should clarify, this is Winforms.

ign
A: 

I tried to recreate this problem, but I couldn't get the DataGridView to show any values at all.

I have always used a DataTable to hold all the data and set it as the DataSource of the DataGridView instead of adding the rows/values to the DataGridView directly. Is there any reason you are doing it this way instead of using a DataTable?

Adam Hughes
A: 

Aahmm... no. I was populating only a few values so I thought I'd get them one by one without having to use a datatable.

But, I'll go that way, thanks.

ign
A: 

Edit: oops! made a mistake on the second line of code. - fixed it.

Sometimes, I hate defining the datasource property.

I think that whenever you create and set a new row for "row", for some weird reason,the old value get disposed. try not using an instance to hold the rows you create :

int i;
i = gridViewParts.Rows.Add( new DataGridViewRow());

DataGridViewCell cellQuantity = new DataGridViewTextBoxCell();
cellQuantity.Value = item.Quantity;
gridViewParts.Rows[i].Cells["colQuantity"] = cellQuantity;

It seems like cells work fine with the cell instances. I have no idea why it is different for rows though. More testings may be required...

A: 

I had the same problem. If you resolved the problem, please let me know.

A: 

Just to extend this question, there's also another way to add a row to a DataGridView, especially if the columns are always the same:

object buffer[4];
List<DataGridViewRow> rows = new List<DataGridViewRow>;
foreach (SaleItem item in this.Invoice.SaleItems)
            {
                buffer[0] = item.Quantity;
                buffer[1] = item.Part.Description;
                buffer[2] = item.Price;
                buffer[3] = item.Quantity * item.Price;
                buffer[4] = item.Part.Number;

                rows.Add(new DataGridViewRow);
                rows(rows.Count - 1).CreateCells(gridViewParts, buffer);
            }
gridViewParts.Rows.AddRange(rows.ToArray());

The values need to be in the same order as the columns (including hidden ones).

Bobby
A: 

Thanks friend for your inquiry..

prashant
+1  A: 

I had the same problem; see here.

BlueRaja - Danny Pflughoeft
Well, you have yourself a correct answer then, thanks.
ign