In the Following code i am going to generate a Grid view with dynamic check box columns.
as u can seen on the picture below.
//When Post back take place firstly have to clear the gridview
//So following code doing that thing
DataTable dtANull = new DataTable();
gvwStudentSubject.DataSource = dtANull;
gvwStudentSubject.DataBind();
//I am going to bind a data table to a grid view
//So first have to create the data table
DataTable dt = new DataTable();
DataColumn dcol;
// following columns are hidden columns.
dcol = new DataColumn("ID", typeof(System.String));
dt.Columns.Add(dcol);
dcol = new DataColumn("ExamEligibleID", typeof(System.String));
dt.Columns.Add(dcol);
//1st Column that appears on the grid view (As shown in the Image above)
dcol = new DataColumn("Name\\Subject", typeof(System.String));
dt.Columns.Add(dcol);
///Creating the Headings(Columns)
///dtExamSubject :-Data Table Contains the list of Subjects
for (int i = 0; i < dcol =" new" i =" 0;" drow =" dt.NewRow();" bfield =" new" datafield =" col.ColumnName;" headertext =" col.ColumnName;" showcheckbox =" Convert.ToBoolean(col.ExtendedProperties[" datasource =" dt;" visible =" false;" visible =" false;">
/// Summary description for CustomBoundField
///
public class CustomBoundField : DataControlField
{
public CustomBoundField()
{
//
// TODO: Add constructor logic here
//
}
#region Public Properties
///
/// This property describe weather the column should be an editable column or non editable column.
///
public bool Editable
{
get
{
object value = base.ViewState["Editable"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return true;
}
}
set
{
base.ViewState["Editable"] = value;
this.OnFieldChanged();
}
}
///
/// This property is to describe weather to display a check box or not.
/// This property works in association with Editable.
///
public bool ShowCheckBox
{
get
{
object value = base.ViewState["ShowCheckBox"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return false;
}
}
set
{
base.ViewState["ShowCheckBox"] = value;
this.OnFieldChanged();
}
}
//Add By Damith ON 2010-03-18
///
/// This property is to describe weather to display a Text Box box or not.
/// This property works in association with Editable.
///
public bool ShowTextBox
{
get
{
object value = base.ViewState["ShowTextBox"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return false;
}
}
set
{
base.ViewState["ShowTextBox"] = value;
this.OnFieldChanged();
}
}
public bool ShowDropDownList
{
get
{
object value = base.ViewState["ShowDropDownList"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return false;
}
}
set
{
base.ViewState["ShowDropDownList"] = value;
this.OnFieldChanged();
}
}
//*Added Code Ended
///
/// This property describe column name, which acts as the primary data source for the column.
/// The data that is displayed in the column will be retreived from the given column name.
///
public string DataField
{
get
{
object value = base.ViewState["DataField"];
if (value != null)
{
return value.ToString();
}
else
{
return string.Empty;
}
}
set
{
base.ViewState["DataField"] = value;
this.OnFieldChanged();
}
}
#endregion
#region Overriden Life Cycle Methods
///
/// Overriding the CreateField method is mandatory if you derive from the DataControlField.
///
///
protected override DataControlField CreateField()
{
return new BoundField();
}
///
/// Adds text controls to a cell's controls collection. Base method of DataControlField is
/// called to import much of the logic that deals with header and footer rendering.
///
/// A reference to the cell
/// The type of the cell
/// State of the row being rendered
/// Index of the row being rendered
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
//Call the base method.
base.InitializeCell(cell, cellType, rowState, rowIndex);
switch (cellType)
{
case DataControlCellType.DataCell:
this.InitializeDataCell(cell, rowState);
break;
case DataControlCellType.Footer:
this.InitializeFooterCell(cell, rowState);
break;
case DataControlCellType.Header:
this.InitializeHeaderCell(cell, rowState);
break;
}
}
#endregion
#region Custom Protected Methods
///
/// Determines which control to bind to data. In this a hyperlink control is bound regardless
/// of the row state. The hyperlink control is then attached to a DataBinding event handler
/// to actually retrieve and display data.
///
/// Note: This control was built with the assumption that it will not be used in a gridview
/// control that uses inline editing. If you are building a custom data control field and
/// using this code for reference purposes key in mind that if your control needs to support
/// inline editing you must determine which control to bind to data based on the row state.
///
/// A reference to the cell
/// State of the row being rendered
protected void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
{
//Check to see if the column is a editable and does not show the checkboxes.
if (Editable & !ShowCheckBox & !ShowTextBox & !ShowDropDownList)
{
Label lblText = new Label();
lblText.DataBinding += new EventHandler(lblText_DataBinding);
cell.Controls.Add(lblText);
}
else
{
if (ShowCheckBox)
{
CheckBox chkBox = new CheckBox();
cell.Controls.Add(chkBox);
Label lblText = new Label();
cell.Controls.Add(lblText);
}
//Code Added By Damith On 2010-03-18
else if (ShowTextBox)
{
TextBox txtBox = new TextBox();
txtBox.Width = 35;
txtBox.MaxLength = 5;
cell.Controls.Add(txtBox);
Label lblText = new Label();
cell.Controls.Add(lblText);
}
else if (ShowDropDownList)
{
DropDownList ddlList = new DropDownList();
Gcs.Sol.Common.Util.FillDropDown(ddlList, DropDownitems());
cell.Controls.Add(ddlList);
Label lblText = new Label();
cell.Controls.Add(lblText);
}
//Added Code Ended
else
{
Label lblText = new Label();
lblText.DataBinding += new EventHandler(lblText_DataBinding);
cell.Controls.Add(lblText);
}
}
}
private SortedList DropDownitems()
{
SortedList ddlList = new SortedList();
ddlList.Add(0, "#");
for (int i = 1; i <= 10; i++)
{
ddlList.Add(i,i.ToString());
}
return ddlList;
}
void lblText_DataBinding(object sender, EventArgs e)
{
// get a reference to the control that raised the event
Label target = (Label)sender;
Control container = target.NamingContainer;
// get a reference to the row object
object dataItem = DataBinder.GetDataItem(container);
// get the row's value for the named data field only use Eval when it is neccessary
// to access child object values, otherwise use GetPropertyValue. GetPropertyValue
// is faster because it does not use reflection
object dataFieldValue = null;
if (this.DataField.Contains("."))
{
dataFieldValue = DataBinder.Eval(dataItem, this.DataField);
}
else
{
dataFieldValue = DataBinder.GetPropertyValue(dataItem, this.DataField);
}
// set the table cell's text. check for null values to prevent ToString errors
if (dataFieldValue != null)
{
target.Text = dataFieldValue.ToString();
}
}
protected void InitializeFooterCell(DataControlFieldCell cell, DataControlRowState rowState)
{
CheckBox chkBox = new CheckBox();
cell.Controls.Add(chkBox);
}
protected void InitializeHeaderCell(DataControlFieldCell cell, DataControlRowState rowState)
{
Label lbl = new Label();
lbl.Text = this.DataField;
cell.Controls.Add(lbl);
}
void txtBox_DataBinding(object sender, EventArgs e)
{
// get a reference to the control that raised the event
TextBox target = (TextBox)sender;
Control container = target.NamingContainer;
// get a reference to the row object
object dataItem = DataBinder.GetDataItem(container);
// get the row's value for the named data field only use Eval when it is neccessary
// to access child object values, otherwise use GetPropertyValue. GetPropertyValue
// is faster because it does not use reflection
object dataFieldValue = null;
if (this.DataField.Contains("."))
{
dataFieldValue = DataBinder.Eval(dataItem, this.DataField);
}
else
{
dataFieldValue = DataBinder.GetPropertyValue(dataItem, this.DataField);
}
// set the table cell's text. check for null values to prevent ToString errors
if (dataFieldValue != null)
{
target.Text = dataFieldValue.ToString();
}
}
#endregion
}