在窗口应用程序中的Gridview上实现排序 [英] Implemented sorting on Gridview in window application

查看:44
本文介绍了在窗口应用程序中的Gridview上实现排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在C#的窗口应用程序的gridview中实现排序.

谢谢

Hello All,

I want to implemented sorting in gridview in window application in c#.

Thanks

推荐答案

hi 试试这个
对于GridView分页和排序,请在aspx网页的源文件中编写以下代码:
hi try this
For GridView paging and sorting write the following code in source file of aspx webpage:
<asp:GridView
              ID="grdCause"
              runat="server
              EnableSortingAndPagingCallback="True"
              AllowPaging="True"
              AllowSorting="True"
              onpageindexchanging="grdCause_PageIndexChanging"
              onsorting="grdCause_Sorting"
         >
</asp:GridView>



以下方法用于加载Gridview.在此方法中,获取数据集并将该数据集绑定到Gridview.
ExecuteProcedure是我的sqlhelper类中的方法.对于该方法,请单击此处.



The following method is for Loading the Gridview .In this method am taking dataset and bind that dataset to Gridview.
ExecuteProcedure is the method in my sqlhelper class.For that method Click here.

private void LoadGrid()
    {

        DataSet ds = dal.ExecuteProcudere("CauseSelectAll", ht);
        grdCause.DataSource = ds.Tables[0];
        grdCause.DataBind();
    }


Gridview pageindexChanging Event  for paging:
protected void grdCause_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCause.PageIndex = e.NewPageIndex;
        LoadGrid();
    }

Gridview Sorting  Event  for Sorting:

    protected void grdCause_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }


    }

Use the following method for Sorting:
private void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        LoadGrid();
        DataTable dt = grdCause.DataSource as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;

        grdCause.DataSource = dv;
        grdCause.DataBind();

    }
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value;
 }
    }



试试这个

hi
try out this

using System;
using System.ComponentModel;
using System.Windows.Forms;

class Form1 : Form
{
    private Button sortButton = new Button();
    private DataGridView dataGridView1 = new DataGridView();

    // Initializes the form. 
    // You can replace this code with designer-generated code. 
    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.SelectionMode =
            DataGridViewSelectionMode.ColumnHeaderSelect;
        dataGridView1.MultiSelect = false;

        sortButton.Dock = DockStyle.Bottom;
        sortButton.Text = "Sort";

        Controls.Add(dataGridView1);
        Controls.Add(sortButton);
        Text = "DataGridView programmatic sort demo";
    }

    // Establishes the main entry point for the application.
    [STAThreadAttribute()]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    // Populates the DataGridView. 
    // Replace this with your own code to populate the DataGridView. 
    public void PopulateDataGridView()
    {
        // Add columns to the DataGridView.
        dataGridView1.ColumnCount = 2;
        dataGridView1.Columns[0].HeaderText = "Last Name";
        dataGridView1.Columns[1].HeaderText = "City";
        // Put the new columns into programmatic sort mode
        dataGridView1.Columns[0].SortMode = 
            DataGridViewColumnSortMode.Programmatic;
        dataGridView1.Columns[1].SortMode =
            DataGridViewColumnSortMode.Programmatic;

        // Populate the DataGridView.
        dataGridView1.Rows.Add(new string[] { "Parker", "Seattle" });
        dataGridView1.Rows.Add(new string[] { "Watson", "Seattle" });
        dataGridView1.Rows.Add(new string[] { "Osborn", "New York" });
        dataGridView1.Rows.Add(new string[] { "Jameson", "New York" });
        dataGridView1.Rows.Add(new string[] { "Brock", "New Jersey" });
    }

    protected override void OnLoad(EventArgs e)
    {
        sortButton.Click += new EventHandler(sortButton_Click);

        PopulateDataGridView();
        base.OnLoad(e);
    }

    private void sortButton_Click(object sender, System.EventArgs e)
    {
        // Check which column is selected, otherwise set NewColumn to null.
        DataGridViewColumn newColumn =
            dataGridView1.Columns.GetColumnCount(
            DataGridViewElementStates.Selected) == 1 ?
            dataGridView1.SelectedColumns[0] : null;

        DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
        ListSortDirection direction;

        // If oldColumn is null, then the DataGridView is not currently sorted. 
        if (oldColumn != null)
        {
            // Sort the same column again, reversing the SortOrder. 
            if (oldColumn == newColumn &&
                dataGridView1.SortOrder == SortOrder.Ascending)
            {
                direction = ListSortDirection.Descending;
            }
            else
            {
                // Sort a new column and remove the old SortGlyph.
                direction = ListSortDirection.Ascending;
                oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
            }
        }
        else
        {
            direction = ListSortDirection.Ascending;
        }

        // If no column has been selected, display an error dialog  box. 
        if (newColumn == null)
        {
            MessageBox.Show("Select a single column and try again.",
                "Error: Invalid Selection", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
        else
        {
            dataGridView1.Sort(newColumn, direction);
            newColumn.HeaderCell.SortGlyphDirection =
                direction == ListSortDirection.Ascending ?
                SortOrder.Ascending : SortOrder.Descending;
        }
    }
}


尝试类似的方法,
在DataGridView上有一个称为排序"的方法:

Try something Like this,
There''s a method on the DataGridView called "Sort":

this.dataGridView1.Sort(this.dataGridView1.Columns["Name"],ListSortDirection.Ascending);



这将以编程方式对您的datagridview进行排序.

问候和感谢
sarva



This will programmatically sort your datagridview.

regards and thanks
sarva


这篇关于在窗口应用程序中的Gridview上实现排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