There is a class DataGridColumn
under the DataGrid net
as the base class for different column types of the DataGrid control.
for a list of all members of this type, see DataGridColumn members.
System.Object
System.Web.UI.WebControls.DataGridColumn
System.Web.UI.WebControls.BoundColumn
System.Web.UI.WebControls.ButtonColumn
System.Web.UI.WebControls.EditCommandColumn
System.Web.UI.WebControls.HyperLinkColumn
System.Web.UI.WebControls.TemplateColumn
we can write a class that inherits Datagirdcolumn, and that's it. Dropdownlistcolumn,checkboxlistcolumn and so on.
The code for the
control is as follows.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
namespace Customcolumn
{
///<summary>
Summary description of the datagridcustomcolumn.
///</summary>
public class DropDownListColumn:System.Web.UI.WebControls.DataGridColumn
{
//Data source
public DataTable DataSource;
//Display text field
public string DataTextField;
//
public string DataValueField;
//datagrid The field to bind in
public string DataField;
public string Datashowfield;
public Dropdownlistcolumn ()
{
//
//TODO: Add constructor logic here
//
}
public override void InitializeCell (TableCell cell, int columnindex, ListItemType itemType)
{
base. InitializeCell (cell, ColumnIndex, ItemType);
switch (itemType)
{
Case Listitemtype.header:
cell. Text = this. HeaderText;
break;
case ListItemType.Item:case ListItemType.AlternatingItem:
cell. DataBinding +=new EventHandler (cell_itemdatabinding);
break;
Case ListItemType.EditItem:
cell. DataBinding +=new EventHandler (cell_edititemdatabinding);
//========================
DropDownList DRP = new DropDownList ();
cell. Controls.Add (DRP);
break;
}
}
private void Cell_itemdatabinding (object sender, EventArgs e)
{
TableCell cell = (TableCell) sender;
DataGridItem DGI = (datagriditem) cell. NamingContainer;
Try
{
cell. Text = (DataBinder.Eval (DGI). Dataitem,datashowfield)). ToString ();
}
Catch
{
throw new Exception ("Please set field");
}
}
private void Cell_edititemdatabinding (Object Sender,eventargs e)
{
TableCell cell = (TableCell) sender;
DropDownList DRP = (DropDownList) cell. Controls[0];;
ListItem Item;
DataGridItem Dgi;
Try
{
//================
DRP. Items.Add (New ListItem ("Please select", "-1"));
//
for (int k=0;k<=datasource.rows.count-1;k++)
{
DataRow dr = Datasource.rows[k];
item = new ListItem ();
item. Text = Dr[datatextfield]. ToString ();
item. Value = Dr[datavaluefield]. ToString ();
DRP. Items.Add (item);
}
DGI = (DataGridItem) cell. NamingContainer;
string Value = DataBinder.Eval (DGI. Dataitem,datafield). ToString ();
item = DRP. Items.findbyvalue (Value);
if (item!=null)
{
item. Selected = true;
}
}
Catch
{
throw new Exception ("wrong data source.") ");
}
}
}
}
aspx page call.
First Sign up:
<%@ Register tagprefix= "Drpcolumn" namespace= "Customcolumn" assembly= "Dropdownlistcolumn"%>
Call
<asp:datagrid id= "Dgteacher" runat= "Server" width= "100%" autogeneratecolumns= "False" allowpaging= "True"
cssclass= "DataGrid" bordercolor= "#333333" borderstyle= "Solid" >
<Columns>
<drpcolumn:dropdownlistcolumn headertext= "mentor" ></DrpColumn:DropDownListColumn>
<asp:boundcolumn datafield= "SpecName2" readonly= "True" headertext= "Major" ></asp:BoundColumn>
<drpcolumn:dropdownlistcolumn datashowfield= "Specname" datafield= "Specguid" HeaderText= "concurrently hillock professional" ></ Drpcolumn:dropdownlistcolumn>
<asp:boundcolumn datafield=&