I have a list in my SharePoint site with name "Empdetails" and having columns (EmpName string, Empaddress string).
I have to bind the list data to the SpGridview with edit, delete, update functionality.
I am able to bind the list data to gridview successfully, but I am unable to provide edit, delete, update functionality to the gridview.
Code:
private void binddata()
{
SPWeb mySite = SPContext.Current.Web;
SPList myList = mySite.Lists["Empdetails"];
SPListItemCollection items = myList.Items;
//Here we will make a datatable and we will put our list data to the data table
DataTable table=new DataTable();
table.Columns.Add("EmpName", typeof(string));
table.Columns.Add("Empaddress", typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem result in items)
{
row = table.Rows.Add();
row["EmpName"] = result["EmpName"].ToString();
row["Empaddress"] = result["Empaddress"].ToString();
}
//binding data to gridview
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bindata(); } }