Adicionando itens em DataGridView

18/07/2008

0

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

Responder

Posts

18/07/2008

Ricardo Silva

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...


Responder

18/07/2008

Buenolex

deu o erro:

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


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar