DataGridViewCheckBoxCell.ThreeState 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出是否已針對裝載的核取方塊控制項啟用三元模式。
public:
property bool ThreeState { bool get(); void set(bool value); };
public bool ThreeState { get; set; }
member this.ThreeState : bool with get, set
Public Property ThreeState As Boolean
屬性值
如果已啟用三元模式,則為 true
;如果已啟用二進位模式,則為 false
。 預設為 false
。
範例
下列程式碼範例會使用 DataGridViewCheckBoxCell 來追蹤辦公室光源的狀態。 屬性 FalseValue 會將 「turnedOff」 與 false
產生關聯,屬性 TrueValue 會將 「turnedOn」 與 true
產生關聯,而 IndeterminateValue 屬性會將 「unknown」 關聯至不確定。
#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());
}
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
public class TriValueVirtualCheckBox:Form
{
DataGridView dataGridView1 = new DataGridView();
const int initialSize = 500;
Dictionary<int, LightStatus> store
= new Dictionary<int, LightStatus>();
public TriValueVirtualCheckBox() : base()
{
Text = this.GetType().Name;
int index = 0;
for(index=0; index<=initialSize; index++)
store.Add(index, LightStatus.Unknown);
Controls.Add(dataGridView1);
dataGridView1.VirtualMode = true;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
dataGridView1.CellValuePushed += new
DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);
dataGridView1.Columns.Add(CreateCheckBoxColumn());
dataGridView1.Rows.AddCopies(0, initialSize);
}
private DataGridViewCheckBoxColumn CreateCheckBoxColumn()
{
DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1
= new DataGridViewCheckBoxColumn();
dataGridViewCheckBoxColumn1.HeaderText = "Lights On";
dataGridViewCheckBoxColumn1.TrueValue = LightStatus.TurnedOn;
dataGridViewCheckBoxColumn1.FalseValue = LightStatus.TurnedOff;
dataGridViewCheckBoxColumn1.IndeterminateValue
= LightStatus.Unknown;
dataGridViewCheckBoxColumn1.ThreeState = true;
dataGridViewCheckBoxColumn1.ValueType = typeof(LightStatus);
return dataGridViewCheckBoxColumn1;
}
#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;
}
#endregion
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new TriValueVirtualCheckBox());
}
}
public enum LightStatus
{
Unknown,
TurnedOn,
TurnedOff
};
Imports System.IO
Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class TriValueVirtualCheckBox
Inherits System.Windows.Forms.Form
Dim WithEvents dataGridView1 As New DataGridView
Const initialSize As Integer = 500
Dim store As New Dictionary(Of Integer, LightStatus)
Public Sub New()
MyBase.New()
Text = Me.GetType().Name
Dim index As Integer = 0
For index = 0 To initialSize
store.Add(index, LightStatus.Unknown)
Next
Controls.Add(dataGridView1)
dataGridView1.VirtualMode = True
dataGridView1.AllowUserToDeleteRows = False
dataGridView1.Columns.Add(CreateCheckBoxColumn())
dataGridView1.Rows.AddCopies(0, initialSize)
End Sub
Private Function CreateCheckBoxColumn() As DataGridViewCheckBoxColumn
Dim dataGridViewCheckBoxColumn1 _
As New DataGridViewCheckBoxColumn()
dataGridViewCheckBoxColumn1.HeaderText = "Lights On"
dataGridViewCheckBoxColumn1.TrueValue = LightStatus.TurnedOn
dataGridViewCheckBoxColumn1.FalseValue = LightStatus.TurnedOff
dataGridViewCheckBoxColumn1.IndeterminateValue = _
LightStatus.Unknown
dataGridViewCheckBoxColumn1.ThreeState = True
dataGridViewCheckBoxColumn1.ValueType = GetType(LightStatus)
Return dataGridViewCheckBoxColumn1
End Function
#Region "data store maintance"
Private Sub dataGridView1_CellValueNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellValueEventArgs) _
Handles dataGridView1.CellValueNeeded
e.Value = store(e.RowIndex)
End Sub
Private Sub dataGridView1_CellValuePushed(ByVal sender As Object, _
ByVal e As DataGridViewCellValueEventArgs) _
Handles dataGridView1.CellValuePushed
store.Item(e.RowIndex) = CType(e.Value, LightStatus)
End Sub
#End Region
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New TriValueVirtualCheckBox())
End Sub
End Class
Public Enum LightStatus
Unknown
TurnedOn
TurnedOff
End Enum
備註
三元模式中的第三個狀態通常代表未初始化或不確定的值。
ThreeState設定擁有資料行的 屬性也會設定 ThreeState 資料行中每個儲存格的 屬性,並重新整理資料行顯示。 若要覆寫個別儲存格的指定值,請在設定資料行值之後設定儲存格值。
不同于資料行 ThreeState 屬性,當您變更其值時,儲存格 ThreeState 屬性不會自動更新 NullValue 相關聯儲存格樣式的 屬性。 當您將儲存格 ThreeState 屬性值變更為 true
時,請務必將 屬性所 Style 傳回物件的 屬性設定 NullValue 為 Indeterminate 。 當您將 ThreeState 屬性值變更為 false
時,請務必將 NullValue 屬性設定為 false
。