This has three fields ID,Manufacturer, Name
Following grid view get populated using the following code.
.ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RenderingData.ascx.cs" Inherits="RenderingData.VisualWebPart1.VisualWebPart1UserControl" %>
code behind
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
namespace RenderingData.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
populateSPGrid();
}
}
protected void populateSPGrid()
{
using (SPSite oSPSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{
SPList oList = oSPWeb.Lists["VSProducts"];
SPListItemCollection oListCollection = oList.Items;
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("Manufacturer", typeof(string));
table.Columns.Add("Name", typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem oSPListItem in oListCollection)
{
row = table.Rows.Add();
row["ID"] = oSPListItem["ID"].ToString();
row["Manufacturer"] = oSPListItem["Manufacturer"].ToString();
row["Name"] = oSPListItem["Name"].ToString();
}
// create the bound fields
SPBoundField boundField;
boundField = new SPBoundField();
boundField.HeaderText = "ID";
boundField.DataField = "ID";
boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
boundField.ItemStyle.Wrap = false;
SPGView.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "Manufacturer";
boundField.DataField = "Manufacturer";
SPGView.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = "Name";
boundField.DataField = "Name";
SPGView.AutoGenerateColumns = false;
SPGView.Columns.Add(boundField);
SPGView.DataSource = table.DefaultView;
SPGView.DataBind();
}
}
}
}
}
No comments:
Post a Comment