Note:-
I have done some more operations with a GridView.
Ascx
|
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EUDGridViewUserControl.ascx.cs"
Inherits="EUDGridView.EUDGridView.EUDGridViewUserControl" %>
<asp:GridView ID="dgvEditUpdateDelete" runat="server" AutoGenerateColumns="False"
CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowDeleting="dgvEditUpdateDelete_RowDeleting"
OnRowEditing="dgvEditUpdateDelete_RowEditing" OnRowUpdating="dgvEditUpdateDelete_RowUpdating"
OnRowCancelingEdit="dgvEditUpdateDelete_RowCancelingEdit" DataKeyNames="ID">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ItemID">
<ItemTemplate>
<%# Eval("ID") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>' Columns="3" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%# Eval("Title") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>' Columns="3" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
|
Ascx.Cs
|
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
namespace EUDGridView.EUDGridView
{
public partial class EUDGridViewUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["Sample List001"];
SPListItemCollection myColl = lst.Items;
if (myColl.Count > 0)
{
if (!IsPostBack)
{
dgvEditUpdateDelete.DataSource = myColl.GetDataTable();
dgvEditUpdateDelete.DataBind();
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
public void getData()
{
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["Sample List001"];
SPListItemCollection myColl = lst.Items;
if (myColl.Count > 0)
{
dgvEditUpdateDelete.DataSource = myColl.GetDataTable();
dgvEditUpdateDelete.DataBind();
}
}
string strItemID1;
protected void dgvEditUpdateDelete_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
strItemID1 = dgvEditUpdateDelete.DataKeys[e.RowIndex].Value.ToString();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
DeleteRow(strItemID1);
getData();
});
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void dgvEditUpdateDelete_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
dgvEditUpdateDelete.EditIndex = e.NewEditIndex;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
getData();
});
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void dgvEditUpdateDelete_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
Label lbl = (Label)dgvEditUpdateDelete.Rows[e.RowIndex].Cells[2].FindControl("lblItemID");
TextBox txtTitle1 = (TextBox)dgvEditUpdateDelete.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
SPSecurity.RunWithElevatedPrivileges(delegate()
{
updateRow(lbl.Text, txtTitle1.Text);
dgvEditUpdateDelete.EditIndex = -1;
getData();
});
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
public void updateRow(string ItemID1,string Title)
{
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["Sample List001"];
SPListItem item = null;
item = lst.GetItemById(int.Parse(ItemID1));
currentWeb.AllowUnsafeUpdates = true;
item["Title"] = Title;
item.Update();
lst.Update();
currentWeb.AllowUnsafeUpdates = false;
}
public void DeleteRow(string ItemIDNew)
{
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["Sample List001"];
SPListItem item = null;
item = lst.GetItemById(int.Parse(ItemIDNew));
currentWeb.AllowUnsafeUpdates = true;
item.Delete();
lst.Update();
currentWeb.AllowUnsafeUpdates = false;
}
protected void dgvEditUpdateDelete_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dgvEditUpdateDelete.EditIndex = -1;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
getData();
});
}
}
}
|
Thanks prasad. Had a great help from this post.
ReplyDeleteOne query if you can help:
How to bind checkboxlist to populate checkboxes of a sharepoint list column in a single gridview cloumn, for the edit and update functionality?
Hi Das,
DeleteI had acheived it. Please see the below url for your reference.
http://sharepoint2010tutorialnew.blogspot.in/2013/01/edit-and-update-checkbox-list-with-in.html
Hi Das,
ReplyDeletesorry for the late reply. I have been busy these days and had not gone through the comments.Like how you populate the Dropdown same way you can populate CheckBox List & Radio Button List. Please follow the article below from my blog.
http://sharepoint2010tutorialnew.blogspot.in/2012/02/getting-data-from-sharepoint-list-to.html
To make it in a GridView Just create a Multiple Lookup column in the Back End first. In the Value pass ID and in text pass Title from Parent list. Now while updating just loop the checkbox list items and get the values with seperated ;. Now According to my knowledge it should work.
Same like TextBox You can embed checkbox list in gridview. but to bind it you have to use findcontrol concept in gridview and bind the data to Checkbox List. While retrieving seperate ; as get the string & show it in the gridview. While editing again loop the checkbox list items and check the checkbox items from the value which comes from the backend. I have to try it. Once i will try it i will give pass you the code.
Regards
Prasad
ThanX prasad
ReplyDeletehelped me alot.
Edit is not working properly
ReplyDelete