编辑,删除,更新SharePoint 2010中使用WebParts的操作



我在我的SharePoint网站中有一个名称为" empdetails"的列表,并具有列(Empname String,empaddress字符串)。

我必须使用编辑,删除,更新功能将列表数据绑定到SpgridView。

我能够成功地将列表数据绑定到GridView,但是我无法为GridView提供编辑,删除,更新功能。

代码:

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

您需要编写所有代码以进行更新和删除。它没有自动提供。

就个人而言,我建议使用指向数据表视图的框出列表查看Web部分,而不是尝试创建自己的。

但是,如果您必须编写自定义代码,则可以从此简化上面的代码:

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

DataTable table = items.GetDataTable();

受保护的void 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();
    }
}

相关内容

  • 没有找到相关文章

最新更新