Mehdi
Mehdi

Reputation: 11

refresh contiously in datagridview

I have written code to show an image in a DataGridView winform by the event of CellFormatting. It works well but the cell that I have changed to an image refreshes continuously. What should I do?

This is my code in CellFormatting:

   dgvUnknownPayed.Rows[e.RowIndex].Cells[colStateImage.Name].Value = Properties.Resources.undo; 

Upvotes: 0

Views: 605

Answers (1)

phadaphunk
phadaphunk

Reputation: 13313

Instead of modifying the cells directly you should modify the datasource.

Datagridview1.datasouce = datatable1.defaultview();

foreach (DataRow row in datatable1.Rows)
{
  row.BeginEdit();
  row[2] = ‘Something_Here’;
  row.EndEdit();
}

If you want to stop all refresh use this :

DataGrid.SuspendLayout(); //Stops refreshing
DataGrid.ResumeLayout(); //Re-Enable refreshing

DataTable is a .Net object you use in this case to bind data to your DataGridView.

DataTable datatable1 = new DataTable Creates a new instance of DataTable.
Datagridview1.datasouce = datatable1.defaultview(); Binds the datatable1 as a datasource for you grid. This way everytime the table changes, the grid will be updated.

Now to use this table you can proceed this way. (This is just an example)

  • You add columns like this table.Columns.Add("Division", typeof(int)); Where you specify the name of the column and the data type that will be entered in it. Like in SQL Server.
  • You add rows like this table.Rows.Add(Something) where something is, in this case, an Integer.

It's as simple as this. Change the Integer type for anything you want. Don't forget this is just a simple example. Your can add many more columns and rows.

Upvotes: 1

Related Questions