By Dhananjay Kumar October 29, 2009
In this article, I am going to show how to add a checkboxes in SPGRidVIew. I will iterate through the SPGridView to find out the selected rows.
Objective:
In this article, I am going to show how to add a checkboxes in SPGRidVIew. I will iterate through the SPGridView to find out the selected rows.
Step 1
Create a SharePoint project by selecting Web Part template.
Choose trust level to Fully. Or in other words deploy into the GAC.
Step 2
Add a class to the Web Part project. Give this class any name. I am giving name here CheckBoxTemplate
- Add the namespace System.Web.UI
- Implement the interface ITemplate
- This class has been ListItemType properties; this will contain the item type.
- This contains a string property which holds the column name.
CheckBoxTemplate.cs
using System; using System.Collections.Generic; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Web.UI.HtmlControls; namespace AWebPart { class CheckBoxTemplate:ITemplate { private ListItemType _itemType; private string _columnName; public CheckBoxTemplate(ListItemType itemType, string columnName) { _itemType = itemType; _columnName = columnName; } public void InstantiateIn(Control container) { switch (_itemType) { case ListItemType.Header : LiteralControl header = new LiteralControl(); header.Text = string.Format("<b>{0}</b>", _columnName); container.Controls.Add(header); break; case ListItemType.Item : CheckBox checkboxitem = new CheckBox(); checkboxitem.ID = "selectedTask"; checkboxitem.Visible = true; container.Controls.Add(checkboxitem); HtmlInputHidden taskIdItem = new HtmlInputHidden(); taskIdItem.ID = "taskIdItem"; container.Controls.Add(taskIdItem); break; default : break; } } } }
Step 3
Create a class Author.cs. This class is simple entity class which is holding Author as entity.
Authors.cs
using System; using System.Collections.Generic; using System.Text; namespace AWebPart { public class Author { public string Name { get; set ;} public int NumberOfArticles { get; set; } } }
-
This is having a button, when we will click button we will loop through the grid view and find out the entire selected row.
-
While creating a grid view, we are adding Template Field as column. This column will contain the checkbox
TemplateField selectTaskColumn = new TemplateField();
selectTaskColumn.HeaderText = "Select Task";
selectTaskColumn.ItemTemplate = new CheckBoxTemplate(ListItemType.Item, "Select Task");
grv.Columns.Add(selectTaskColumn); - This code will loop through the all rows of Grid View and find out the selected row. We are iterating through and concatening all the authors in a string.
for (int idx = 0; idx < gridviewwithcheckbox.Rows.Count; idx++) { CheckBox selectCtl = (CheckBox)gridviewwithcheckbox.Rows[idx].FindControl("selectedTask"); HtmlInputHidden taskIdCtl = (HtmlInputHidden)gridviewwithcheckbox.Rows[idx].FindControl("taskIdItem"); if (selectCtl.Checked && taskIdCtl.Value != String.Empty) { //System.Windows.Forms.MessageBox.Show(taskIdCtl.Value.ToString()); str = str + taskIdCtl.Value.ToString(); } }
WebPart1.cs
using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using System.Windows; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; using System.Collections.Generic; using Microsoft.SharePoint.Utilities; using System.Data; using System.Web.UI.HtmlControls; namespace AWebPart { [Guid("00bc296d-8515-4d12-b876-82dc7861a8e1")] public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart { SPGridView gridviewwithcheckbox=null; public WebPart1() { } protected override void CreateChildControls() { base.CreateChildControls(); Panel p1 = new Panel(); this.Controls.Add(p1); gridviewwithcheckbox = new SPGridView(); createGridViewWithCheckBox(ref gridviewwithcheckbox); p1.Controls.Add(gridviewwithcheckbox); Button b1 = new Button(); b1.Text = "Click Here For Selected Item To Display"; p1.Controls.Add(b1); b1.Click += new EventHandler(b1_Click); } void b1_Click(object sender, EventArgs e) { string str = string.Empty; string strjavascript = string.Empty ; for (int idx = 0; idx < gridviewwithcheckbox.Rows.Count; idx++) { CheckBox selectCtl = (CheckBox)gridviewwithcheckbox.Rows[idx].FindControl("selectedTask"); HtmlInputHidden taskIdCtl = (HtmlInputHidden)gridviewwithcheckbox.Rows[idx].FindControl("taskIdItem"); if (selectCtl.Checked && taskIdCtl.Value != String.Empty) { //System.Windows.Forms.MessageBox.Show(taskIdCtl.Value.ToString()); str = str + taskIdCtl.Value.ToString(); } } Page.RegisterStartupScript("a", strjavascript); } public List<Author> GetAuthorDetails() { try { List<Author> Authors = new List<Author>() { new Author(){Name = "Praveen Masood",NumberOfArticles =200}, new Author(){Name = "R Raveen ",NumberOfArticles = 500}, new Author(){ Name ="Dhananjay Kumar",NumberOfArticles =85}, new Author(){Name =" Mahesh Chand ",NumberOfArticles =600} }; return Authors; } catch (Exception ex) { SPUtility.TransferToErrorPage(ex.Message); return null; } } public void createGridViewWithCheckBox(ref SPGridView grv) { try { // grv = new SPGridView(); DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("NArticles", typeof(int)); DataRow row; foreach (Author author in GetAuthorDetails()) { row = dt.Rows.Add(); row["Name"] = author.Name; row["NArticles"] = author.NumberOfArticles; } TemplateField selectTaskColumn = new TemplateField(); selectTaskColumn.HeaderText = "Select Task"; selectTaskColumn.ItemTemplate = new CheckBoxTemplate(ListItemType.Item, "Select Task"); grv.Columns.Add(selectTaskColumn); SPBoundField field; field = new SPBoundField(); field.HeaderText = "Name"; field.DataField = "Name"; grv.Columns.Add(field); field = new SPBoundField(); field.HeaderText = "Number of Articles"; field.DataField = "NArticles"; grv.Columns.Add(field); grv.AutoGenerateColumns = false; grv.DataSource = dt.DefaultView; grv.DataBind(); } catch (Exception ex) { SPUtility.TransferToErrorPage(ex.Message); } } private void gridviewwithcheckbox_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HtmlInputHidden itemId = (HtmlInputHidden)e.Row.FindControl("taskIdItem"); if (itemId != null) { DataRowView data = (DataRowView)e.Row.DataItem; itemId.Value = data["TaskId"].ToString(); } } } } }
Right click and deploy the web part to the sharepoint site.
Output
Conclusion
In this article, I have shown how to add a checkbox in SPGridview. Thanks for reading.
发表评论
-
Creating Hierarchical Menus with a CustomAction in SharePoint
2011-11-17 14:11 919原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 1009原文链接 http://johnholliday.net/ ... -
Filtering with SPGridView
2011-10-12 15:23 771原文链接 http://vspug.com/bobsbon ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1096原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1295原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 830原文链接 http://blogs.msdn.com/b/ ... -
How to add a custom action to a SharePoint list actions menu for a specific list
2011-10-11 10:56 957原文链接 http://www.nearinfinity. ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 950原文链接 http://www.csharpest.net ... -
SharePoint的列级安全性
2011-10-10 11:25 1161原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1735原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1429原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1298原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 899原文链接 http://msdn.microsoft ...
相关推荐
- **Provisioning Web Parts**: Scripts can be created to provision web parts in bulk on SharePoint pages, improving efficiency in page customization. #### Chapter 8: Managing Documents and Records in ...
- **Custom Actions:** Custom actions extend the SharePoint user interface by adding new commands or modifying existing ones. - **Ribbons:** Ribbons provide a customizable toolbar for SharePoint pages....
adding: lua5.3.5-x86/lua.exe (in=14336) (out=7311) (deflated 49%) adding: lua5.3.5-x86/lua.o (in=24873) (out=9654) (deflated 61%) adding: lua5.3.5-x86/lua5.3.5-static.lib (in=662596) (out=244441) ...
While ASP.NET 3.5 boasts server controls like the ListView and the incredibly flexible GridView, it also includes advancements in AJAX technology combined with JavaScript® debugging features in ...
Adding New Hardware for Avaya
BADI(Business Add-In)是SAP用来扩展标准行为的机制之一。在这一步骤中,您需要创建并实现一个BADI,以处理用户提交的决策。实现BADI的关键步骤包括: - 创建BADI:定义BADI的接口,包括方法签名和参数列表。 - ...
在Oracle Solaris 11.3中添加和更新软件是系统管理员日常维护工作的重要部分。Oracle Solaris操作系统是一个功能强大的企业级操作系统,提供了一系列高级管理工具和服务,以确保系统的稳定性和安全性。...
Oracle Solaris 11.2 是Oracle公司发布的一款先进的操作系统,尤其在企业级服务器和数据中心环境中广泛应用。在Oracle Solaris 11.2中,添加和更新软件是系统管理的重要部分,确保系统的性能、安全性和稳定性。...
Learn to use the document and picture libraries for adding and editing content, add discussion boards and surveys, receive alerts when documents and information have been added or changed, and ...
Remove "Add People" button in Google Meet. This is meant for managed student computers. 阻止用户/与会者将人员添加到Google Meet会话中。此扩展程序在Google Meets中隐藏了“添加人员”,适用于使用Google ...
cognos_pp_infrastructure_adding_and_removing_ui_sections_in_cognos_connection.pdf,可以定义cognos connection 页面,比如新增链接或者隐藏某些链接等
In the first edition, models initially developed to describe wave propagation in porous media saturated by heavy fluids are used to predict the acoustical performances of air saturated sound absorbing...
在本篇文章中,我们将深入探讨如何为SAPUI5中的`TextField`添加占位符功能。此技术是在SAPUI5框架默认不提供占位符功能的情况下实现的一种工作方法。 ### 添加占位符功能 #### 标题和描述概述 ...
标题 "A tutorial on adding columns to Explorer’s details view via" 是一篇关于如何通过列处理程序外壳扩展在Windows资源管理器(Explorer)的详细视图中添加自定义列的教程。这通常涉及到增强Windows操作系统中...
此外,"Adding-the-org-Chart-Back-into-a-SharePoint-My-Sit.pdf"这个文件可能是详细的步骤指南或者PowerShell脚本示例,建议下载并查阅以获取更具体的指导。在实际操作时,务必遵循文档中的说明,并根据自己的环境...
Adding white noise to a signal with fixed SNR