0

This is my code code for the Page_Load Event

            OdbcConnection myConnection;
            DataSet dataSet = new DataSet();
            OdbcDataAdapter adapter;

            //making my connection
            myConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings  ["ODBC_ConnectionString"].ConnectionString);

            adapter = new OdbcDataAdapter("SELECT * from Company", myConnection);

            adapter.Fill(dataSet, "MyData");

            GridView1.DataSource = dataSet;
            Session["DataSource"] = dataSet;
            GridView1.DataBind();

This is my code for the PageIndexChanging event and it all works fine.

           DataSet ds = new DataSet();

            if (Session["DataSource"] != null)
                ds = ((DataSet)Session["DataSource"]);

            GridView1.DataSource = ds;
            GridView1.PageIndex = e.NewPageIndex;
            this.GridView1.DataBind();

Now what code do i need to create the Sorting event?

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ?????????????????????????
    }

Etienne

2 Answers 2

1

I usually do this:

 public string SortField {
            get {
                return (string) ViewState["_sortField"];
            }
            set {
                ViewState["_sortField"] = value;
            }
        }
        public string SortDir {
            get {
                return (string) ViewState["_sortDir"];
            }
            set {
                ViewState["_sortDir"] = value;
            }
        }

Put your code to do databinding into another Method because you have to call it during Sort, Paging, and when your page first loads. Call it DoDataBind() for example. Then you have in DoDataBind()

DataTable dt = yourDataSet.Tables[0];
dt.DefaultView.Sort = SortField + " " + SortDir;

GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();

Then your event looks like this:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {

            if (e.SortExpression == SortField && SortDir != "desc") {
                SortDir = "desc";
            }
            else {
                SortDir = "asc";
            }

            SortField = e.SortExpression;

            DoDataBind();
        }

Then in your aspx page you'll need to specify what the SortExpression is. For example something like this:

<asp:BoundField DataField="FIRSTNAME" 
HeaderText="First Name" SortExpression="FIRSTNAME" />
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply...Not sure what u mean with "Put your code to do databinding into another Method " What code? The code you gave me?
The code to databind the grid. So this: OdbcConnection myConnection; DataSet dataSet = new DataSet(); OdbcDataAdapter adapter; ...snip... adapter.Fill(dataSet, "MyData"); //Then the code I mentioned: DataTable dt = dataSet.Tables[0]; dt.DefaultView.Sort = SortField...etc
0

You can filter and sort your dataset using:

ds.Tables[0].Select(filterExp, sortExp, etc...);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.