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 884原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 922原文链接 http://johnholliday.net/ ... -
Filtering with SPGridView
2011-10-12 15:23 752原文链接 http://vspug.com/bobsbon ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1046原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1269原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 793原文链接 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 927原文链接 http://www.nearinfinity. ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 901原文链接 http://www.csharpest.net ... -
SharePoint的列级安全性
2011-10-10 11:25 1131原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1696原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1385原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1241原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 853原文链接 http://msdn.microsoft ...
相关推荐
Preface 1 Chapter 1: PowerShell Scripting Methods and Creating Custom Commands 7 Introduction 7 Setting up your Virtual Machine and running...Managing sandbox solutions in SharePoint site collections 286
Oracle Solaris 11.3 Adding and Updating Software in Oracle Solaris 11.3-190
Oracle Solaris 11.2 Adding and Updating Software in Oracle Solaris 11.2-134
Adding a system call in linux
SAP Fiori - Adding a Custom Workflow in 6 Steps
Adding placeholder functionality for TextField in SAPUI5
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 ...
Unity's single codebase approach minimizes inefficient switching among development tools and concentrates your attention on making great interactive experiences. Unity in Action teaches you how to ...
Adding New Hardware for Avaya
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 页面,比如新增链接或者隐藏某些链接等
A tutorial on adding columns to Explorer’s details view via a column handler shell extension.(13KB)
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...
Taking the pain out of adding a horizontal scrollbar to a listbox.给列表框添加水平滚动条
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) ...
Adding white noise to a signal with fixed SNR
Specification, updating the base definitions, adding new interfaces, and removing obsolete ones. This was called the 2008 version of POSIX.1, which included version 7 of the Base Specification and was...
语言:español 在Coto Digital中添加新卡时修复错误的扩展 该软件包是对尝试在结帐时上载新信用卡时在Coto Digital中发现的错误的临时修复。 在Coto修复错误之前,此扩展程序将帮助您启用需要上载卡的损坏的按钮。