E
EatHam
OK - I'm a bit new with .NET, so please bear with me.
My problem is that I cannot get the sort event to fire. If anyone can shed
some light on this, I would greatly appreciate it.
Using c#, datagrid in a .ascx control, using a placeholder to put it in the
parent page.
Relevant code:
<asp:datagrid Visible="False"
id="grdBook" runat="server" AutoGenerateColumns="False"
AlternatingItemStyle-BackColor="#DFDFDF" GridLines="Vertical"
AllowSorting="true" AllowPaging="true" AllowCustomPaging="False"
PageSize="30" OnSortCommand="grdBook_SortCommand">
<Columns>
<asp:BoundColumn DataField="Symbol" HeaderText="Symbol"
SortExpression="Symbol" />
</Columns>
</asp:datagrid>
Code behind:
private void Page_Load(object sender, System.EventArgs e)
{
//Nothing relevant here - I don't want it to bind data and display the grid
initially
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
grdBook.SortCommand += new
DataGridSortCommandEventHandler(grdBook_SortCommand);
}
public void grdBook_SortCommand(object sender, DataGridSortCommandEventArgs
e)
{
BindData(e.SortExpression.ToString());
}
private void btnSearch_Click(object source, System.EventArgs e)
{
BindData("EntryTime");
}
private void BindData(string strSortString)
{
//Build the query for the given order type
SqlConnection conBook = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["bookVi
ew"]);
conBook.Open();
string strBookSQL = "This works fine on btnSearch_Click..." +
strSortString;
SqlCommand cmdGetBook = new SqlCommand(strBookSQL, conBook);
SqlDataAdapter daBook = new SqlDataAdapter();
daBook.SelectCommand = cmdGetBook;
DataSet dsBook = new DataSet();
daBook.Fill(dsBook);
grdBook.DataSource = dsBook.Tables[0];
grdBook.DataBind();
lblRecordCount.Text = dsBook.Tables[0].Rows.Count.ToString() + " records
found.";
lblPageCount.Text = "Page " + (grdBook.CurrentPageIndex + 1) + " of " +
grdBook.PageCount + "<br>";
foreach (System.Web.UI.Control abc in Page.Controls)
{
if (abc.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")
{
foreach (System.Web.UI.Control def in abc.Controls)
{
if (def.ID == "phdBookResults")
{
def.Controls.Add(grdBook);
def.Controls.Add(lblPageCount);
def.Controls.Add(lblRecordCount);
lblPageCount.Visible = true;
lblRecordCount.Visible = true;
grdBook.Visible = true;
}
}
}
}
}
My problem is that I cannot get the sort event to fire. If anyone can shed
some light on this, I would greatly appreciate it.
Using c#, datagrid in a .ascx control, using a placeholder to put it in the
parent page.
Relevant code:
<asp:datagrid Visible="False"
id="grdBook" runat="server" AutoGenerateColumns="False"
AlternatingItemStyle-BackColor="#DFDFDF" GridLines="Vertical"
AllowSorting="true" AllowPaging="true" AllowCustomPaging="False"
PageSize="30" OnSortCommand="grdBook_SortCommand">
<Columns>
<asp:BoundColumn DataField="Symbol" HeaderText="Symbol"
SortExpression="Symbol" />
</Columns>
</asp:datagrid>
Code behind:
private void Page_Load(object sender, System.EventArgs e)
{
//Nothing relevant here - I don't want it to bind data and display the grid
initially
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
grdBook.SortCommand += new
DataGridSortCommandEventHandler(grdBook_SortCommand);
}
public void grdBook_SortCommand(object sender, DataGridSortCommandEventArgs
e)
{
BindData(e.SortExpression.ToString());
}
private void btnSearch_Click(object source, System.EventArgs e)
{
BindData("EntryTime");
}
private void BindData(string strSortString)
{
//Build the query for the given order type
SqlConnection conBook = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["bookVi
ew"]);
conBook.Open();
string strBookSQL = "This works fine on btnSearch_Click..." +
strSortString;
SqlCommand cmdGetBook = new SqlCommand(strBookSQL, conBook);
SqlDataAdapter daBook = new SqlDataAdapter();
daBook.SelectCommand = cmdGetBook;
DataSet dsBook = new DataSet();
daBook.Fill(dsBook);
grdBook.DataSource = dsBook.Tables[0];
grdBook.DataBind();
lblRecordCount.Text = dsBook.Tables[0].Rows.Count.ToString() + " records
found.";
lblPageCount.Text = "Page " + (grdBook.CurrentPageIndex + 1) + " of " +
grdBook.PageCount + "<br>";
foreach (System.Web.UI.Control abc in Page.Controls)
{
if (abc.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")
{
foreach (System.Web.UI.Control def in abc.Controls)
{
if (def.ID == "phdBookResults")
{
def.Controls.Add(grdBook);
def.Controls.Add(lblPageCount);
def.Controls.Add(lblRecordCount);
lblPageCount.Visible = true;
lblRecordCount.Visible = true;
grdBook.Visible = true;
}
}
}
}
}