Michael71
MIS
- Oct 17, 2006
- 18
I have a problem creating a Web part with a grid control...this is what I have so far.
using System;
using System.Data;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
namespace Infonizer.WebParts
{
/// <summary>
/// Description for Sales Person Activities.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:SalesStageSetup runat=server></{0}:SalesStageSetup>"),
XmlRoot(Namespace = "Infonizer.WebParts")]
public class SalesStageSetup : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string CLASS_NAME = "SalesPersonActivities";
private StringBuilder _errors = null;
private DataGrid _grdRecords;
protected override void CreateChildControls()
{
dgSalesStage = new DataGrid();
dgSalesStage.EnableViewState = true;
dgSalesStage.AutoGenerateColumns = false;
dgSalesStage.AllowSorting = true;
dgSalesStage.HeaderStyle.BackColor = System.Drawing.Color.SlateGray;
dgSalesStage.HeaderStyle.Font.Bold = true;
dgSalesStage.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dgSalesStage.AlternatingItemStyle.BackColor = System.Drawing.Color.Gainsboro;
BoundColumn col = new BoundColumn();
col.HeaderText = "Picture Color";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofileictureColor";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Stock N VIN(R)";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:VIN";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Year";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:Year";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:Year";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Picture Color";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofileictureColor";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Series";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:Series";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:Series";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Model Description";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:ModelDesc";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:ModelDesc";
dgSalesStage.Columns.Add(col);
//add additional columns here based on code above
dgSalesStage.Width = System.Web.UI.WebControls.Unit.Percentage(100);
DataSet ds = this.Query(this.TextBoxQuery.Text);
DataView dv = ds.Tables[0].DefaultView;
dgSalesStage.DataSource = dv;
dgSalesStage.DataBind();
dgSalesStage.SortCommand += new DataGridSortCommandEventHandler(dgSalesStage_SortCommand);
Controls.Add(dgSalesStage);
}
//Render the control
protected override void RenderWebPart(HtmlTextWriter output)
{
this.EnsureChildControls();
this.dgSalesStage.RenderControl(output);
}
//Add the sort method
private void dgSalesStage_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string sortExpression = (string)ViewState["SortExp"];
string sortDirection = (string)ViewState["SortDir"];
if (sortExpression != e.SortExpression)
{
sortExpression = e.SortExpression;
sortDirection = "asc";
}
else
{
if (sortDirection == "asc")
sortDirection = "desc";
else
sortDirection = "asc";
}
ViewState["SortExp"] = sortExpression;
ViewState["SortDir"] = sortDirection;
}
//Bind the DataGrid on PreRender
protected override void OnPreRender(EventArgs e)
{
DataSet ds = this.Query(this.TextBoxQuery.Text);
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null && ViewState["SortDir"] != null)
{
dv.Sort = ViewState["SortExp"] + " " + ViewState["SortDir"];
}
this.dgSalesStage.DataSource = dv;
this.dgSalesStage.DataBind();
base.OnPreRender(e);
}
}
}
using System;
using System.Data;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
namespace Infonizer.WebParts
{
/// <summary>
/// Description for Sales Person Activities.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:SalesStageSetup runat=server></{0}:SalesStageSetup>"),
XmlRoot(Namespace = "Infonizer.WebParts")]
public class SalesStageSetup : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string CLASS_NAME = "SalesPersonActivities";
private StringBuilder _errors = null;
private DataGrid _grdRecords;
protected override void CreateChildControls()
{
dgSalesStage = new DataGrid();
dgSalesStage.EnableViewState = true;
dgSalesStage.AutoGenerateColumns = false;
dgSalesStage.AllowSorting = true;
dgSalesStage.HeaderStyle.BackColor = System.Drawing.Color.SlateGray;
dgSalesStage.HeaderStyle.Font.Bold = true;
dgSalesStage.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dgSalesStage.AlternatingItemStyle.BackColor = System.Drawing.Color.Gainsboro;
BoundColumn col = new BoundColumn();
col.HeaderText = "Picture Color";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofileictureColor";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Stock N VIN(R)";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:VIN";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Year";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:Year";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:Year";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Picture Color";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofileictureColor";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofileictureColor";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Series";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:Series";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:Series";
dgSalesStage.Columns.Add(col);
BoundColumn col = new BoundColumn();
col.HeaderText = "Model Description";
col.DataField = "urn:schemas-microsoftcom:sharepointortalrofile:ModelDesc";
col.SortExpression = "urn:schemas-microsoft-com:sharepointortalrofile:ModelDesc";
dgSalesStage.Columns.Add(col);
//add additional columns here based on code above
dgSalesStage.Width = System.Web.UI.WebControls.Unit.Percentage(100);
DataSet ds = this.Query(this.TextBoxQuery.Text);
DataView dv = ds.Tables[0].DefaultView;
dgSalesStage.DataSource = dv;
dgSalesStage.DataBind();
dgSalesStage.SortCommand += new DataGridSortCommandEventHandler(dgSalesStage_SortCommand);
Controls.Add(dgSalesStage);
}
//Render the control
protected override void RenderWebPart(HtmlTextWriter output)
{
this.EnsureChildControls();
this.dgSalesStage.RenderControl(output);
}
//Add the sort method
private void dgSalesStage_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string sortExpression = (string)ViewState["SortExp"];
string sortDirection = (string)ViewState["SortDir"];
if (sortExpression != e.SortExpression)
{
sortExpression = e.SortExpression;
sortDirection = "asc";
}
else
{
if (sortDirection == "asc")
sortDirection = "desc";
else
sortDirection = "asc";
}
ViewState["SortExp"] = sortExpression;
ViewState["SortDir"] = sortDirection;
}
//Bind the DataGrid on PreRender
protected override void OnPreRender(EventArgs e)
{
DataSet ds = this.Query(this.TextBoxQuery.Text);
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null && ViewState["SortDir"] != null)
{
dv.Sort = ViewState["SortExp"] + " " + ViewState["SortDir"];
}
this.dgSalesStage.DataSource = dv;
this.dgSalesStage.DataBind();
base.OnPreRender(e);
}
}
}