Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert value into DataGridView Cell?

I have DataGridView (that hold any DataBase)

I want to insert any value into any Cell (and that this value will save on DataBase)

How to do it (in C#)

Thank's in advance

like image 203
Gold Avatar asked Mar 07 '10 20:03

Gold


2 Answers

This is perfect code but it cannot add a new row:

dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = value;

But this code can insert a new row:

var index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[1].Value = "1";
this.dataGridView1.Rows[index].Cells[2].Value = "Baqar";
like image 36
Baqar Hassan Avatar answered Sep 24 '22 15:09

Baqar Hassan


You can access any DGV cell as follows :

dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = value;

But usually it's better to use databinding : you bind the DGV to a data source (DataTable, collection...) through the DataSource property, and only work on the data source itself. The DataGridView will automatically reflect the changes, and changes made on the DataGridView will be reflected on the data source

like image 118
Thomas Levesque Avatar answered Sep 21 '22 15:09

Thomas Levesque