Real problem with GridView TemplateField Sorting
-
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(); -
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();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.
-
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.
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.
-
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();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
andDataBind
for every page postback.. You should filter out usingif(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 -
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.
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
-
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
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.
-
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.
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.
-
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.
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.
-
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.
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 ?