简体   繁体   English

使用Webparts在SharePoint 2010中的SPgridview中编辑,删除,更新操作

[英]Edit,delete,update operations in SPgridview in sharepoint 2010 using Webparts

I have a list in my SharePoint site with name "Empdetails" and having columns (EmpName string, Empaddress string). 我的SharePoint网站中有一个列表,名称为“ Empdetails”,并且具有列(EmpName字符串,Empaddress字符串)。

I have to bind the list data to the SpGridview with edit, delete, update functionality. 我必须使用编辑,删除,更新功能将列表数据绑定到SpGridview。

I am able to bind the list data to gridview successfully, but I am unable to provide edit, delete, update functionality to the gridview. 我能够将列表数据成功绑定到gridview,但是我无法为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();
}

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. 就个人而言,我建议使用指向“数据表”视图的开箱即用的“列表视图” Web部件,而不要尝试创建自己的。

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

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bindata(); 受保护的无效Page_Load(对象发送者,EventArgs e){如果(!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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM