How to retreive value from a list and display it in a GridView in SharePoint Through Visual Studio
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace Sun_SharePointProject2. sunVisualWebPart1
{
class Data
{
public string studentId { get; set;}
public string name {get; set;}
public string address {get; set;}
public string section { get; set; }
}
public partial class sunVisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPList info = SPContext.Current.Web.Lists[" Student List"];
List<Data> data = new List<Data>();
foreach (SPListItem item in info.Items)
{
Data d = new Data();
d.studentId = item["Student ID"].ToString();
d.name = item["Name"].ToString();
d.address = item["Address"].ToString();
d.section=item["Section"]. ToString();
data.Add(d);
}
GridView1.DataSource = data;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message + "-" + ex.StackTrace;
}
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace Sun_SharePointProject2.
{
class Data
{
public string studentId { get; set;}
public string name {get; set;}
public string address {get; set;}
public string section { get; set; }
}
public partial class sunVisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPList info = SPContext.Current.Web.Lists["
List<Data> data = new List<Data>();
foreach (SPListItem item in info.Items)
{
Data d = new Data();
d.studentId = item["Student ID"].ToString();
d.name = item["Name"].ToString();
d.address = item["Address"].ToString();
d.section=item["Section"].
data.Add(d);
}
GridView1.DataSource = data;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message + "-" + ex.StackTrace;
}
}
}
}
"Student List" is your List name in SharePoint i guess..!!
ReplyDelete