I have used the following code to get the list items in a gridview!!
Though I have only three fields visible in the list - Title, Mobile and Email ID. It ends up showing around 10 different fields like created, modified etc.. I need only the three columns (Title, Mobile and Email ID) to show up in the list, how can I do it!!
public partial class Contact_DetailsUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
BindGrid();
}
catch (Exception ex)
{
}
}
private void BindGrid()
{
//string SiteListURL = ConfigurationManager.AppSettings["SiteListURL"].ToString();
try
{
string SiteListURL = "http://sonio274:6419/Lists/MS%20presales%20Contact/";
using (SPSite oSiteCollection = new SPSite(SiteListURL))
using (SPWeb web = oSiteCollection.OpenWeb())
{
DataTable dt = new DataTable();
SPQuery query = new SPQuery();
query.Query = "";
dt = web.Lists["MS presales Contact"].GetItems(query).GetDataTable();
if (dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}