- Add CustomBoundField file to your project (c# libaray project for creation of dll or in web application directly access it)
- To acces file in your code behind follow the Pseudo code below .If you are using as dll add refence then access in your application
Pseduo code
DataSet ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add("UserName");
// ds.Tables[0].Columns.Add("xyz");
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
// ds.Tables[0].NewRow();
ds.Tables[0].Rows[0][0] = "sdfg1";
//ds.Tables[0].Rows[0][1] = "0";
CustomBoundField cbound = new CustomBoundField();
cbound.HeaderText = "Name";
cbound.DataField = "UserName";
cbound.Editable = true ;
CustomBoundField cbound1 = new CustomBoundField();
cbound1.HeaderText = "tempcolum1";
cbound1.ShowCheckBox = true;
CustomBoundField cbound2 = new CustomBoundField();
cbound2.HeaderText = "tempcolum2";
cbound2.ShowRadioButton = true;
CustomBoundField cbound3 = new CustomBoundField();
cbound3.HeaderText = "tempcolum3";
cbound3.ShowTextBox = true;
CustomBoundField cbound4 = new CustomBoundField();
cbound4.HeaderText = "tempcolum4";
cbound4.ShowDropDown = true;
GridView1.Columns.Add(cbound);
GridView1.Columns.Add(cbound1);
GridView1.Columns.Add(cbound2);
GridView1.Columns.Add(cbound3);
GridView1.Columns.Add(cbound4);
GridView1.AutoGenerateColumns = false;
GridView1.DataSource = ds;
GridView1.DataBind() ;
CustomBound Field class (ref:Codeproject)
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();
}
}
public bool ShowRadioButton
{
get
{
object value = base.ViewState["ShowRadioButton"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return false;
}
}
set
{
base.ViewState["ShowRadioButton"] = value;
this.OnFieldChanged();
}
}
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 ShowDropDown
{
get
{
object value = base.ViewState["ShowDropDown"];
if (value != null)
{
return Convert.ToBoolean(value);
}
else
{
return false;
}
}
set
{
base.ViewState["ShowDropDown"] = value;
this.OnFieldChanged();
}
}
///
/// 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 & !ShowRadioButton & !ShowTextBox & !ShowDropDown)
{
string ID = Guid.NewGuid().ToString();
TextBox txtBox = new TextBox();
txtBox.Columns = 5;
txtBox.ID = ID;
txtBox.DataBinding += new EventHandler(txtBox_DataBinding);
cell.Controls.Add(txtBox);
}
else
{
if (ShowCheckBox)
{
CheckBox chkBox = new CheckBox();
cell.Controls.Add(chkBox);
}
else if (ShowRadioButton)
{
RadioButton rdBttn1 = new RadioButton();
rdBttn1.GroupName = "grp1";
rdBttn1.Text = "Yes";
RadioButton rdBttn2 = new RadioButton();
rdBttn2.GroupName = "grp1";
rdBttn2.Text = "No";
cell.Controls.Add(rdBttn1);
cell.Controls.Add(rdBttn2);
}
else if (ShowTextBox)
{
TextBox txtBox = new TextBox();
cell.Controls.Add(txtBox);
txtBox.Columns = 5;
}
else if (ShowDropDown)
{
DropDownList ddl = new DropDownList();
cell.Controls.Add(ddl);
// txtBox.Columns = 5;
}
else
{
Label lblText = new Label();
lblText.DataBinding += new EventHandler(lblText_DataBinding);
cell.Controls.Add(lblText);
}
}
}
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)
{
// TODO:if wnat add label to headet text then use it else comment it
//Label lbl = new Label();
//lbl.Text = "Name";//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
}