Question : How To bind sharepoint List with Grid view in Webpart? Part 3 adding Checkbox
Solution : adding checkbox for selection for this we have to write a class first.
name of class : CheckBoxSupportClass.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 WBP_TempWebPart
{
public class CheckBoxSupportClass:ITemplate
{
private ListItemType _itemType;
private string _columnName;
public CheckBoxSupportClass(ListItemType itemType, string columnName)
{
_itemType = itemType;
_columnName = columnName;
}
#region ITemplate Members
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;
}
}
#endregion
}
}
--------------------------------
and then
-------------------------------
2) Now by adding reference of the above class we have to code the following
private void BindGridWithList() {
SPGridView oGrid = new SPGridView();
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Personnel"];
SPQuery myquery = new SPQuery();
myquery.Query = "<Where><Neq><FieldRef Name='EmployeeName' /><Value Type='Text'> </Value></Neq></Where><OrderBy><FieldRef Name='EmployeeName' Ascending='True' /></OrderBy>";
SPListItemCollection myitemcol = list.GetItems(myquery);
TemplateField selectTaskColumn = new TemplateField();
selectTaskColumn.HeaderText = "Select Task";
selectTaskColumn.ItemTemplate = new CheckBoxSupportClass(ListItemType.Item, "Select Task");
oGrid.Columns.Add(selectTaskColumn);
DataTable table = new DataTable();
//table.Columns.Add("Select");
table.Columns.Add("Employee Name");
table.Columns.Add("Employee Group");
table.Columns.Add("Employee JobRole");
table.Columns.Add("Status");
table.Columns.Add("Last Taken");
table.Columns.Add("Applied Assignment");
string status = string.Empty;
foreach (SPListItem item in myitemcol){
DataRow row = table.NewRow();
//row["Select"] = string.Empty;
row["Employee Name"] = item["EmployeeName"].ToString();
row["Employee Group"] = item["EmployeeGroup"].ToString();
//row["Employee JobRole"] = item["EmployeeJobRole"].ToString();
row["Employee JobRole"] = GetJobRole(item["EmployeeJobRole"].ToString());
//to do call the function for find out the course status, last taken and applied assignment
//FindCourseStatusFromAssignment(item["EmployeeName"].ToString(), "LastTaken", "AppliedAssignment");
status = FindCourseStatusFromAssignment(item["EmployeeName"].ToString(), "LastTaken", "AppliedAssignment");
//row["Status"] = status;
row["Status"] = GetJobRole(status);
table.Rows.Add(row);
}
DataView mydataview = new DataView(table);
//table = mydataview.ToTable(true,"Select", "EmployeeName", "EmployeeGroup", "EmployeeJobRole", "Status");//, "Select Country", "Select State");
table = mydataview.ToTable(true, "Employee Name", "Employee Group", "Employee JobRole", "Status","Last Taken","Applied Assignment");//, "Select Country", "Select State");
oGrid.DataSource = table;
//oGrid.TemplateControl.Controls.Add(chkBox);
oGrid.DataBind();
}
Solution : adding checkbox for selection for this we have to write a class first.
name of class : CheckBoxSupportClass.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 WBP_TempWebPart
{
public class CheckBoxSupportClass:ITemplate
{
private ListItemType _itemType;
private string _columnName;
public CheckBoxSupportClass(ListItemType itemType, string columnName)
{
_itemType = itemType;
_columnName = columnName;
}
#region ITemplate Members
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;
}
}
#endregion
}
}
--------------------------------
and then
-------------------------------
2) Now by adding reference of the above class we have to code the following
private void BindGridWithList() {
SPGridView oGrid = new SPGridView();
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Personnel"];
SPQuery myquery = new SPQuery();
myquery.Query = "<Where><Neq><FieldRef Name='EmployeeName' /><Value Type='Text'> </Value></Neq></Where><OrderBy><FieldRef Name='EmployeeName' Ascending='True' /></OrderBy>";
SPListItemCollection myitemcol = list.GetItems(myquery);
TemplateField selectTaskColumn = new TemplateField();
selectTaskColumn.HeaderText = "Select Task";
selectTaskColumn.ItemTemplate = new CheckBoxSupportClass(ListItemType.Item, "Select Task");
oGrid.Columns.Add(selectTaskColumn);
DataTable table = new DataTable();
//table.Columns.Add("Select");
table.Columns.Add("Employee Name");
table.Columns.Add("Employee Group");
table.Columns.Add("Employee JobRole");
table.Columns.Add("Status");
table.Columns.Add("Last Taken");
table.Columns.Add("Applied Assignment");
string status = string.Empty;
foreach (SPListItem item in myitemcol){
DataRow row = table.NewRow();
//row["Select"] = string.Empty;
row["Employee Name"] = item["EmployeeName"].ToString();
row["Employee Group"] = item["EmployeeGroup"].ToString();
//row["Employee JobRole"] = item["EmployeeJobRole"].ToString();
row["Employee JobRole"] = GetJobRole(item["EmployeeJobRole"].ToString());
//to do call the function for find out the course status, last taken and applied assignment
//FindCourseStatusFromAssignment(item["EmployeeName"].ToString(), "LastTaken", "AppliedAssignment");
status = FindCourseStatusFromAssignment(item["EmployeeName"].ToString(), "LastTaken", "AppliedAssignment");
//row["Status"] = status;
row["Status"] = GetJobRole(status);
table.Rows.Add(row);
}
DataView mydataview = new DataView(table);
//table = mydataview.ToTable(true,"Select", "EmployeeName", "EmployeeGroup", "EmployeeJobRole", "Status");//, "Select Country", "Select State");
table = mydataview.ToTable(true, "Employee Name", "Employee Group", "Employee JobRole", "Status","Last Taken","Applied Assignment");//, "Select Country", "Select State");
oGrid.DataSource = table;
//oGrid.TemplateControl.Controls.Add(chkBox);
oGrid.DataBind();
}