Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Real problem with GridView TemplateField Sorting

Real problem with GridView TemplateField Sorting

Scheduled Pinned Locked Moved ASP.NET
cssdatabasesysadminalgorithmshelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxdd 7
    wrote on last edited by
    #1

    I have a big GridView (with, edit,update,delete and insert operations),(forgive all this code, I cant present my problem other way :(( The template field is like this: (the gridview has OnPageIndexChanging="GridView3_PageIndexChanging" OnSorting="GridView3_Sorting" AllowSorting="True" properties)

    <asp:TemplateField HeaderText="Nome" SortExpression="Nome">
    <EditItemTemplate>
    <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtNName" runat="server" width="75px" Visible='<%# (bool) show_hide_insert() %>'> </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>

    The Grid View is populated this way:

    public void TempTable()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Name", typeof(String));
    dt.Columns.Add("Year", typeof(String));

    // i've erased some colummns here

        dt.Columns.Add("Image", typeof(String));
    
    
    
        Session\["data"\] = dt;
        Temp = dt.Copy();
    

    // query. ...

        DataSet ds = GetData(query);
        GridView3.DataSource = ds;
        GridView3.DataBind();
    
    }
    

    DataSet GetData(String queryString)
    {

        string connectionString;
        connectionString = WebConfigurationManager.ConnectionStrings\["ConnectionString1"\].ConnectionString;
        DataSet ds = new DataSet();
    
        try
        {
         
            SqlConnection Conn = new SqlConnection(connectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
            adapter.Fill(ds);
    
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
    
        }
    
        return ds;
    }
    

    public DataTable Temp
    {
    get
    {
    object o = ViewState["Temp"];
    if (o == null)
    {
    DataTable dt = new DataTable();

    M A 2 Replies Last reply
    0
    • M Maxdd 7

      I have a big GridView (with, edit,update,delete and insert operations),(forgive all this code, I cant present my problem other way :(( The template field is like this: (the gridview has OnPageIndexChanging="GridView3_PageIndexChanging" OnSorting="GridView3_Sorting" AllowSorting="True" properties)

      <asp:TemplateField HeaderText="Nome" SortExpression="Nome">
      <EditItemTemplate>
      <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
      <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
      </ItemTemplate>
      <FooterTemplate>
      <asp:TextBox ID="txtNName" runat="server" width="75px" Visible='<%# (bool) show_hide_insert() %>'> </asp:TextBox>
      </FooterTemplate>
      </asp:TemplateField>

      The Grid View is populated this way:

      public void TempTable()
      {
      DataTable dt = new DataTable();
      dt.Columns.Add("ID", typeof(int));
      dt.Columns.Add("Name", typeof(String));
      dt.Columns.Add("Year", typeof(String));

      // i've erased some colummns here

          dt.Columns.Add("Image", typeof(String));
      
      
      
          Session\["data"\] = dt;
          Temp = dt.Copy();
      

      // query. ...

          DataSet ds = GetData(query);
          GridView3.DataSource = ds;
          GridView3.DataBind();
      
      }
      

      DataSet GetData(String queryString)
      {

          string connectionString;
          connectionString = WebConfigurationManager.ConnectionStrings\["ConnectionString1"\].ConnectionString;
          DataSet ds = new DataSet();
      
          try
          {
           
              SqlConnection Conn = new SqlConnection(connectionString);
              SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
              adapter.Fill(ds);
      
          }
          catch (Exception ex)
          {
              Response.Write(ex.Message);
      
          }
      
          return ds;
      }
      

      public DataTable Temp
      {
      get
      {
      object o = ViewState["Temp"];
      if (o == null)
      {
      DataTable dt = new DataTable();

      M Offline
      M Offline
      Maxdd 7
      wrote on last edited by
      #2

      I tried to work around this:

      DataTable dataTable = Temp as DataTable;

          if (dataTable != null)
          {
              DataView dataView = new DataView(dataTable);
              dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
      
              Temp.AcceptChanges();
              GridView3.DataSource = dataView;
              GridView3.DataBind();
      

      but it not working. When I try to sort, the GridView disappears. I think I not seeing the way datatable works (or its not even necessary), so if anyone can give me an hint, I appreciate very much.

      C 1 Reply Last reply
      0
      • M Maxdd 7

        I tried to work around this:

        DataTable dataTable = Temp as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
        
                Temp.AcceptChanges();
                GridView3.DataSource = dataView;
                GridView3.DataBind();
        

        but it not working. When I try to sort, the GridView disappears. I think I not seeing the way datatable works (or its not even necessary), so if anyone can give me an hint, I appreciate very much.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Have you done any sort of debugging ? What have you found ? Why don't you use descriptive variable names ?

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        M 1 Reply Last reply
        0
        • M Maxdd 7

          I have a big GridView (with, edit,update,delete and insert operations),(forgive all this code, I cant present my problem other way :(( The template field is like this: (the gridview has OnPageIndexChanging="GridView3_PageIndexChanging" OnSorting="GridView3_Sorting" AllowSorting="True" properties)

          <asp:TemplateField HeaderText="Nome" SortExpression="Nome">
          <EditItemTemplate>
          <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
          </EditItemTemplate>
          <ItemTemplate>
          <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
          </ItemTemplate>
          <FooterTemplate>
          <asp:TextBox ID="txtNName" runat="server" width="75px" Visible='<%# (bool) show_hide_insert() %>'> </asp:TextBox>
          </FooterTemplate>
          </asp:TemplateField>

          The Grid View is populated this way:

          public void TempTable()
          {
          DataTable dt = new DataTable();
          dt.Columns.Add("ID", typeof(int));
          dt.Columns.Add("Name", typeof(String));
          dt.Columns.Add("Year", typeof(String));

          // i've erased some colummns here

              dt.Columns.Add("Image", typeof(String));
          
          
          
              Session\["data"\] = dt;
              Temp = dt.Copy();
          

          // query. ...

              DataSet ds = GetData(query);
              GridView3.DataSource = ds;
              GridView3.DataBind();
          
          }
          

          DataSet GetData(String queryString)
          {

              string connectionString;
              connectionString = WebConfigurationManager.ConnectionStrings\["ConnectionString1"\].ConnectionString;
              DataSet ds = new DataSet();
          
              try
              {
               
                  SqlConnection Conn = new SqlConnection(connectionString);
                  SqlDataAdapter adapter = new SqlDataAdapter(queryString, connectionString);
                  adapter.Fill(ds);
          
              }
              catch (Exception ex)
              {
                  Response.Write(ex.Message);
          
              }
          
              return ds;
          }
          

          public DataTable Temp
          {
          get
          {
          object o = ViewState["Temp"];
          if (o == null)
          {
          DataTable dt = new DataTable();

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          There is lot easier way to handle sorting. Use onsorting eventhandler to give automatic sorting expression to a column.

          Maxdd 7 wrote:

          DataView dataView = new DataView(dataTable);

          Rather Use DataView dataView = dataTable.DefaultView

          Maxdd 7 wrote:

          Session["data"] = dt; Why do you need Session to store datatable. .. :doh: :doh: :doh: Also what exactly the problem is? Check Page_Load, if you have set DataSource and DataBind for every page postback.. You should filter out using if(IsPostBack) :)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          1 Reply Last reply
          0
          • C Christian Graus

            Have you done any sort of debugging ? What have you found ? Why don't you use descriptive variable names ?

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            M Offline
            M Offline
            Maxdd 7
            wrote on last edited by
            #5

            Christian Graus wrote:

            Have you done any sort of debugging ? What have you found ? Why don't you use descriptive variable names ?

            I have found nothing.. I forgot to present my exact problem: if I run with the code in first post, and I try to sort, it happens nothing. With the "solution" of my second post, when I sort the Gridview disappears. What is exactly descriptive variable names? Can you give an example?

            Rather Use DataView dataView = dataTable.DefaultView Maxdd 7 wrote: Session["data"] = dt; Why do you need Session to store datatable. .. D'Oh! D'Oh! D'Oh! Also what exactly the problem is? Check Page_Load, if you have set DataSource and DataBind for every page postback.. You should filter out using if(IsPostBack)

            Yes, in fact Session["data"] its nothing ;P My temp table is loaded this way: if (!Page.IsPostback) TempTable(); So I suppose I set DataSource and Databind just one time.. but its strange because if I do if(Page.Ispostback) my gridview does not appears... Your advice, DataView dataView = dataTable.DefaultView, should I apply it in the code of my first post ( DataTable dataTable = GridView3.DataSource as DataTable; ) or the second one ? (DataTable dataTable = Temp as DataTable;) I tried with both but the problem still exists.. (first does not happen nothing, second disappear :( Dont know else to do... Thanks for your help!

            modified on Tuesday, November 24, 2009 4:50 PM

            C 1 Reply Last reply
            0
            • M Maxdd 7

              Christian Graus wrote:

              Have you done any sort of debugging ? What have you found ? Why don't you use descriptive variable names ?

              I have found nothing.. I forgot to present my exact problem: if I run with the code in first post, and I try to sort, it happens nothing. With the "solution" of my second post, when I sort the Gridview disappears. What is exactly descriptive variable names? Can you give an example?

              Rather Use DataView dataView = dataTable.DefaultView Maxdd 7 wrote: Session["data"] = dt; Why do you need Session to store datatable. .. D'Oh! D'Oh! D'Oh! Also what exactly the problem is? Check Page_Load, if you have set DataSource and DataBind for every page postback.. You should filter out using if(IsPostBack)

              Yes, in fact Session["data"] its nothing ;P My temp table is loaded this way: if (!Page.IsPostback) TempTable(); So I suppose I set DataSource and Databind just one time.. but its strange because if I do if(Page.Ispostback) my gridview does not appears... Your advice, DataView dataView = dataTable.DefaultView, should I apply it in the code of my first post ( DataTable dataTable = GridView3.DataSource as DataTable; ) or the second one ? (DataTable dataTable = Temp as DataTable;) I tried with both but the problem still exists.. (first does not happen nothing, second disappear :( Dont know else to do... Thanks for your help!

              modified on Tuesday, November 24, 2009 4:50 PM

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Maxdd 7 wrote:

              What is exactly descriptive variable names? Can you give an example?

              reportList as opposed to listView1.

              Maxdd 7 wrote:

              So I suppose I set DataSource and Databind just one time.. but its strange because if I do if(Page.Ispostback) my gridview does not appears...

              Are you using viewstate, or is it turned off ?

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              M 1 Reply Last reply
              0
              • C Christian Graus

                Maxdd 7 wrote:

                What is exactly descriptive variable names? Can you give an example?

                reportList as opposed to listView1.

                Maxdd 7 wrote:

                So I suppose I set DataSource and Databind just one time.. but its strange because if I do if(Page.Ispostback) my gridview does not appears...

                Are you using viewstate, or is it turned off ?

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                M Offline
                M Offline
                Maxdd 7
                wrote on last edited by
                #7

                Christian Graus wrote:

                Are you using viewstate, or is it turned off ?

                public DataTable Temp
                {
                get
                {
                object o = ViewState["Temp"];
                if (o == null)
                {
                DataTable dt = new DataTable();
                return dt;
                }
                else
                return (DataTable)o;
                }
                set
                {
                ViewState["Temp"] = value;
                }
                }

                Have I answered to your question? If not I'll try again.

                C 1 Reply Last reply
                0
                • M Maxdd 7

                  Christian Graus wrote:

                  Are you using viewstate, or is it turned off ?

                  public DataTable Temp
                  {
                  get
                  {
                  object o = ViewState["Temp"];
                  if (o == null)
                  {
                  DataTable dt = new DataTable();
                  return dt;
                  }
                  else
                  return (DataTable)o;
                  }
                  set
                  {
                  ViewState["Temp"] = value;
                  }
                  }

                  Have I answered to your question? If not I'll try again.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  This doesn't tell me if viewstate is turned on for the page or not. Have you set breakpoints to see if your data here is persisted or if it's lost ?

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  M 1 Reply Last reply
                  0
                  • C Christian Graus

                    This doesn't tell me if viewstate is turned on for the page or not. Have you set breakpoints to see if your data here is persisted or if it's lost ?

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                    M Offline
                    M Offline
                    Maxdd 7
                    wrote on last edited by
                    #9

                    Christian Graus wrote:

                    Have you set breakpoints to see if your data here is persisted or if it's lost ?

                    Could you recommend where to set breakpoints? I am not doing breakpoints but I'm using Response.write in many places to know where of there's any data not executed. But I think I know the problem: with the solution in my second post (after sort grid disappears), looks like cant sort temporary table... maybe I should use another temp table ?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups