Monday, 30 June 2014

SharePoint 2010 GridView Visual WebPart

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace AjaxVWP.AjaxVisualWebPart
{
public partial class AjaxVisualWebPartUserControl : UserControl
{
List<PlayerStat> listOfPlayerStats = new List<PlayerStat>();
string mySiteURL = "http://SERVER:8888";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDataGridLoad_Click(object sender, EventArgs e)
{
statDataGrid.Width = Unit.Percentage(100);
statDataGrid.CellPadding = 1;
statDataGrid.HeaderStyle.Font.Bold = true;
statDataGrid.HeaderStyle.CssClass = "ms-vh1";
statDataGrid.GridLines = GridLines.Horizontal;
statDataGrid.BorderWidth = Unit.Pixel(3);
statDataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
using (SPSite mySiteCollection = new SPSite(mySiteURL))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList myList = web.Lists["Stats"];
foreach (SPListItem tempListItem in myList.Items)
{
PlayerStat tempStat = new PlayerStat();
tempStat.playerName = tempListItem["Title"].ToString();
tempStat.numOfGoals = tempListItem["Goals"].ToString();
tempStat.numOfAssists = tempListItem["Assists"].ToString();
tempStat.numOfPIM = tempListItem["PIM"].ToString();
tempStat.gamesPlayed = tempListItem["Games"].ToString();
tempStat.playerAVG = calcPlayerAverage(tempStat.gamesPlayed,
tempStat.numOfGoals, tempStat.numOfAssists);
listOfPlayerStats.Add(tempStat);
}
}
}
statDataGrid.DataSource = listOfPlayerStats;
statDataGrid.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
using (SPSite mySiteCollection = new SPSite(mySiteURL))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Stats"];
SPListItem newStat = list.Items.Add();
newStat["Title"] = txtbxName.Text;
newStat["Goals"] = txtbxGoals.Text;
newStat["Assists"] = txtbxAssists.Text;
newStat["PIM"] = txtbxPIM.Text;
newStat["Games"] = txtbxGoals.Text;
newStat.Update();
web.AllowUnsafeUpdates = false;
}
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
txtbxName.Text = "";
txtbxGames.Text = "";
txtbxGoals.Text = "";
txtbxAssists.Text = "";
txtbxPIM.Text = "";
}
private string calcPlayerAverage(string games, string goals, string assists)
{
int numGames = Int32.Parse(games);
int numGoals = Int32.Parse(goals);
int numAssists = Int32.Parse(assists);
double avgStat = 0.00;
avgStat = (numGoals * 2) + (numAssists * 1) / numGames;
return avgStat.ToString();
}
}
}

SharePoint 2010 Grid View