Monday, 5 March 2012

Customizing SPGridView style (with hover alt rows)

ascx


<style type="text/css">

.GridViewHeaderStyle {
background-color:#c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}

.GridViewAltRowStyle
{
background-color: #F0F8FF;
font-weight: bold;
color: black;
}

.GridRowStyle
{
background-color: #E6E6FA;
font-weight: bold;
color: black;
}

.GridViewAltRowStyle:hover, .GridRowStyle:hover
{
background-color: yellow;
color: black;
font-weight: bold;
}

</style>


<SharePoint:SPGridView ID="myGridView"
runat="server" AutoGenerateColumns="false" >
</SharePoint:SPGridView>




ascx.cs


public partial class SPGridViewTestUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string[] fields = { "ID","Name","Country","Total"};
foreach (string s in fields)
{
BoundField bf = new BoundField();
bf.DataField = s;
bf.HeaderText = s;
myGridView.Columns.Add(bf);
}


myGridView.GroupField = "Name";
myGridView.AllowGrouping = true;
myGridView.AllowGroupCollapse = true;
myGridView.AlternatingRowStyle.CssClass = "GridViewAltRowStyle";
myGridView.RowStyle.CssClass = "GridRowStyle";

myGridView.DataSource = SelectData();
myGridView.DataBind();

// if this is set before databind, it won't include the column where the grouping is (top left)
foreach (DataControlField dcf in myGridView.Columns)
{
dcf.HeaderStyle.CssClass = "GridViewHeaderStyle";
}
}


public DataTable SelectData()
{
DataTable dataSource = new DataTable();

dataSource.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
dataSource.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
dataSource.Columns.Add(new DataColumn("Country", Type.GetType("System.String")));
dataSource.Columns.Add(new DataColumn("Total", Type.GetType("System.Int32")));

dataSource.Rows.Add(1, "Jack", "USA", 10000);
dataSource.Rows.Add(2, "Jack", "Thailand", 15000);
dataSource.Rows.Add(3, "Jack", "Australia", 5000);
dataSource.Rows.Add(4, "Sam", "USA", 7000);
dataSource.Rows.Add(5, "Sam", "Thailand", 30000);
dataSource.Rows.Add(6, "Sam", "Australia", 8700);
dataSource.Rows.Add(7, "Andy", "USA", 3000);
dataSource.Rows.Add(8, "Andy", "Thailand", 50000);
dataSource.Rows.Add(9, "Andy", "Australia", 25000);

return dataSource;
}
}




How it looks,

Wednesday, 29 February 2012

Replace to HTML










Monday, 27 February 2012

Load a List Table dynamically with columns

This took me a while to figure out, for predefined fields like ID, Modified, Created, Title, it will still have a Hidden=True even though it is not shown in AllItems.aspx. Thus I have to check it by using the Exists method.


public void loadHeaderActionTable()
{
SPWeb web = SPContext.Current.Web;
SPList actionList = web.Lists[ActionListName];
SPListItemCollection actions = actionList.GetItems(myActionListView);

TableRow header = new TableRow();
foreach (SPField fItem in actions.Fields)
{

if (!fItem.Hidden
&& myActionListView.ViewFields.Exists(fItem.InternalName))
{

TableCell cItem = new TableCell();
cItem.Text = fItem.Title.ToString();
cItem.CssClass = "ms-vh-div";
header.Cells.Add(cItem);
myActionColumnTitle.Add(cItem.Text);

}
}
tblACList.Rows.Add(header);

}

Friday, 24 February 2012

Working List Defination

Elements.xml


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field ID="{9814D5C4-1A49-42A7-8229-06288E2E4C3F}" Name="ActionID" Type="Text" DisplayName="ActionID" Required="TRUE"></Field>
<Field ID="{271C7377-6800-4CD8-8120-D794BA90B023}" Name="Title" Type="Text" DisplayName="Title" Required="TRUE"></Field>
<Field ID="{FAE223E3-D53F-4886-9021-286C96728696}" Name="AssignedTo" Type="UserMulti" DisplayName="Resp" Required="False"></Field>
<Field ID="{A31215AF-BCD1-401D-899F-F6B2A7F77AAD}" Name="InitiatedDate" Type="DateTime" Format="DateOnly" DisplayName="InitiatedDate" Required="TRUE">
<Default>[today]</Default>
</Field>
<Field ID="{CFEE2340-8D7F-4BE9-8FE7-15DA5A255B50}" Name="TargetDate" Type="DateTime" Format="DateOnly" DisplayName="TargetDate" Required="FALSE">
<Default></Default>
</Field>
<Field ID="{E40F6006-2A64-43B9-B11F-BADD96C1ECCE}" Name="ClosedDate" Type="DateTime" Format="DateOnly" DisplayName="ClosedDate" Required="FALSE">
<Default></Default>
</Field>
<Field ID="{50B78056-F0B3-4B5B-B71F-DABD97E8D49F}" Name="Comments" Type="Note" DisplayName="Comments" Required="FALSE" NumLines="5"></Field>
<Field ID="{21D31AC1-2959-4794-84F5-C5C2CC48D06D}" Name="SubjectID" Type="Lookup" DisplayName="SubjectID" List="Lists/SubjectList" ShowField="SubjectID" Required="TRUE"></Field>

<ContentType
ID="0x0100A5D7A562B9B440FEBA25E181C4A3A098"
Name="ActionList"
Group="Action Content Type"
Description="Action List"
Version="0">
<FieldRefs>
<FieldRef ID="{9814D5C4-1A49-42A7-8229-06288E2E4C3F}" />
<FieldRef ID="{271C7377-6800-4CD8-8120-D794BA90B023}" />
<FieldRef ID="{FAE223E3-D53F-4886-9021-286C96728696}" />
<FieldRef ID="{A31215AF-BCD1-401D-899F-F6B2A7F77AAD}" />
<FieldRef ID="{CFEE2340-8D7F-4BE9-8FE7-15DA5A255B50}" />
<FieldRef ID="{E40F6006-2A64-43B9-B11F-BADD96C1ECCE}" />
<FieldRef ID="{50B78056-F0B3-4B5B-B71F-DABD97E8D49F}" />
<FieldRef ID="{21D31AC1-2959-4794-84F5-C5C2CC48D06D}" />
</FieldRefs>
</ContentType>

<!-- Do not change the value of the Name attribute below.
If it does not match the folder name of the List Definition
project item, an error will occur when the project is run. -->
<ListTemplate
Name="ListDefinition1"
Type="10001"
AllowDeletion="TRUE"
DisallowContentTypes="FALSE"
BaseType="0"
OnQuickLaunch="FALSE"
SecurityBits="11"
Sequence="410"
DisplayName="ActionList"
Description="My Action List Definition"
Image="/_layouts/images/itgen.gif"/>
</Elements>


Schema.xml



<?xml version="1.0" encoding="utf-8"?>
<List EnableContentTypes="TRUE" xmlns:ows="Microsoft SharePoint" Title="ActionList" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ActionList-ListDefinition1" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
<MetaData>
<ContentTypes>
<ContentTypeRef ID="0x0100A5D7A562B9B440FEBA25E181C4A3A098" />
<ContentTypeRef ID="0x01">
<Folder TargetName="Item" />
</ContentTypeRef>
<ContentTypeRef ID="0x0120" />
</ContentTypes>

<Fields>
<Field ID="{9814D5C4-1A49-42A7-8229-06288E2E4C3F}" Name="ActionID" Type="Text" DisplayName="ActionID" Required="TRUE" ></Field>
<Field ID="{271C7377-6800-4CD8-8120-D794BA90B023}" Name="Title" Type="Text" DisplayName="Title" Required="TRUE"></Field>
<Field ID="{FAE223E3-D53F-4886-9021-286C96728696}" Name="AssignedTo" Type="UserMulti" DisplayName="Resp" Required="False"></Field>
<Field ID="{A31215AF-BCD1-401D-899F-F6B2A7F77AAD}" Name="InitiatedDate" Type="DateTime" Format="DateOnly" DisplayName="Initiated Date" Required="TRUE">
<Default>[today]</Default>
</Field>
<Field ID="{CFEE2340-8D7F-4BE9-8FE7-15DA5A255B50}" Name="TargetDate" Type="DateTime" Format="DateOnly" DisplayName="TargetDate" Required="FALSE">
<Default></Default>
</Field>
<Field ID="{E40F6006-2A64-43B9-B11F-BADD96C1ECCE}" Name="ClosedDate" Type="DateTime" Format="DateOnly" DisplayName="ClosedDate" Required="FALSE">
<Default></Default>
</Field>
<Field ID="{50B78056-F0B3-4B5B-B71F-DABD97E8D49F}" Name="Comments" Type="Note" DisplayName="Comments" Required="FALSE" NumLines="5"></Field>
<Field ID="{21D31AC1-2959-4794-84F5-C5C2CC48D06D}" Name="SubjectID" Type="Lookup" DisplayName="SubjectID" List="Lists/SubjectList" ShowField="SubjectID" Required="TRUE"></Field>

</Fields>
<Views>
<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="LinkTitleNoMenu"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
</ParameterBindings>
</View>
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="ActionID"></FieldRef>
<FieldRef Name="LinkTitle"></FieldRef>
<FieldRef Name="AssignedTo"></FieldRef>
<FieldRef Name="InitiatedDate"></FieldRef>
<FieldRef Name="TargetDate"></FieldRef>
<FieldRef Name="ClosedDate"></FieldRef>
<FieldRef Name="Comments"></FieldRef>
<FieldRef Name="SubjectID"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="ID"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
</ParameterBindings>
</View>
</Views>
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
</Forms>
</MetaData>
</List>


ListInstance's Element.xml


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance Title="ActionList"
OnQuickLaunch="TRUE"
TemplateType="10001"
Url="Lists/ActionList"
Description="My List Instance">
<Data>
<Rows>
</Rows>
</Data>
</ListInstance>
</Elements>