DataGridTableStyle.AllowSorting 属性

指示在使用此 DataGridTableStyle 时是否允许在网格表上进行排序。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property AllowSorting As Boolean
用法
Dim instance As DataGridTableStyle
Dim value As Boolean

value = instance.AllowSorting

instance.AllowSorting = value
public bool AllowSorting { get; set; }
public:
property bool AllowSorting {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_AllowSorting ()

/** @property */
public void set_AllowSorting (boolean value)
public function get AllowSorting () : boolean

public function set AllowSorting (value : boolean)

属性值

如果允许排序,则为 true;否则为 false。默认为 true

备注

AllowSorting 属性设置为 true 时,每个列标头中均将显示一个三角形来指示排序方向。用户可以单击任意列标头以按该列对网格进行排序。第二次单击列将更改排序方向。

此属性重写 DataGrid.AllowSorting 属性。

示例

下面的代码示例允许通过单击按钮切换 DataGrid 上的排序可用性,并在标签中显示当前排序状态。该示例要求有 DataGrid(带有包含一些数据的 System.Data.DataSet),以及 Form 上的一个 Button 和一个 Label

Private Sub DataGridTableStyle_Sample_Load(ByVal sender As Object, _
                        ByVal e As EventArgs) Handles MyBase.Load
   myDataGridTableStyle1 = New DataGridTableStyle()

   mylabel.Text = "Sorting Status :" + myDataGridTableStyle1.AllowSorting.ToString()
   If myDataGridTableStyle1.AllowSorting = True Then
      btnApplyStyles.Text = "Remove Sorting"
   Else
      btnApplyStyles.Text = "Apply Sorting"
   End If
   ' Attach custom event handlers.
   AddHandler myDataGridTableStyle1.AllowSortingChanged, AddressOf AllowSortingChanged_Handler
   myDataGridTableStyle1.MappingName = "Customers"
End Sub 'DataGridTableStyle_Sample_Load

Private Sub AllowSortingChanged_Handler(ByVal sender As Object, ByVal e As EventArgs)
   mylabel.Text = "Sorting Status :" + myDataGridTableStyle1.AllowSorting.ToString()
End Sub 'AllowSortingChanged_Handler

Private Sub btnApplyStyles_Click(ByVal sender As Object, _
                                 ByVal e As EventArgs) Handles btnApplyStyles.Click
   If myDataGridTableStyle1.AllowSorting = True Then
      ' Remove sorting.
      myDataGridTableStyle1.AllowSorting = False
      btnApplyStyles.Text = "Allow Sorting"
   Else
      ' Allow sorting.
      myDataGridTableStyle1.AllowSorting = True
      btnApplyStyles.Text = "Remove Sorting"
   End If

   mylabel.Text = "Sorting Status :" + myDataGridTableStyle1.AllowSorting.ToString
   ' Add the DataGridTableStyle to DataGrid.
   myDataGrid.TableStyles.Add(myDataGridTableStyle1)
End Sub 'btnApplyStyles_Click
private void DataGridTableStyle_Sample_Load(object sender,
                                       EventArgs e)
{
   myDataGridTableStyle1 = new DataGridTableStyle();

   mylabel.Text = "Sorting Status :" + 
         myDataGridTableStyle1.AllowSorting.ToString();
   if(myDataGridTableStyle1.AllowSorting == true)
   {
      btnApplyStyles.Text = "Remove Sorting";
   }
   else
   {
      btnApplyStyles.Text = "Apply Sorting";
   }
   // Attach custom event handlers.
   myDataGridTableStyle1.AllowSortingChanged += 
               new System.EventHandler(AllowSortingChanged_Handler);
   myDataGridTableStyle1.MappingName = "Customers";
} 
private void AllowSortingChanged_Handler(object sender,EventArgs e)
{         
   mylabel.Text = "Sorting Status :" 
         + myDataGridTableStyle1.AllowSorting.ToString();
}     
private void btnApplyStyles_Click(object sender, EventArgs e)
{       

   if(myDataGridTableStyle1.AllowSorting == true)
   {            
      // Remove sorting.
      myDataGridTableStyle1.AllowSorting = false; 
      btnApplyStyles.Text = "Allow Sorting";
   }
   else
   {
      // Allow sorting.
      myDataGridTableStyle1.AllowSorting = true;
      btnApplyStyles.Text = "Remove Sorting";
   } 

   mylabel.Text = "Sorting Status :" + myDataGridTableStyle1.AllowSorting;
   // Add the DataGridTableStyle to DataGrid.
   myDataGrid.TableStyles.Add(myDataGridTableStyle1);
}
private:
   void DataGridTableStyle_Sample_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myDataGridTableStyle1 = gcnew DataGridTableStyle;
      mylabel->Text = String::Concat( "Sorting Status : ", myDataGridTableStyle1->AllowSorting );
      if ( myDataGridTableStyle1->AllowSorting == true )
      {
         btnApplyStyles->Text = "Remove Sorting";
      }
      else
      {
         btnApplyStyles->Text = "Apply Sorting";
      }

      myDataGridTableStyle1->AllowSortingChanged += gcnew System::EventHandler(
         this, &DataGridTableStyle_Sample::AllowSortingChanged_Handler );
      myDataGridTableStyle1->MappingName = "Customers";
   }

   void AllowSortingChanged_Handler( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      mylabel->Text = String::Concat( "Sorting Status : ", myDataGridTableStyle1->AllowSorting );
   }

   void btnApplyStyles_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      if ( myDataGridTableStyle1->AllowSorting == true )
      {
         // Remove sorting.
         myDataGridTableStyle1->AllowSorting = false;
         btnApplyStyles->Text = "Allow Sorting";
      }
      else
      {
         // Allow sorting.
         myDataGridTableStyle1->AllowSorting = true;
         btnApplyStyles->Text = "Remove Sorting";
      }

      mylabel->Text = String::Concat( "Sorting Status : ", myDataGridTableStyle1->AllowSorting );

      // Add the DataGridTableStyle to DataGrid.
      myDataGrid->TableStyles->Add( myDataGridTableStyle1 );
   }
