DataGridViewCheckBoxColumn.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
属性值
如果寄宿的 DataGridViewCheckBoxCell 对象能够具有第三种(即不确定)状态,则值为 true
;否则值为 false
。 默认值为 false
。
例外
CellTemplate 属性的值为 null
。
示例
下面的代码示例使用 a DataGridViewCheckBoxColumn 跟踪办公室照明的状态。 该属性将“turnedOff”与,该FalseValue属性将“turnedOn”与true
关联,并且该IndeterminateValue属性将“unknown”与不确定TrueValue关联。false
#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 属性返回 CellTemplate 的单元格对象的属性。 设置此属性还会设置 ThreeState 列中每个单元格的属性,并刷新列显示。 若要替代单个单元格的指定值,请设置列值后设置单元格值。
NullValue如果属性返回DefaultCellStyle的对象的属性具有一个值false
,则将ThreeState属性值更改为true
自动设置为 NullValue Indeterminate。 如果有 NullValue 一个值 Indeterminate,则更改 ThreeState 属性值以 false
自动设置为 NullValue false
。