<SharePoint:SPGridView ID="grdPropertyValues" runat="server" AlternatingRowStyle-BackColor="#99CCFF" AutoGenerateColumns="false"
RowStyle-BackColor="#66CCFF" AllowFiltering="true" AllowPaging="false" AllowSorting="false" BorderColor="Black" >
</SharePoint:SPGridView>
SPSite site = new SPSite("http://abcdev");
using (SPWeb web = site.OpenWeb())
{
SPList clientlist = web.Lists.TryGetList("clientmatter");
if(clientlist == null)
{
return;
}
SPListItemCollection results = clientlist.Items;
SPListItemCollection spListItem;
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='clientid' /></OrderBy>";
DataTable dt = new DataTable();
dt = clientlist.GetItems(query).GetDataTable();
DataView dv = new DataView(dt);
DataTable tbl = dv.ToTable(true, "clientid");
BoundField colTitle = new BoundField();
this.grdPropertyValues.DataSource = tbl;
colTitle.DataField = "clientid";
colTitle.HeaderText = "clientid";
this.grdPropertyValues.Columns.Add(colTitle);
this.Controls.Add(grdPropertyValues);
this.grdPropertyValues.DataBind();
}
Although the above code works, it does not meet my requirements.SPSite site = new SPSite("http://abcdev");
using (SPWeb web = site.OpenWeb())
{
SPList clientlist = web.Lists.TryGetList("clientmatter");
if(clientlist == null)
{
return;
}
SPListItemCollection results = clientlist.Items;
SPListItemCollection spListItem;
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='clientid' /></OrderBy><Where><Eq><FieldRef Name='clientid' /><FieldRef Name='Client' /></Eq></Where>";
DataTable dt = new DataTable();
dt = clientlist.GetItems(query).GetDataTable();
DataView dv = new DataView(dt);
DataTable tbl = dv.ToTable(true, "clientid");
BoundField colTitle = new BoundField();
this.grdPropertyValues.DataSource = tbl;
colTitle.DataField = "clientid";
colTitle.HeaderText = "clientid";
this.grdPropertyValues.Columns.Add(colTitle);
HyperLinkField hlf = new HyperLinkField();
hlf.HeaderText = "Header Text";
hlf.DataNavigateUrlFields = new string[] { "Client" };
hlf.DataNavigateUrlFormatString = "http://google.com?q={0}";
hlf.DataTextField = "Client";
this.grdPropertyValues.Columns.Add(hlf);
// I tried creating a 'colLink' field but it failed too
//BoundField colLink = new BoundField();
//this.grdPropertyValues.DataSource = tbl;
//colLink.DataField = "Client";
//colLink.HeaderText = "Client";
//this.grdPropertyValues.Columns.Add(colLink);
this.Controls.Add(grdPropertyValues);
this.grdPropertyValues.DataBind();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using SimpleLogic;
namespace SimpleLogic.ControlTemplates.SimpleLogic
{
class SimpleSPGrid
{
public SimpleLogic _logic;
public ObjectDataSource _gridDS;
public SPGridView _grid;
public SimpleSPGrid(SimpleLogic logic)
{
_logic = logic;
}
protected sealed override void CreateChildControls()
{
const string GRIDID = "grid";
const string DATASOURCEID = "gridDS";
_gridDS = new ObjectDataSource();
_gridDS.ID = DATASOURCEID;
_gridDS.TypeName = typeof(SimpleLogic).AssemblyQualifiedName;
_gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
_gridDS.SelectMethod = "Select";
_gridDS.UpdateMethod = "Update";
_gridDS.Updating += new ObjectDataSourceMethodEventHandler(gridDS_Updating);
Panel1.Controls.Add(_gridDS);
_grid = new SPGridView();
_grid.ID = GRIDID;
_grid.DataSourceID = _gridDS.ID;
_grid.AutoGenerateColumns = false;
_grid.DataKeyNames = _logic.GetDataKeyNames();
CommandField command = new CommandField();
command.ShowEditButton = true;
_grid.Columns.Add(command);
foreach (BoundField column in _logic.GetColumns())
{
_grid.Columns.Add(column);
}
Panel1.Controls.Add(_grid);
}
void gridDS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
Dictionary<string, string> data = new Dictionary<string, string>();
foreach (DictionaryEntry entry in e.InputParameters)
{
string value = entry.Value == null ? null : entry.Value.ToString();
data.Add(entry.Key.ToString(), value);
}
e.InputParameters.Clear();
e.InputParameters.Add("data", data);
}
private void gridDS_ObjectCreating(object sender, ObjectDataSourceDisposingEventArgs e)
{
e.ObjectInstance = _logic;
}
}
}
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.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml.Serialization;
namespace SimpleLogic.ControlTemplates.SimpleLogic
{
[ToolboxData("<{0}:SimpleLogic runat=server></{0}:SimpleLogic>")]
[XmlRoot(Namespace = "SimpleLogic")]
public partial class SimpleLogic : UserControl
{
const string LIST_NAME = "clientmatter";
string[] LIST_COLUMNS = null;
string VIEW_FIELDS = string.Empty;
string[] DATA_KEY_NAMES = null;
private SPSite site = new SPSite("http://abcdev");
private SPWeb _currentweb;
private SPWeb sPWeb;
protected void Page_Load(object sender, EventArgs e)
{
SimpleLogic logic = new SimpleLogic();
SimpleSPGrid grid = new SimpleSPGrid(logic);
Panel1.Controls.Add(grid);
}
protected override void CreateChildControls()
{
try
{
// Loads a user control
SimpleLogic myUserControl = (SimpleLogic)Page.LoadControl("~/_controltemplates/SimpleLogic/SimpleLogic.ascx");
// Adds it to the controls collection of the Web Part
this.Controls.Add(myUserControl);
}
catch (Exception e)
{
this.Controls.Add(new Label()
{
Text = e.ToString()
});
}
}
public string[] GetDataKeyNames()
{
return DATA_KEY_NAMES;
}
public List<BoundField> GetColumns()
{
List<BoundField> fields = new List<BoundField>();
fields.Add(GetBoundField("ID"));
fields[0].ReadOnly = true;
fields.Add(GetBoundField("Title"));
fields.Add(GetBoundField("clientid"));
fields.Add(GetBoundField("Client"));
return fields;
}
private BoundField GetBoundField(string name)
{
BoundField bf = new BoundField();
bf.HeaderText = name;
bf.DataField = name;
return bf;
}
public DataTable Select()
{
SPQuery q = new SPQuery();
q.ViewFields = VIEW_FIELDS;
q.Query = "<Where><IsNotNull><FieldRef Name='clientid' /></IsNotNull></Where>";
SPList list = _currentweb.Lists[LIST_NAME];
SPListItemCollection items = list.GetItems(q);
return items.GetDataTable();
}
private string GetViewFields(string[] columns)
{
StringBuilder sb = new StringBuilder();
foreach (string field in columns)
{
sb.AppendFormat("<FieldRef Name='{0}' />", field);
}
return sb.ToString();
}
public void Update(Dictionary<string, string> data)
{
SPList list = _currentweb.Lists[LIST_NAME];
int id = Int32.Parse(data["ID"]);
SPListItem item = list.GetItemById(id);
item["Title"] = data["Title"];
item["clientid"] = data["clientid"];
item["Client"] = data["Client"];
item.Update();
}
}
}
<%@ 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="SimpleLogic.ascx.cs" Inherits="SimpleLogic.ControlTemplates.SimpleLogic.SimpleLogic" %>
<asp:Label ID="Label1" runat="server">SPGridView Test</asp:Label>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
protected sealed override void CreateChildControls()
[ToolboxData("<{0}:SimpleLogic runat=server></{0}:SimpleLogic>")]
[XmlRoot(Namespace = "SimpleLogic")]
public partial class SimpleLogic : UserControl
class SimpleSPGrid
{
[ToolboxData("<{0}:SimpleSPGrid runat=server></{0}:SimpleSPGrid>")]
public partial class SimpleSPGrid : UserControl
using System.Web.UI;
[ToolboxData("<{0}:SimpleSPGrid runat=server></{0}:SimpleSPGrid>")]
public partial class SimpleSPGrid : UserControl
[ToolboxData("<{0}:SimpleLogic runat=server></{0}:SimpleLogic>")]
[XmlRoot(Namespace = "SimpleLogic")]
public delegate void ObjectDataSourceObjectEventHandler(
Object sender,
ObjectDataSourceEventArgs e
)
public delegate void ObjectDataSourceObjectEventHandler(Object sender, ObjectDataSourceEventArgs e);
2. and added to the main constructorpublic event ObjectDataSourceObjectEventHandler evHandler;
namespace SimpleLogic.ControlTemplates.SimpleLogic
{
[ToolboxData("<{0}:SimpleSPGrid runat=server></{0}:SimpleSPGrid>")]
[XmlRoot(Namespace = "SimpleLogic")]
public partial class SimpleSPGrid : UserControl
{
public SimpleLogic _logic;
public ObjectDataSource _gridDS;
public SPGridView _grid;
public delegate void ObjectDataSourceObjectEventHandler(Object sender, ObjectDataSourceEventArgs e);
public event ObjectDataSourceObjectEventHandler evHandler;
public SimpleSPGrid(SimpleLogic logic)
{
_logic = logic;
}
protected sealed override void CreateChildControls(object sender, EventArgs e)
{
const string GRIDID = "grid";
const string DATASOURCEID = "gridDS";
_gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
_gridDS = new ObjectDataSource();
_gridDS.ID = DATASOURCEID;
_gridDS.TypeName = typeof(SimpleLogic).AssemblyQualifiedName;
_gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
_gridDS.SelectMethod = "Select";
_gridDS.UpdateMethod = "Update";
_gridDS.Updating += new ObjectDataSourceMethodEventHandler(gridDS_Updating);
Panel1.Controls.Add(_gridDS);
_grid = new SPGridView();
_grid.ID = GRIDID;
_grid.DataSourceID = _gridDS.ID;
_grid.AutoGenerateColumns = false;
_grid.DataKeyNames = _logic.GetDataKeyNames();
CommandField command = new CommandField();
command.ShowEditButton = true;
_grid.Columns.Add(command);
foreach (BoundField column in _logic.GetColumns())
{
_grid.Columns.Add(column);
}
Panel1.Controls.Add(_grid);
}
void gridDS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
Dictionary<string, string> data = new Dictionary<string, string>();
foreach (DictionaryEntry entry in e.InputParameters)
{
string value = entry.Value == null ? null : entry.Value.ToString();
data.Add(entry.Key.ToString(), value);
}
e.InputParameters.Clear();
e.InputParameters.Add("data", data);
}
private void gridDS_ObjectCreating(object sender, ObjectDataSourceDisposingEventArgs e)
{
e.ObjectInstance = _logic;
}
}
}
private void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = _logic;
}
Some of the errors have gone away, but the remaining 2 include:
So, as a user types in the textbox the SPGridView will automatically filter the grid and reduce the rows. I want to be able to choose a row to see the details of the passed in as a querystring.
Any ideas?