DatGrid Paging Event not firing
-
I use DataGrid1.AutoGenerateColumns = true; My Datagrid paging and sorting event work well,but the DataGrid Cell auto wrap.I set ItemStyle.Wrap = false,It wrap too. I set DataGrid1.AutoGenerateColumns = false,set ItemStyle.Wrap = false,the grid cell never wrap,but page and sort event never fireing,this is why?
-
I use DataGrid1.AutoGenerateColumns = true; My Datagrid paging and sorting event work well,but the DataGrid Cell auto wrap.I set ItemStyle.Wrap = false,It wrap too. I set DataGrid1.AutoGenerateColumns = false,set ItemStyle.Wrap = false,the grid cell never wrap,but page and sort event never fireing,this is why?
Hi Richard, Richard White wrote: but the DataGrid Cell auto wrap.I set ItemStyle.Wrap = false,It wrap too. Can you check the style of the datagrid control to see if the width property is too short? Setting the ItemStyle.Wrap affects the row of the table (the tr element), so you might want to try to set nowrap for the cell(the td element) instead of the row. Richard White wrote: but page and sort event never fireing,this is why Can you make sure that the event handlers are wired up to the events of the datagrid and other things still remain such as the AllowSorting, SortExpression properties ...? Can you post a snippet of the datagrid markup here?
-
I use DataGrid1.AutoGenerateColumns = true; My Datagrid paging and sorting event work well,but the DataGrid Cell auto wrap.I set ItemStyle.Wrap = false,It wrap too. I set DataGrid1.AutoGenerateColumns = false,set ItemStyle.Wrap = false,the grid cell never wrap,but page and sort event never fireing,this is why?
-
Hi Richard, Richard White wrote: but the DataGrid Cell auto wrap.I set ItemStyle.Wrap = false,It wrap too. Can you check the style of the datagrid control to see if the width property is too short? Setting the ItemStyle.Wrap affects the row of the table (the tr element), so you might want to try to set nowrap for the cell(the td element) instead of the row. Richard White wrote: but page and sort event never fireing,this is why Can you make sure that the event handlers are wired up to the events of the datagrid and other things still remain such as the AllowSorting, SortExpression properties ...? Can you post a snippet of the datagrid markup here?
I set width of datagrid is "100%",It seemed if the there have more colummn,the datagrid which set DataGrid1.AutoGenerateColumns = true will auto wrap. // DataGrid1.AutoGenerateColumns = true code
OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL,cn); dataset = new DataSet(); adapter.Fill(dataset,"myTable"); DataView dataview = new DataView(dataset.Tables[0]); DataGrid1.DataSource = dataview; DataGrid1.DataBind();
up code page and sort event work well,but some colummn auto wrap -------------------------------------------------------------- // DataGrid1.AutoGenerateColumns = false codeDataGrid1.AutoGenerateColumns = false; BoundColumn dgc = null; string sFieldName = ""; dgc = new BoundColumn(); sFieldName = "ROWNUM"; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.ItemStyle.Height = new Unit(15); dgc.ItemStyle.Wrap = false; DataGrid1.Columns.Add(dgc); .......... .......... OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL,cn); dataset = new DataSet(); adapter.Fill(dataset,"myTable"); DataView dataview = new DataView(dataset.Tables[0]); DataGrid1.DataSource = dataview; DataGrid1.DataBind();
up code cell no wrap,but page and sort event not firing any more -
I set width of datagrid is "100%",It seemed if the there have more colummn,the datagrid which set DataGrid1.AutoGenerateColumns = true will auto wrap. // DataGrid1.AutoGenerateColumns = true code
OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL,cn); dataset = new DataSet(); adapter.Fill(dataset,"myTable"); DataView dataview = new DataView(dataset.Tables[0]); DataGrid1.DataSource = dataview; DataGrid1.DataBind();
up code page and sort event work well,but some colummn auto wrap -------------------------------------------------------------- // DataGrid1.AutoGenerateColumns = false codeDataGrid1.AutoGenerateColumns = false; BoundColumn dgc = null; string sFieldName = ""; dgc = new BoundColumn(); sFieldName = "ROWNUM"; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.ItemStyle.Height = new Unit(15); dgc.ItemStyle.Wrap = false; DataGrid1.Columns.Add(dgc); .......... .......... OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL,cn); dataset = new DataSet(); adapter.Fill(dataset,"myTable"); DataView dataview = new DataView(dataset.Tables[0]); DataGrid1.DataSource = dataview; DataGrid1.DataBind();
up code cell no wrap,but page and sort event not firing any more+ Case1: Yes, maybe. You can try to add the
nowrap
for each cell of the column. Basically, the automatically generated columns are not contained in the Columns collection, you can create an event handler for the ItemDataBound or ItemCreated events of the datagrid, in the event handler you can set for the cells. + Case2: I thought you might have placed the columns for the datagrid at design time when you set the AutoGenerateColumns to false. But here, you dynamically add the columns to the grid, so you need to make sure that the dynamic columns are added again when the page posts back, and more importantly, the adding of the columns should happen prior to the loading of the ViewState data of the grid. For example, here you can try to override the LoadViewState method of the page instance:protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);this.AddColumns();
}Another way is to add the columns in the OnInit event. For more information, you can see Control Execution Lifecycle (.NET Framework Developer's Guide)[^]
-
+ Case1: Yes, maybe. You can try to add the
nowrap
for each cell of the column. Basically, the automatically generated columns are not contained in the Columns collection, you can create an event handler for the ItemDataBound or ItemCreated events of the datagrid, in the event handler you can set for the cells. + Case2: I thought you might have placed the columns for the datagrid at design time when you set the AutoGenerateColumns to false. But here, you dynamically add the columns to the grid, so you need to make sure that the dynamic columns are added again when the page posts back, and more importantly, the adding of the columns should happen prior to the loading of the ViewState data of the grid. For example, here you can try to override the LoadViewState method of the page instance:protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);this.AddColumns();
}Another way is to add the columns in the OnInit event. For more information, you can see Control Execution Lifecycle (.NET Framework Developer's Guide)[^]
-
Yes,I have placed the columns for the datagrid at design time. I think your analysis is right,thank you! I will try your ways later,Thank you again.
Hi Richard, Because you dynamically add columns at runtime, you need to make sure that the dynamic columns are also added when the page posts back.
private void Page_Load(object sender, System.EventArgs e) { //The same as your method. ... if (Page.IsPostBack) { BindData(""); } } private void BindData(string sSortBy) { //Bind data based on the new sort expression or the current one. if(sSortBy=="") sSortBy = CurrentSortExpression; else CurrentSortExpression = sSortBy; SqlConnection conn = new SqlConnection("..."); conn.Open(); //The same as your method. ... conn.Close(); } //Keep the current Sort Expression. private string CurrentSortExpression { get{return ViewState["CurrentSortExpression"] as string;} set{ViewState["CurrentSortExpression"] = value;} } //Other methods keep the same. ....
In my next post, the sample code is improved a bit to demonstrate the things: + The data source is saved in Session so that the page does not need to execute the SQL command again when it posts back. + Use the DataView.Sort property to do the sorting instead of executing the SQL command with the Order By. -
Yes,I have placed the columns for the datagrid at design time. I think your analysis is right,thank you! I will try your ways later,Thank you again.
private void Page_Load(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.Columns.Clear(); DataGrid1.SelectedItemStyle.Wrap = false; DataGrid1.EditItemStyle.Wrap = false; DataGrid1.AlternatingItemStyle.Wrap = false; DataGrid1.HeaderStyle.Wrap = false; DataGrid1.HeaderStyle.BackColor = Color.Chocolate; DataGrid1.HeaderStyle.Font.Bold = true; DataGrid1.AllowPaging = true; DataGrid1.AllowSorting = true; DataGrid1.PagerStyle.Mode = PagerMode.NumericPages; DataGrid1.EnableViewState = true; if (Page.IsPostBack) { BindData(""); } } private DataView CreateDataSource() { if(Session["DataSource"]==null) { SqlConnection conn = new SqlConnection("server =127.0.0.1;User ID =sa ; Password=;Database =Northwind;"); conn.Open(); string strSQL = "select CustomerID,CompanyName,ContactName,ContactTitle,Address,City" + ",Region,PostalCode,Country,Phone,Fax" + " from customers"; //+ " Order By " + sSortBy; SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataView dv = new DataView(ds.Tables["customers"]); Session["DataSource"] = dv; conn.Close(); } return Session["DataSource"] as DataView; } private void BindData(string sSortBy) { DataView dv = CreateDataSource(); if(sSortBy!="") dv.Sort = sSortBy; BoundColumn dgc = null; string sFieldName = ""; //DataTable dt = ds.Tables[0]; DataTable dt = dv.Table; foreach (DataColumn dc in dt.Columns) { dgc = new BoundColumn(); sFieldName = dc.ColumnName; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.SortExpression = sFieldName; dgc.ItemStyle.Wrap = false; dgc.HeaderStyle.Wrap = false; dgc.FooterStyle.Wrap = false; DataGrid1.Columns.Add(dgc); } DataGrid1.DataSource = dv; DataGrid1.DataBind(); }
+ Because, there are many columns added to the datagrid, you can make a scrollable datagrid by putting it inside a div tag:<div style="OVERFLOW: scroll; WIDTH: 100%;">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</div>Also, because you delete all columns at runtime, so you don't need to add any column at design time.
-
private void Page_Load(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.Columns.Clear(); DataGrid1.SelectedItemStyle.Wrap = false; DataGrid1.EditItemStyle.Wrap = false; DataGrid1.AlternatingItemStyle.Wrap = false; DataGrid1.HeaderStyle.Wrap = false; DataGrid1.HeaderStyle.BackColor = Color.Chocolate; DataGrid1.HeaderStyle.Font.Bold = true; DataGrid1.AllowPaging = true; DataGrid1.AllowSorting = true; DataGrid1.PagerStyle.Mode = PagerMode.NumericPages; DataGrid1.EnableViewState = true; if (Page.IsPostBack) { BindData(""); } } private DataView CreateDataSource() { if(Session["DataSource"]==null) { SqlConnection conn = new SqlConnection("server =127.0.0.1;User ID =sa ; Password=;Database =Northwind;"); conn.Open(); string strSQL = "select CustomerID,CompanyName,ContactName,ContactTitle,Address,City" + ",Region,PostalCode,Country,Phone,Fax" + " from customers"; //+ " Order By " + sSortBy; SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataView dv = new DataView(ds.Tables["customers"]); Session["DataSource"] = dv; conn.Close(); } return Session["DataSource"] as DataView; } private void BindData(string sSortBy) { DataView dv = CreateDataSource(); if(sSortBy!="") dv.Sort = sSortBy; BoundColumn dgc = null; string sFieldName = ""; //DataTable dt = ds.Tables[0]; DataTable dt = dv.Table; foreach (DataColumn dc in dt.Columns) { dgc = new BoundColumn(); sFieldName = dc.ColumnName; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.SortExpression = sFieldName; dgc.ItemStyle.Wrap = false; dgc.HeaderStyle.Wrap = false; dgc.FooterStyle.Wrap = false; DataGrid1.Columns.Add(dgc); } DataGrid1.DataSource = dv; DataGrid1.DataBind(); }
+ Because, there are many columns added to the datagrid, you can make a scrollable datagrid by putting it inside a div tag:<div style="OVERFLOW: scroll; WIDTH: 100%;">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</div>Also, because you delete all columns at runtime, so you don't need to add any column at design time.
-
Richard, The sample code (both of them) works just fine on my machine before I posted it so I got a bit suprise when you say the two events do not work. Are you sure that you put the BindData method in the IsPostBack block? Also, can you make sure that the event handlers are still wired up to the events? If it's still not working, can you post it here then I take a look at it?
-
Richard, The sample code (both of them) works just fine on my machine before I posted it so I got a bit suprise when you say the two events do not work. Are you sure that you put the BindData method in the IsPostBack block? Also, can you make sure that the event handlers are still wired up to the events? If it's still not working, can you post it here then I take a look at it?
I am sorry for I misundstand you help.realy It work very well. Thank you again for your help me go out from this big trouble. Thank you. This is I test your code test it work very well.
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace DataGridApp { /// /// WrapTest2 的摘要说明。 /// public class WrapTest2 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.Columns.Clear(); DataGrid1.SelectedItemStyle.Wrap = false; DataGrid1.EditItemStyle.Wrap = false; DataGrid1.AlternatingItemStyle.Wrap = false; DataGrid1.HeaderStyle.Wrap = false; DataGrid1.HeaderStyle.BackColor = Color.Chocolate; DataGrid1.HeaderStyle.Font.Bold = true; DataGrid1.AllowPaging = true; DataGrid1.AllowSorting = true; DataGrid1.PagerStyle.Mode = PagerMode.NumericPages; DataGrid1.EnableViewState = true; BindData(""); if (!Page.IsPostBack) { } } private DataView CreateDataSource() { if(Session["DataSource"]==null) { SqlConnection conn = new SqlConnection("server =127.0.0.1;User ID =sa ; Password =;Database =Northwind;"); conn.Open(); string strSQL = "select CustomerID,CompanyName,ContactName,ContactTitle,Address,City" + ",Region,PostalCode,Country,Phone,Fax" + " from customers"; SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataView dv = new DataView(ds.Tables["customers"]); Session["DataSource"] = dv; conn.Close(); } return Session["DataSource"] as DataView; } private void BindData(string sSortBy) { DataView dv = CreateDataSource(); if(sSortBy!="") dv.Sort = sSortBy; BoundColumn dgc = null; string sFieldName = ""; DataTable dt = dv.Table; foreach (DataColumn dc in dt.Columns) { dgc = new BoundColumn(); sFieldName = dc.ColumnName; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.SortExpression = sFieldName; dgc.ItemStyle.Wrap = false; dgc.HeaderStyle.Wrap = false; dgc.Footer
-
I am sorry for I misundstand you help.realy It work very well. Thank you again for your help me go out from this big trouble. Thank you. This is I test your code test it work very well.
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace DataGridApp { /// /// WrapTest2 的摘要说明。 /// public class WrapTest2 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.Columns.Clear(); DataGrid1.SelectedItemStyle.Wrap = false; DataGrid1.EditItemStyle.Wrap = false; DataGrid1.AlternatingItemStyle.Wrap = false; DataGrid1.HeaderStyle.Wrap = false; DataGrid1.HeaderStyle.BackColor = Color.Chocolate; DataGrid1.HeaderStyle.Font.Bold = true; DataGrid1.AllowPaging = true; DataGrid1.AllowSorting = true; DataGrid1.PagerStyle.Mode = PagerMode.NumericPages; DataGrid1.EnableViewState = true; BindData(""); if (!Page.IsPostBack) { } } private DataView CreateDataSource() { if(Session["DataSource"]==null) { SqlConnection conn = new SqlConnection("server =127.0.0.1;User ID =sa ; Password =;Database =Northwind;"); conn.Open(); string strSQL = "select CustomerID,CompanyName,ContactName,ContactTitle,Address,City" + ",Region,PostalCode,Country,Phone,Fax" + " from customers"; SqlDataAdapter da = new SqlDataAdapter(strSQL, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataView dv = new DataView(ds.Tables["customers"]); Session["DataSource"] = dv; conn.Close(); } return Session["DataSource"] as DataView; } private void BindData(string sSortBy) { DataView dv = CreateDataSource(); if(sSortBy!="") dv.Sort = sSortBy; BoundColumn dgc = null; string sFieldName = ""; DataTable dt = dv.Table; foreach (DataColumn dc in dt.Columns) { dgc = new BoundColumn(); sFieldName = dc.ColumnName; dgc.DataField = sFieldName; dgc.HeaderText = sFieldName; dgc.SortExpression = sFieldName; dgc.ItemStyle.Wrap = false; dgc.HeaderStyle.Wrap = false; dgc.Footer
-
Did you just move the BindData method out of the !Page.IsPostBack block, then it works? I'm just curious about the chinese text in your code while your name is clearly an English name. ;P . Are you using VS which supports Chinese?