Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting a gridview

Status
Not open for further replies.

wrexhamafc234

Programmer
Oct 23, 2006
18
GB
hello, im trying to sort a gridview

Code:
protected void Page_Load(object sender, EventArgs e)
    {
        DataTable alerttable = new DataTable("alerttable");
        alerttable.Columns.Add("ID");
        alerttable.Columns.Add("Title");
        alerttable.Columns.Add("Section");
        alerttable.Columns.Add("Priority");
        alerttable.Columns.Add("AlertDate");
        alerttable.Columns.Add("Type");
        alerttable.Columns.Add("Details");
        alerttable = AlertDataFunctions.GetAllAlertDetails(userID, sort, ViewType.Text);

        GridView1.DataSource = alerttable;
        GridView1.Sorting += new GridViewSortEventHandler(GridView1_Sorting);
        if (AlertName.Checked == true)
        {
            GridView1.Sort("Title", SortDirection.Ascending);
        }
        else if (Date.Checked == true)
        {
            GridView1.Sort("AlertDate", SortDirection.Ascending);
        }

        GridView1.DataBind();
}

    void GridView1_Sorting(Object sender, GridViewSortEventArgs e)
    {


    }

However, im not too sure what to put into the GridView1_Sorting function.

Any ideas?
 
1. put your page_load into a seperate event and call it like below (call LoadData() whatever you want).
2. seems in your theory, the sort will depend on a checkbox, that will hold its value between postbacks, only if you check for postback as below, so just call your void again in the sorting event, and it might work for you.
Code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
              LoadData();
        }
    }

public void LoadData();
    {
        DataTable alerttable = new DataTable("alerttable");
        ...
    }
    public void GridView1_Sorting(Object sender, GridViewSortEventArgs e)
    {
        LoadData();
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top