Adicionando itens em DataGridView

.NET

18/07/2008

Olá amigos, esta forma de adicionar itens em datagridview está correta:

//Exibo informações no grid (interface)
int iLinha = dgvPedido.RowCount;
this.dgvPedido.Rows.Add();
this.dgvPedido.Rows[iLinha].Cells[0].Value = codPro.ToString();
this.dgvPedido.Rows[iLinha].Cells[1].Value = desc;
this.dgvPedido.Rows[iLinha].Cells[2].Value = qtd.ToString();
this.dgvPedido.Rows[iLinha].Cells[3].Value = preco;
this.dgvPedido.Rows[iLinha].Cells[4].Value = total;

Pois quando clico para alterar um pedido e tento adicionar mais linhas surge a mensagem de erro:

No row can be added to a DataGridView control that does not have columns. Columns must be added first.

Grato,
Lex.


Buenolex

Buenolex

Curtidas 0

Respostas

Ricardo Silva

Ricardo Silva

18/07/2008

DataGridViewRow row = new DataGridViewRow();

row.Cells[0].Value = codPro.ToString();
row.Cells[1].Value = desc;
row.Cells[2].Value = qtd.ToString();
row.Cells[3].Value = preco;
row.Cells[4].Value = total; 

this.dgvPedido.Rows.Add(row);


ve se funciona...


GOSTEI 0
Buenolex

Buenolex

18/07/2008

deu o erro:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


GOSTEI 0
POSTAR