Thursday, 8 March 2012

Outer Join with Linq SharePoint 2010

With help from http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/10/15/my-500th-post-left-outer-joins-with-linq.aspx, this is my outer join linq



var outerX = from cust in dataContext.Customers.ToList()
join ord in dataContext.Orders.ToList()
on cust.CustomerID.ToString().Trim()
equals ord.CustomerID.CustomerID.ToString().Trim()
into CustomerInformationGroup
from item in CustomerInformationGroup.DefaultIfEmpty(
new OrdersItem { Title="-",OrderID=0,OrderDate=DateTime.Now,Quantity=0,ProductID=new ProductsItem(),CustomerID=new CustomersItem()})
select new
{
CustomerID = cust.CustomerID,
Title = cust.Title,
City = cust.City,
CustomerCountry = cust.CustomerCountry,
JoiningDate = cust.JoiningDate,
OrderTitle = item.Title
};

dataGrdCustomer.DataSource = outerX.ToList();
dataGrdCustomer.Columns.Clear();

DataGridViewTextBoxColumn custName = new DataGridViewTextBoxColumn();
custName.DataPropertyName = "Title";
custName.Name = "Title";
custName.HeaderText = "Name";

dataGrdCustomer.Columns.Add(custName);

DataGridViewTextBoxColumn custID = new DataGridViewTextBoxColumn();
custID.DataPropertyName = "CustomerID";
custID.Name = "CustomerID";
custID.HeaderText = "CustomerID";
dataGrdCustomer.Columns.Add(custID);

DataGridViewTextBoxColumn custCity = new DataGridViewTextBoxColumn();
custCity.DataPropertyName = "City";
custCity.Name = "City";
custCity.HeaderText = "City";
dataGrdCustomer.Columns.Add(custCity);

DataGridViewTextBoxColumn custCountry = new DataGridViewTextBoxColumn();
custCountry.DataPropertyName = "CustomerCountry";
custCountry.Name = "CustomerCountry";
custCountry.HeaderText = "Country";
dataGrdCustomer.Columns.Add(custCountry);

DataGridViewTextBoxColumn prodTitle = new DataGridViewTextBoxColumn();
prodTitle.DataPropertyName = "OrderTitle";
prodTitle.Name = "OrderTitle";
prodTitle.HeaderText = "OrderTitle";
dataGrdCustomer.Columns.Add(prodTitle);


Here's the output table,

Inner join with LINQ in SharePoint 2010



var x = from cust in dataContext.Customers.ToList()
join ord in dataContext.Orders.ToList()
on cust.CustomerID.ToString().Trim() equals
ord.CustomerID.CustomerID.ToString().Trim()
select new
{
CustomerID = cust.CustomerID,
Title = cust.Title,
City = cust.City,
CustomerCountry = cust.CustomerCountry,
JoiningDate = cust.JoiningDate,
OrderTitle = ord.Title
};
dataGrdCustomer.DataSource = x.ToList();

Tuesday, 6 March 2012

Caml Query Client Object

I was wondering why the list query returns ALL of my list when the reason was that I have left out the <Query> tag! This is the working version...




SP.List oList = context.Web.Lists.GetByTitle("Projects");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "" + projectCode + "";
ListItemCollection collListItem = oList.GetItems(camlQuery);
context.Load(collListItem);
context.ExecuteQuery();

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,