Edit,delete,update operations in SPgridview in sharepoint 2010 using Webparts

4k views Asked by At

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();
}
2

There are 2 answers

0
Amay Kulkarni On

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bindata(); } }

    public void Bindata()
    {
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["VisualWebpart"];

        DataTable dt = new DataTable();
        dt.Columns.Add("Title", typeof(string));

        foreach (SPListItem item in list.Items)
        {
            DataRow dr = dt.NewRow();
            dr["Title"] = item["Title"].ToString();
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    protected void grd_Insert(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["VisualWebpart"];
            SPListItemCollection myColl = lst.Items;
            TextBox txtTitle = (TextBox)GridView1.FooterRow.FindControl("txtTitle");
            SPListItem item = myColl.Add();
            item["Title"] = txtTitle.Text;

            item.Update();
            txtTitle.Text = "";
            Bindata();
         }

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        Bindata();
     }


    protected void GridView1_Cancel(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;
        Bindata();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int Title = row.DataItemIndex;

        SPWeb currentWeb = SPContext.Current.Web;
        SPList lst = currentWeb.Lists["VisualWebpart"];
        SPListItemCollection myColl = lst.Items;
        int itemcount = myColl.Count;


        for (int i = 0; i <= itemcount-1; i++)
        {
            SPListItem item = myColl[i];
            if (Title==i)
            {
                myColl.Delete(i);
            }
        }

        Bindata();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int Title = row.DataItemIndex;

        SPWeb currentWeb = SPContext.Current.Web;
        SPList lst = currentWeb.Lists["VisualWebpart"];
        SPListItemCollection myColl = lst.Items;
        int itemcount = myColl.Count;
        TextBox txtTit = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTit");


        string d = txtTit.Text;

        for (int i = 0; i <= itemcount - 1; i++)
        {
            SPListItem item = myColl[i];
            if (Title == i)
            {
                item["Title"] = d;
                item.Update();

            }
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
}
3
Rich Bennema On

You will need to write all of the code for updates and deletes. It is not provided automatically.

Personally, I would recommend using the out of the box List View web part that points to a Datasheet view rather than trying to create my own.

But if you must write custom code, your code above might be able to be simplified from this:

DataTable table = new DataTable();
table.Columns.Add("EmpName", typeof(string));
table.Columns.Add("Empaddress", typeof(string));    
DataRow row;
foreach (SPListItem result in items)
{
    row = table.Rows.Add();
    row["EmpName"] = result["EmpName"].ToString();
    row["Empaddress"] = result["Empaddress"].ToString();
}

to this:

DataTable table = items.GetDataTable();