In Sharepoint When retrieve data using web service, there is need for customization during display in GridView. There are two ways to do this.
1. First Way
Use Template Column of GridView. Write Inline coding to Format data.
For Example .
In Codebehind file Write Following Function.
To Fetch data using web service.
public DataTable GetData()
{
DataTable dt = new DataTable();
Lists lst = new Lists();
lst.Url = "/_vti_bin/lists.asmx";'>http://<your site>/_vti_bin/lists.asmx";
lst.Credentials = new System.Net.NetworkCredential("test", "test");
XmlNode result =lst.GetListItems("Orders", String.Empty, null, null, int.MaxValue.ToString(), null, string.Empty);
System.IO.StringReader read = new System.IO.StringReader(result.OuterXml);
DataSet dst = new DataSet();
dst.ReadXml(read);
if (dst.Tables.Count == 2 && Convert.ToInt32(dst.Tables[0].Rows[0]["ItemCount"].ToString()) > 0)
{
dt = dst.Tables[1];
}
else
{
dt.Columns.Add(new DataColumn("ows_CustomerID",typeof(string)));
}
return dt;
}
// Enum For Display Mode of LookUpField.
public enum Mode
{
ID,
Value,
All
};
// Function that format data (This Fuction must be Public or Proctected)
protected string GetLookupValue(string value,Mode LookUpDisplayMode)
{
string returnVal = String.Empty;
string[] str;
try
{
switch (LookUpDisplayMode)
{
case Mode.All:
returnVal = value;
break;
case Mode.ID:
str = value.Split(new string[] { ";#" }, StringSplitOptions.None);
returnVal = str[0];
break;
case Mode.Value:
str = value.Split(new string[] { ";#" }, StringSplitOptions.None);
returnVal = str[1];
break;
}
}
catch
{
}
return returnVal;
}
And in ASPX Page.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# GetLookupValue(Eval("ows_CustomerID").ToString(),Mode.ID) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# GetLookupValue(Eval("ows_CustomerID").ToString(),Mode.Value) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# GetLookupValue(Eval("ows_CustomerID").ToString(),Mode.All) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2. Other Way
This way is create custom DataBoundField that inherits from BoundField.This is required one time coding as well as avoid inline coding.
Put following class in either App_Code or create class library and use it any application you want.
//LookupField Class |
public class LookupField : BoundField public LookupField() public Mode LookUpDisplayMode protected override string FormatDataValue(object dataValue, bool encode) protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) protected override DataControlField CreateField() protected override void OnDataBindField(object sender, EventArgs e) |
// ASPX Page. |
Register Control <%@ Register TagPrefix="CC" Assembly="App_Code" Namespace="LookupFields" %> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" |
In above two way use following code to bind GridView.
//ASPX Page protected void Page_Load(object sender, EventArgs e) |
Note: Implementation of custom LookupField is not complete. You may required to add or override few more function in order to work with diffrent mode of GridView Row. (Row Selection , Editing etc).
Please give me your comment.
2 comments:
My current project requires ability to display choice column value via color or image associated with a choice
But Sharepoint standard packaged misses that control
I am looking for available solutions on market
I came across
http://amazepoint.com/Products/VisualChoiceColumn/Overview.aspx
Does anybody has experiece using it?
I never used VisualChoiceColumn. If you want to create your own column than you can creare custom
column.
http://vspug.com/nicksevens/2007/08/31/create-custom-field-types-for-sharepoint/
Post a Comment