Good resources:

First Create A Class To Be Able To Add Data

Create a class which has a variable for each column the datagrid will have – the class will be created for each data grid row. You can implement how these properties get set however you want, but a good way is to write them as part of the constructor.

	ref class GridEntry
		property String ^GroupName;	//Order here important - this is right most column
		property bool Overloaded;
		property bool Ok;
		property bool Underloaded;

		public: GridEntry::GridEntry(String ^NewGroupName, bool NewOverloaded, bool NewOverloadWarning, bool NewOk, bool NewUnderloadWarning, bool NewUnderloaded)
			GroupName = NewGroupName;
			Overloaded = NewOverloaded;
			Ok = NewOk;
			Underloaded = NewUnderloaded;

To add the class to a form see here

Add DataViewGrid to your form

Add the datagrid to your form and create each of the columns.

For each column set the 'datapropertyname' to match the class property you want to display in it.

Go through the main properties and disable things like allow sort, allow resize etc assumign you don't want them.

Heading row style / alignment etc

Display It

	array<GridEntry^> ^GridData;
	GridData = gcnew array<GridEntry^>(10);
	GridData[0] = gcnew GridEntry("adam", 1, 1, 1, 1, 0);
	GridData[1] = gcnew GridEntry("nick", 0, 0, 0, 0, 1);

	MyDataGridView1->DataSource = GridData;

Column Titles

	dataGridView1->Columns[0]->HeaderText = "Date / Time";
	dataGridView1->Columns[1]->HeaderText = "Device No.";

Get Data

When using an array the array is updated as the user interacts ith it. When using a database as the datasource you have to manually tell it to be updated.

Getting Edit Events

All of the cell value type events only occur after editing of a cell is complete – yes all of them! When you say click a check box in a cell it begins an editing session. The value of the underlying cell doesn't change until that editing session ends. It's only when you click in another cell that the editing session ends and the cell value changes. The reason it is done this way is because while the user is in the process of editing the contents of a cell they may enter a value that is not valid. To avoid throwing errors because of this the grid waits until the entire value has been entered before propagating it to the underlying cell. That is the correct way to do things. If you want to force the cell value to be updated immediately when the user makes a change then you will have to write code to do this. You have to be careful with this though as a user can press escape to cancel any edits they've made which won't cause an event. A simple solution is the have your processing code in the CellValueChanged event as normal and then to force it to occur by using the EndEdit event on the datagrid. For checkboxes you can simply use the mouse up event:

	private: System::Void MyDataGridView1_CellMouseUp(System::Object^  sender, System::Windows::Forms::DataGridViewCellMouseEventArgs^  e)

And use this as the value changed event:

private: System::Void MyDataGridView1_CellValueChanged(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)
	//Get the value of a cell
	//value = MyDataGridView1->Rows(e->RowIndex)->Cells(e->ColumnIndex)->Value;

Adding To or Modifying The Array

Not sure this is the offficial best way to update the data grid view, but simply using this again does the job

	MyDataGridView->DataSource = MyArray;



Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.


Your email address will not be published. Required fields are marked *