Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web Part with a grid control...help

Status
Not open for further replies.

Michael71

MIS
Oct 17, 2006
18
0
0
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:sharepoint:portal:profile:pictureColor";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile:pictureColor";

dgSalesStage.Columns.Add(col);

BoundColumn col = new BoundColumn();

col.HeaderText = "Stock N VIN(R)";

col.DataField = "urn:schemas-microsoftcom:sharepoint:portal:profile:VIN";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile:pictureColor";

dgSalesStage.Columns.Add(col);


BoundColumn col = new BoundColumn();

col.HeaderText = "Year";

col.DataField = "urn:schemas-microsoftcom:sharepoint:portal:profile:Year";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile:Year";

dgSalesStage.Columns.Add(col);

BoundColumn col = new BoundColumn();

col.HeaderText = "Picture Color";

col.DataField = "urn:schemas-microsoftcom:sharepoint:portal:profile:pictureColor";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile:pictureColor";

dgSalesStage.Columns.Add(col);


BoundColumn col = new BoundColumn();

col.HeaderText = "Series";

col.DataField = "urn:schemas-microsoftcom:sharepoint:portal:profile:Series";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile:Series";

dgSalesStage.Columns.Add(col);


BoundColumn col = new BoundColumn();

col.HeaderText = "Model Description";

col.DataField = "urn:schemas-microsoftcom:sharepoint:portal:profile:ModelDesc";

col.SortExpression = "urn:schemas-microsoft-com:sharepoint:portal:profile: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);

}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top