关闭

DataGridView控件用法合集(六)

标签: vb.netobjectbuttonnulleachc#
491人阅读 评论(0) 收藏 举报

29. DataGridView行排序(点击列表头自动排序的设置)

30. DataGridView自动行排序(新追加值也会自动排序)

31. DataGridView自动行排序禁止情况下的排序

32. DataGridView指定列指定排序

 

29. DataGridView行排序(点击列表头自动排序的设置)

[VB.NET]

'並び替えができないようにする

For Each c As DataGridViewColumn In DataGridView1.Columns

    c.SortMode = DataGridViewColumnSortMode.NotSortable

Next c

30. DataGridView自动行排序(新追加值也会自动排序)

[VB.NET]

'フォームのLoadイベントハンドラ

Private Sub Form1_Load(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles MyBase.Load

    '自動的に並び替えられるようにする

    Dim c As DataGridViewColumn

    For Each c In DataGridView1.Columns

        c.SortMode = DataGridViewColumnSortMode.Automatic

    Next c

End Sub

'Button1Clickイベントハンドラ

Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles Button1.Click

    If DataGridView1.CurrentCell Is Nothing Then

        Return

    End If

    '並び替える列を決める

    Dim sortColumn As DataGridViewColumn = _

        DataGridView1.CurrentCell.OwningColumn

    '並び替えの方向(昇順か降順か)を決める

    Dim sortDirection As System.ComponentModel.ListSortDirection = _

        System.ComponentModel.ListSortDirection.Ascending

    If Not (DataGridView1.SortedColumn Is Nothing) AndAlso _

            DataGridView1.SortedColumn.Equals(sortColumn) Then

        sortDirection = IIf(DataGridView1.SortOrder = SortOrder.Ascending, _

            System.ComponentModel.ListSortDirection.Descending, _

            System.ComponentModel.ListSortDirection.Ascending)

    End If

    '並び替えを行う

    DataGridView1.Sort(sortColumn, sortDirection)

End Sub

31. DataGridView自动行排序禁止情况下的排序

'ColumnHeaderMouseClickイベントハンドラ

Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, _

       ByVal e As DataGridViewCellMouseEventArgs) _

        Handles DataGridView1.ColumnHeaderMouseClick

    Dim clickedColumn As DataGridViewColumn = _

        DataGridView1.Columns(e.ColumnIndex)

    If clickedColumn.SortMode <> DataGridViewColumnSortMode.Automatic Then

        Me.SortRows(clickedColumn, True)

    End If

End Sub

'RowsAddedイベントハンドラ

Private Sub DataGridView1_RowsAdded(ByVal sender As Object, _

        ByVal e As DataGridViewRowsAddedEventArgs) _

        Handles DataGridView1.RowsAdded

    Me.SortRows(DataGridView1.SortedColumn, False)

End Sub

'CellValueChangedイベントハンドラ

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, _

        ByVal e As DataGridViewCellEventArgs) _

        Handles DataGridView1.CellValueChanged

    If Not (DataGridView1.SortedColumn Is Nothing) AndAlso _

        e.ColumnIndex = DataGridView1.SortedColumn.Index Then

        Me.SortRows(DataGridView1.SortedColumn, False)

    End If

End Sub

''' <summary>

''' 指定された列を基準にして並び替えを行う

''' </summary>

''' <param name="sortColumn">基準にする列</param>