private void DataGridTableStyleSampleLoad(Object sender, EventArgs e)
{
    myDataGridTableStyle1 = new DataGridTableStyle();
    mylabel.set_Text("Sorting Status :" 
        + System.Convert.ToString(myDataGridTableStyle1.get_AllowSorting()));
    if (myDataGridTableStyle1.get_AllowSorting() == true) {
        btnApplyStyles.set_Text("Remove Sorting");
    }
    else {
        btnApplyStyles.set_Text("Apply Sorting");
    }

    // Attach custom event handlers.
    myDataGridTableStyle1.add_AllowSortingChanged(
        new System.EventHandler(AllowSortingChanged_Handler));
    myDataGridTableStyle1.set_MappingName("Customers");
} //DataGridTableStyleSampleLoad

private void AllowSortingChanged_Handler(Object sender, EventArgs e)
{
    mylabel.set_Text("Sorting Status :" 
        + System.Convert.ToString(myDataGridTableStyle1.get_AllowSorting()));
} //AllowSortingChanged_Handler

private void btnApplyStyles_Click(Object sender, EventArgs e)
{
    if (myDataGridTableStyle1.get_AllowSorting() == true) {
        // Remove sorting.
        myDataGridTableStyle1.set_AllowSorting(false);
        btnApplyStyles.set_Text("Allow Sorting");
    }
    else {
        // Allow sorting.
        myDataGridTableStyle1.set_AllowSorting(true);
        btnApplyStyles.set_Text("Remove Sorting");
    }
    mylabel.set_Text("Sorting Status :" 
        + ((myDataGridTableStyle1.get_AllowSorting()) ? "True" : "False"));
    // Add the DataGridTableStyle to DataGrid.
    myDataGrid.get_TableStyles().Add(myDataGridTableStyle1);
} //btnApplyStyles_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DataGridTableStyle 类
DataGridTableStyle 成员
System.Windows.Forms 命名空间
AllowSortingChanged
DataGrid.AllowSorting 属性