Click here to Skip to main content
14,981,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am writing to seek help in creating 'sorting' feature on my gridview using code behind method. This is what i have so far and currently, its not sorting anything on the client-side. I am also getting the following error (Cannot implicitly convert type 'void' to 'System.Data.DataTable'), on the following line:

C#
DataTable table = BindGridview();


Any I missing any additional information? Any help would be very much appreciated.

ASP.NET
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" >
    </asp:GridView>


C#
protected void SortRecords(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;
    string direction = string.Empty;

    if (SortDirection == SortDirection.Ascending)
    {
        SortDirection = SortDirection.Descending;
        direction = " DESC";
    }
    else
    {
        SortDirection = SortDirection.Ascending;
        direction = " ASC";
    }
   DataTable table = BindGridview();
   table.DefaultView.Sort = sortExpression + direction;
   GridView1.DataSource = table;
   GridView1.DataBind();
}

public SortDirection SortDirection
{
    get
    {
        if (ViewState["SortDirection"] == null)
        {
            ViewState["SortDirection"] = SortDirection.Ascending;
        }
        return (SortDirection)ViewState["SortDirection"];
    }
    set
    {
        ViewState["SortDirection"] = value;

    }

}


Further functional methods:

C#
protected void BindGridview()
     //public DataSet BindGridView()
    {

        string query = "select top 100 [Name], CUSIP, ISINs, [Size (m)],[Current size],priceTalk, [Decimal price], Cover, Type, UploadDate  from [dbo].[database_BWICs]";
        DataSet dt = GetData(query);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    private static DataSet GetData(string query)
    {
        DataSet dt = new DataSet();
        SqlCommand cmd = new SqlCommand(query);
        String constr = ConfigurationManager.ConnectionStrings["####"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
Posted
Updated 15-Jan-14 6:05am
v4
Comments
Karthik_Mahalingam 15-Jan-14 12:01pm
   
post your full code..
something is missing out in the code..
miss786 15-Jan-14 12:08pm
   
Hi, Thank you for your response. I have updated my post with entire code I am currently using. If I am missing a method or something to make this functionality work, please do let me know. Thank you for your time and help.

Quote:

C#
DataTable table = this.BindGridView();

does not contain a definition for 'BindGridView' and no extension method 'BindGridView' accepting a first argument
The Exception is quite clear. There is no method like that.

I guess you should do like...
C#
DataTable table = BindGridView();

Else post the function code.
Comments
miss786 15-Jan-14 12:03pm
   
Hi, Thank you so much for your suggestion. I tired updating my code using your suggestion and now I am experiencing a new error (Cannot implicitly convert type 'void' to 'System.Data.DataTable'). I tried changing the 'DataTable table' function to 'Dataset' but that again gave me bunch of more errors. I have updated my post with additional methods as requested. Any suggestion would be most welcome. Thank you.
   
That is because the return type of that function is void, but you are assigning it to a DataTable.
You should do like below... You should call GetData method.

DataSet table = GetData();
table.DefaultView.Sort = sortExpression + direction;
GridView1.DataSource = table;
GridView1.DataBind();
Karthik_Mahalingam 15-Jan-14 13:11pm
   
given 5!!! because i know you would ve given right solution, if OP posted full code at the begining..
since i got the right comment from OP, my solution was right..
   
Thanks a lot bro. :)
Karthik_Mahalingam 16-Jan-14 4:23am
   
welcome TD :)
do like this..

C#
protected  DataTable GetDataTable () // new method
     
    {

        string query = "select top 100 [Name], CUSIP, ISINs, [Size (m)],[Current size],priceTalk, [Decimal price], Cover, Type, UploadDate  from [dbo].[database_BWICs]";
        DataSet dt = GetData(query);
       

return dt.Tables[0];
 
    }



C#
protected void BindGridview()  // modify this method
     //public DataSet BindGridView()
    {
 
        var dt = GetDataTable( );
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }




C#
protected void SortRecords(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        string direction = string.Empty;

        if (SortDirection == SortDirection.Ascending)
        {
            SortDirection = SortDirection.Descending;
            direction = " DESC";
        }
        else
        {
            SortDirection = SortDirection.Ascending;
            direction = " ASC";
        }
       DataTable table = GetDataTable();
       table.DefaultView.Sort = sortExpression + direction;
       GridView1.DataSource = table;
       GridView1.DataBind();
    }

    public SortDirection SortDirection
    {
        get
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;

        }

    }
   
Comments
miss786 15-Jan-14 12:23pm
   
Your a Star!! Thank you so much for help and your time in writing a thorough solution. I am very grateful to your help. Many thanks.
Karthik_Mahalingam 15-Jan-14 12:47pm
   
Thanks miss786 :)
Karthik_Mahalingam 16-Jan-14 4:23am
   
Thanks TD :)
   
Welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900