Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Update SharePoint List Data in a GridView (Part 1)
<!-- Assemblies, imports, and registrations go here -->
<asp:GridView ID="StarbucksGridView" runat="server" AutoGenerateEditButton="true" AutoGenerateColumns="true"
OnRowEditing="Edit_Row" OnRowCancelingEdit="Cancel_Edit"
OnRowUpdating="Update_Row" CellPadding="4" EnableModelValidation="True"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint;
namespace SampleProject.EditGridViewP1
public partial class EditGridViewP1UserControl : UserControl
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
//Select the current site, assuming the list is in this site.
SPWeb thisWeb = SPContext.Current.Web;
SPQuery query = new SPQuery();
//The list name is Starbucks.
SPList thisList = thisWeb.Lists["Starbucks"];
SPListItemCollection queryResults = thisList.GetItems(query);
//Construct a data table.
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Size", typeof(string));
dt.Columns.Add("Calories", typeof(int));
dt.Columns.Add("Seasonal", typeof(bool));
//Code to fill the DataTable goes here; I’ll omit it to save some space.
foreach (SPListItem item in queryResults) {
DataRow dr = dt.NewRow();
dr["ID"] = Convert.ToInt32(item["ID"].ToString());
dr["Title"] = item["Title"] != null ? item["Title"].ToString() : "";
dr["Size"] = item["Size"] != null ? item["Size"].ToString() : "";
dr["Calories"] = item["Calories"] != null ? Convert.ToInt32(item["Calories"].ToString()) : 0;
dr["Seasonal"] = item["Seasonal"] != null ? Convert.ToBoolean(item["Seasonal"].ToString()) : false;
} //end foreach
//Persist the table in a Session object.
Session["StarbucksData"] = dt;
//Remember, earlier we set AutoGenerateColumns to true,
//so there’s no need to define the columns in the code.
StarbucksGridView.DataSource = dt;
} //end Page_Load
private void Bind_Data()
//Bind to flashed table
StarbucksGridView.DataSource = Session["StarbucksData"];
} //end Bind_Data
protected void Edit_Row(object sender, GridViewEditEventArgs e)
//Set the index of the row to be edited.
StarbucksGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
} //end Edit_Row
protected void Cancel_Edit(object sender, GridViewCancelEditEventArgs e)
//Reset the edit index.
StarbucksGridView.EditIndex = -1;
//Bind data to the GridView control.
} //end Cancel_Edit
protected void Update_Row(object sender, GridViewUpdateEventArgs e)
//Get the updated values from the controls.
//Please note that Cells[0] is the Update/Cancel command column.
GridViewRow row = StarbucksGridView.Rows[e.RowIndex];
string id = ((TextBox)row.Cells[1].Controls[0]).Text;
string title = ((TextBox)row.Cells[2].Controls[0]).Text;
string size = ((TextBox)row.Cells[3].Controls[0]).Text;
string calories = ((TextBox)row.Cells[4].Controls[0]).Text;
bool seasonal = ((CheckBox)row.Cells[5].Controls[0]).Checked;
//Retrieve the data from the Session object.
DataTable ds = (DataTable)Session["StarbucksData"];
//Update the values in the Session table.
ds.Rows[row.DataItemIndex]["ID"] = Convert.ToInt32(id);
ds.Rows[row.DataItemIndex]["Title"] = title;
ds.Rows[row.DataItemIndex]["Size"] = size;
ds.Rows[row.DataItemIndex]["Calories"] = Convert.ToInt32(calories);
ds.Rows[row.DataItemIndex]["Seasonal"] = seasonal;
//Update the values in the SharePoint List.
SPWeb thisWeb = SPContext.Current.Web;
SPList thisList = thisWeb.Lists["Starbucks"];
SPListItem item = thisList.GetItemById(Convert.ToInt32(id));
item["Title"] = title;
item["Size"] = size;
item["Calories"] = Convert.ToInt32(calories);
item["Seasonal"] = seasonal;
//Reset the edit index.
StarbucksGridView.EditIndex = -1;
//Bind data to the GridView control.
} //end Update_Row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment