设为首页 收藏本站
查看: 1053|回复: 0

[经验分享] Adding CheckBoxes in SharePoint GridView (SPGridView)

[复制链接]

尚未签到

发表于 2017-5-24 10:13:03 | 显示全部楼层 |阅读模式
原文链接

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/checkboxspgridviewsharepoint10292009155022PM/checkboxspgridviewsharepoint.aspx
  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.
  
DSC0000.gif
 
  Choose trust level to Fully. Or in other words deploy into the GAC.
  
DSC0001.gif
 
  Step 2

Add a class to the Web Part project. Give this class any name. I am giving name here CheckBoxTemplate
  
DSC0002.gif
 


  • 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.
  
DSC0003.gif
 
  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
  
  
DSC0004.gif
 
  Conclusion

In this article, I have shown how to add a checkbox in SPGridview. Thanks for reading.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-380364-1-1.html 上篇帖子: 开发自定义字段类型 sharepoint 下篇帖子: SharePoint 2007 启用企业版功能遇到的权限问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表