How to set Cell value of DataGridViewRow by column name?

user2612401

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

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);

However, I would like to add the Cell value by column name instead of doing it by the index, something like this:

row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;

But doing it like that throws an error saying it couldn't find the column named "code". I'm setting the DataGridView columns from the designer like this: Columns from DataGridViewDesigner

Am I doing something wrong? How can I accomplish what I want to do?

Derek W

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set Cell value of DataGridViewRow by column name?

From Dev

Datagridviewrow returning incorrect value of cell

From Dev

How to get "Column Name" and "Previous Value" from Cell Editing Grid

From Dev

How to combine cell value to column name with CASE logic in SQL

From Dev

Get Cell Value with column name in Php Excel

From Dev

Pandas column name of the max cell value

From Dev

Name worksheets based on cell value in another column

From Dev

Excel - Inserting cell value in formula column name

From Dev

How to set the sql results index value by desired column name in mysql?

From Dev

SQL Server: how to set a column name as the value of a dynamic variable?

From Dev

SQL Server: how to set a column name as the value of a dynamic variable?

From Dev

Set new cell value by cell name in Apache POI

From Dev

Excel – Set cell values in a column based on another cell value in intervals

From Dev

How to check if cell is null by column name in SQL?

From Dev

How to add new cell by column name in datagridview

From Dev

How can I use the value of a cell in a row to chose find a column name in a pandas dataframe?

From Dev

How set cell name for formula in HSSFDataValidation

From Dev

For each row extract the value in the column name that match another value in the cell

From Dev

For each row extract the value in the column name that match another value in the cell

From Dev

How to set column name for DataGridView?

From Dev

How to set column name for DataGridView?

From Dev

set value for a column with dynamic name in python sqlalchemy

From Dev

How to find column id by cell value?

From Dev

How to duplicate a row based on column or cell value

From Dev

How to find column id by cell value?

From Dev

How to retrieve the value of the header column of a clicked cell?

From Dev

How to copy cell value twice in another column

From Dev

How to repeat Column Value if the nextline cell is empty

From Dev

How merge value of one cell with a whole column?

Related Related

  1. 1

    How to set Cell value of DataGridViewRow by column name?

  2. 2

    Datagridviewrow returning incorrect value of cell

  3. 3

    How to get "Column Name" and "Previous Value" from Cell Editing Grid

  4. 4

    How to combine cell value to column name with CASE logic in SQL

  5. 5

    Get Cell Value with column name in Php Excel

  6. 6

    Pandas column name of the max cell value

  7. 7

    Name worksheets based on cell value in another column

  8. 8

    Excel - Inserting cell value in formula column name

  9. 9

    How to set the sql results index value by desired column name in mysql?

  10. 10

    SQL Server: how to set a column name as the value of a dynamic variable?

  11. 11

    SQL Server: how to set a column name as the value of a dynamic variable?

  12. 12

    Set new cell value by cell name in Apache POI

  13. 13

    Excel – Set cell values in a column based on another cell value in intervals

  14. 14

    How to check if cell is null by column name in SQL?

  15. 15

    How to add new cell by column name in datagridview

  16. 16

    How can I use the value of a cell in a row to chose find a column name in a pandas dataframe?

  17. 17

    How set cell name for formula in HSSFDataValidation

  18. 18

    For each row extract the value in the column name that match another value in the cell

  19. 19

    For each row extract the value in the column name that match another value in the cell

  20. 20

    How to set column name for DataGridView?

  21. 21

    How to set column name for DataGridView?

  22. 22

    set value for a column with dynamic name in python sqlalchemy

  23. 23

    How to find column id by cell value?

  24. 24

    How to duplicate a row based on column or cell value

  25. 25

    How to find column id by cell value?

  26. 26

    How to retrieve the value of the header column of a clicked cell?

  27. 27

    How to copy cell value twice in another column

  28. 28

    How to repeat Column Value if the nextline cell is empty

  29. 29

    How merge value of one cell with a whole column?

HotTag

Archive