views:

265

answers:

0

I have created a webpart which contains a spgridview that is bound to a dataview.My problem is that even though i have set the allowsorting option to true ,the gridview header shows no link buttons. The code for the webpart is as follows:

  public void createthegridviewfields()
    {

        foreach (string col in colnames)
        {
            if (col == null || col == string.Empty)
                break;
            BoundField bf = new BoundField();
            bf.DataField = col;
            bf.HeaderText = col.ToUpper();
            bf.SortExpression = col;
            gr.Columns.Add(bf);
        }

    }
    public void createthegridview()
    {
        createthegridviewfields();
        gr.DataSource = gr_view;
        gr.DataBind();

    }
    protected override void CreateChildControls()
    {

     connstring = "Provider=SQLNCLI.1;Data Source=" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ";Initial Catalog=" + dbname.ToString() + ";Integrated Security=SSPI;Persist Security Info=False;";
                                       fields = removewhitespaces(fields);
                    Regex r = new Regex(",+");
                    fields = r.Replace(fields, ",");
                    querystring = "select " + fields + " from " + tablename;

                        myfunctions f = new myfunctions();
                        System.Data.DataTable t = f.generatedatasetfromsqlquery(querystring, connstring);//this generates the datatable which is filled with the queried values.
                        gr_view = new DataView(t);
                        gr.AutoGenerateColumns = false;
                        createthegridview();
                        gr.AllowSorting = true;
                        gr.Sorting += new GridViewSortEventHandler(gr_Sorting);
                                                                                                                    this.Controls.Add(gr);
                        this.Controls.Add(new LiteralControl("<br/>"));
                        base.CreateChildControls();
                    } 
     void gr_Sorting(object sender, GridViewSortEventArgs e)
    {
        string lastExpression = "";
        if (ViewState["SortExpression"] != null)
            lastExpression = ViewState["SortExpression"].ToString();

        string lastDirection = "asc";
        if (ViewState["SortDirection"] != null)
            lastDirection = ViewState["SortDirection"].ToString();

        string newDirection = "asc";
        if (e.SortExpression == lastExpression)
            newDirection = (lastDirection == "asc") ? "desc" : "asc";

        ViewState["SortExpression"] = e.SortExpression;
        ViewState["SortDirection"] = newDirection;

        gr_view.Sort = e.SortExpression + " " + newDirection;
        gr.DataBind();

    }