How to set Cell value of DataGridViewRow by column name?

后端 未结 5 732
小蘑菇
小蘑菇 2020-12-15 20:51

In windows forms, I\'m trying to fill a DataGridView manually by inserting DataGridViewRows to it, so my code looks like this:

Data         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 21:12

    So in order to accomplish the approach you desire it would need to be done this way:

    //Create the new row first and get the index of the new row
    int rowIndex = this.dataGridView1.Rows.Add();
    
    //Obtain a reference to the newly created DataGridViewRow 
    var row = this.dataGridView1.Rows[rowIndex];
    
    //Now this won't fail since the row and columns exist 
    row.Cells["code"].Value = product.Id;
    row.Cells["description"].Value = product.Description;
    

提交回复
热议问题