Tuesday, January 3, 2012

Binding data to Asp.Net GridView

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SharePoint2010Test;
using System.Xml;
public partial class SharePoint_2010_Default2 : System.Web.UI.Page
{
    DataSet dset = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SharePoint2010Test.Lists listService = new SharePoint2010Test.Lists();
            listService.UseDefaultCredentials=true;
            listService.Url = "http://agmsm:9999/sites/venkat/_vti_bin/Lists.asmx";
            //string strListName = "{296A9F52-5A48-4CB9-9294-47F93DF2F85F}";
            string strListName = "Tasks";
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");

            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            viewFields.InnerXml = "<FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /><FieldRef Name=\"Status\" /><FieldRef Name=\"Priority\" /><FieldRef Name=\"AssignedTo\" />";

            query.InnerXml = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            System.Xml.XmlNode nodeListItems = listService.GetListItems(strListName, null, query, viewFields, "5", null, null);
            //System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
            DataSet ds = getData(nodeListItems);
            DataTable dt = ds.Tables["row"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
            System.Xml.XmlElement batchElement = xmlDoc.CreateElement("Batch");
            batchElement.SetAttribute("OnError", "Continue");
            batchElement.SetAttribute("ListVersion", "1");
            Response.Write("<Table width='100%'>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strID = dt.Rows[i]["ows_ID"].ToString();
                string strTitle = dt.Rows[i]["ows_Title"].ToString();
                string strStatus = dt.Rows[i]["ows_Status"].ToString();
                string strPriority = dt.Rows[i]["ows_Priority"].ToString();
                string strAssignedTo = dt.Rows[i]["ows_AssignedTo"].ToString();
                strAssignedTo = strAssignedTo.Substring(strAssignedTo.IndexOf('#') + 1);
                Response.Write("<Tr>");
                Response.Write("<Td>");
                Response.Write(strID);
                Response.Write("</Td>");
                Response.Write("<Td>");
                Response.Write(strTitle);
                Response.Write("</Td>");
                if (strStatus == "Not Started")
                {
                    Response.Write("<Td bgcolor='Red'>");

                }
                else if (strStatus == "Completed")
                {
                    Response.Write("<Td bgcolor='Green'>");
                }
                else if (strStatus == "In Progress")
                {
                    Response.Write("<Td bgcolor='Orange'>");
                }
                else if (strStatus == "Deferred")
                {
                    Response.Write("<Td bgcolor='Pink'>");
                }
                Response.Write("<Strong>");
                Response.Write("<Font Color='White'>");
                Response.Write(strStatus);
                Response.Write("</Font>");
                Response.Write("</Strong>");
                Response.Write("</Td>");
                Response.Write("<Td>");
                Response.Write(strPriority);
                Response.Write("</Td>");
                Response.Write("<Td>");
                Response.Write(strAssignedTo);
                Response.Write("</Td>");
                Response.Write("</Tr>");
                Response.Write("<Tr>");
                Response.Write("<Td bgcolor='blue' colspan='5'>");
                Response.Write("</Td>");
                Response.Write("</Tr>");

            }
            Response.Write("</Table>");
            try
            {
                //listService.UpdateListItems(listName, batchElement);
                //XmlNode strNode = listService.UpdateListItems(strListName, batchElement);
                //Response.Write("Response " + strNode.OuterXml);
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Response.Write("Message:" + ex.Message);
            }
        }

    }
    public DataSet getData(XmlNode xmlnodeinput)
    {
        if (xmlnodeinput != null)
        {
            XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
            //dataset = new DataSet();
            if (xtr != null)
            {
                dset.ReadXml(xtr);
            }
        }
        return dset;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[3].Text = e.Row.Cells[3].Text.Substring(e.Row.Cells[3].Text.IndexOf('#') + 1);
    }
}

No comments:

Post a Comment