Write a dropdownlistcolumn for the DataGrid

Source: Internet
Author: User
Tags eval tostring
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=&







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.