DataGridViewCheckBoxColumn::ThreeState Property
Gets or sets a value indicating whether the hosted check box cells will allow three check states rather than two.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Property Value
Type: System::Booleantrue if the hosted DataGridViewCheckBoxCell objects are able to have a third, indeterminate, state; otherwise, false. The default is false.
| Exception | Condition |
|---|---|
| InvalidOperationException | The value of the CellTemplate property is null. |
The indeterminate state can be useful, for example, when you do not want to set a default value in the check box.
Getting or setting this property gets or sets the ThreeState property of the cell object returned by the CellTemplate property. Setting this property also sets the ThreeState property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.
If the NullValue property of the object returned by the DefaultCellStyle property has a value of false, changing the ThreeState property value to true automatically sets NullValue to Indeterminate. If NullValue has a value of Indeterminate, changing the ThreeState property value to false automatically sets NullValue to false.
The following code example uses a DataGridViewCheckBoxColumn to track the status of office lighting. The FalseValue property associates "turnedOff" with false, the TrueValue property associates "turnedOn" with true, and the IndeterminateValue property associates "unknown" to indeterminate.
#using <System.Windows.Forms.dll> #using <System.dll> #using <System.Drawing.dll> using namespace System; using namespace System::IO; using namespace System::Collections::Generic; using namespace System::Windows::Forms; public enum class LightStatus { Unknown, TurnedOn, TurnedOff }; public ref class TriValueVirtualCheckBox: public Form { private: DataGridView^ dataGridView1; private: const int initialSize; private: Dictionary<int, LightStatus>^ store; public: TriValueVirtualCheckBox() : Form(), initialSize(500) { dataGridView1 = gcnew DataGridView(); store = gcnew Dictionary<int, LightStatus>(); Text = this->GetType()->Name; for(int i = 0; i < initialSize; i++) { store->Add(i, LightStatus::Unknown); } Controls->Add(dataGridView1); dataGridView1->VirtualMode = true; dataGridView1->AllowUserToDeleteRows = false; dataGridView1->CellValueNeeded += gcnew DataGridViewCellValueEventHandler( this, &TriValueVirtualCheckBox::dataGridView1_CellValueNeeded); dataGridView1->CellValuePushed += gcnew DataGridViewCellValueEventHandler( this, &TriValueVirtualCheckBox::dataGridView1_CellValuePushed); dataGridView1->Columns->Add(CreateCheckBoxColumn()); dataGridView1->Rows->AddCopies(0, initialSize); } private: DataGridViewCheckBoxColumn^ CreateCheckBoxColumn() { DataGridViewCheckBoxColumn^ dataGridViewCheckBoxColumn1 = gcnew DataGridViewCheckBoxColumn(); dataGridViewCheckBoxColumn1->HeaderText = "Lights On"; dataGridViewCheckBoxColumn1->TrueValue = LightStatus::TurnedOn; dataGridViewCheckBoxColumn1->FalseValue = LightStatus::TurnedOff; dataGridViewCheckBoxColumn1->IndeterminateValue = LightStatus::Unknown; dataGridViewCheckBoxColumn1->ThreeState = true; dataGridViewCheckBoxColumn1->ValueType = LightStatus::typeid; return dataGridViewCheckBoxColumn1; } #pragma region "data store maintance" private: void dataGridView1_CellValueNeeded(Object^ sender, DataGridViewCellValueEventArgs^ e) { e->Value = store[e->RowIndex]; } private: void dataGridView1_CellValuePushed(Object^ sender, DataGridViewCellValueEventArgs^ e) { store[e->RowIndex] = (LightStatus) e->Value; } #pragma endregion }; [STAThread] int main() { Application::Run(gcnew TriValueVirtualCheckBox()); }
Available since 2.0