''' <param name="orderToggle">並び替えの方向をトグルで変更する</param>

Private Sub SortRows(ByVal sortColumn As DataGridViewColumn, _

        ByVal orderToggle As Boolean)

    If sortColumn Is Nothing Then

        Return

    End If

    '今までの並び替えグリフを消す

    If sortColumn.SortMode = DataGridViewColumnSortMode.Programmatic AndAlso _

        Not (DataGridView1.SortedColumn Is Nothing) AndAlso _

        Not DataGridView1.SortedColumn.Equals(sortColumn) Then

        DataGridView1.SortedColumn.HeaderCell.SortGlyphDirection = _

            SortOrder.None

    End If

    '並び替えの方向(昇順か降順か)を決める

    Dim sortDirection As System.ComponentModel.ListSortDirection

    If orderToggle Then

        sortDirection = IIf(DataGridView1.SortOrder = SortOrder.Descending, _

            System.ComponentModel.ListSortDirection.Ascending, _

            System.ComponentModel.ListSortDirection.Descending)

    Else

        sortDirection = IIf(DataGridView1.SortOrder = SortOrder.Descending, _

            System.ComponentModel.ListSortDirection.Descending, _

            System.ComponentModel.ListSortDirection.Ascending)

    End If

    Dim sOrder As SortOrder = _

        IIf(sortDirection = System.ComponentModel.ListSortDirection.Ascending, _

            SortOrder.Ascending, SortOrder.Descending)

    '並び替えを行う

    DataGridView1.Sort(sortColumn, sortDirection)

    If sortColumn.SortMode = DataGridViewColumnSortMode.Programmatic Then

        '並び替えグリフを変更

        sortColumn.HeaderCell.SortGlyphDirection = sOrder

    End If

End Sub

 [C#]

//フォームのLoadイベントハンドラ

private void Form1_Load(object sender, EventArgs e)

{

    //イベントハンドラの追加

    DataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(

        DataGridView1_RowsAdded);

    DataGridView1.CellValueChanged += new DataGridViewCellEventHandler(

        DataGridView1_CellValueChanged);

    DataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(

        DataGridView1_ColumnHeaderMouseClick);

}

//ColumnHeaderMouseClickイベントハンドラ

private void DataGridView1_ColumnHeaderMouseClick(object sender,

    DataGridViewCellMouseEventArgs e)

{

    DataGridViewColumn clickedColumn = DataGridView1.Columns[e.ColumnIndex];

    if (clickedColumn.SortMode != DataGridViewColumnSortMode.Automatic)

        this.SortRows(clickedColumn, true);

}

//RowsAddedイベントハンドラ

private void DataGridView1_RowsAdded(object sender,

    DataGridViewRowsAddedEventArgs e)

{

    this.SortRows(DataGridView1.SortedColumn, false);

}

//CellValueChangedイベントハンドラ

private void DataGridView1_CellValueChanged(object sender,

    DataGridViewCellEventArgs e)

{

    if (DataGridView1.SortedColumn != null &&

        e.ColumnIndex == DataGridView1.SortedColumn.Index)

        this.SortRows(DataGridView1.SortedColumn, false);

}

/// <summary>

/// 指定された列を基準にして並び替えを行う

/// </summary>

/// <param name="sortColumn">基準にする列</param>

/// <param name="orderToggle">並び替えの方向をトグルで変更する</param>

private void SortRows(DataGridViewColumn sortColumn, bool orderToggle)

{

    if (sortColumn == null)

        return;

    //今までの並び替えグリフを消す

    if (sortColumn.SortMode == DataGridViewColumnSortMode.Programmatic &&

        DataGridView1.SortedColumn != null &&

        !DataGridView1.SortedColumn.Equals(sortColumn))

    {

        DataGridView1.SortedColumn.HeaderCell.SortGlyphDirection =

            SortOrder.None;

    }

    //並び替えの方向(昇順か降順か)を決める

    ListSortDirection sortDirection;

    if (orderToggle)

    {

        sortDirection =

            DataGridView1.SortOrder == SortOrder.Descending ?

            ListSortDirection.Ascending : ListSortDirection.Descending;

    }

    else

    {

        sortDirection =

            DataGridView1.SortOrder == SortOrder.Descending ?

            ListSortDirection.Descending : ListSortDirection.Ascending;

    }

    SortOrder sortOrder =

        sortDirection == ListSortDirection.Ascending ?

        SortOrder.Ascending : SortOrder.Descending;

    //並び替えを行う

    DataGridView1.Sort(sortColumn, sortDirection);

    if (sortColumn.SortMode == DataGridViewColumnSortMode.Programmatic)

    {

        //並び替えグリフを変更

        sortColumn.HeaderCell.SortGlyphDirection = sortOrder;

    }

}

32. DataGridView指定列指定排序

 [VB.NET]

'DataGridView1にバインドされているDataTableを取得

Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)

'DataViewを取得

Dim dv As DataView = dt.DefaultView

'Column1Column2で昇順に並び替える

dv.Sort = "Column1, Column2 ASC"

'2つの列のヘッダーに並び替えグリフを表示する

DataGridView1.Columns("Column1").HeaderCell.SortGlyphDirection = _

    SortOrder.Ascending

DataGridView1.Columns("Column2").HeaderCell.SortGlyphDirection = _

    SortOrder.Ascending

 [C#]

//DataGridView1にバインドされているDataTableを取得

DataTable dt = (DataTable)DataGridView1.DataSource;

//DataViewを取得

DataView dv = dt.DefaultView;

//Column1Column2で昇順に並び替える

dv.Sort = "Column1, Column2 ASC";

//2つの列のヘッダーに並び替えグリフを表示する

DataGridView1.Columns["Column1"].HeaderCell.SortGlyphDirection =

    SortOrder.Ascending;

DataGridView1.Columns["Column2"].HeaderCell.SortGlyphDirection =

    SortOrder.Ascending; 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36561次
    • 积分:489
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:13篇
    • 译文:1篇
    • 评论:10条