为DataGrid 写一个 DropDownListColumn

Net下有一个类DataGridColumn

作为 DataGrid 控件的不同列类型的基类。

有关此类型所有成员的列表,请参阅 DataGridColumn 成员

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

我们完成可以写一个继承DataGirdColumn的类,这样就成了。DropDownListColumn,CheckBoxListColumn等。

 

控件的代码如下。

using System;
using System.Web.UI;
using System.Web.UI.WebControls ;
using System.Collections ;
using System.Data ;


namespace CustomColumn
{
 /// <summary>
 /// DataGridCustomColumn 的摘要说明。
 /// </summary>
 public class DropDownListColumn :System.Web.UI.WebControls.DataGridColumn
 {
  //数据源
  public DataTable DataSource ;
  //显示的文本字段
  public string DataTextField ;
  //
  public string DataValueField ;

  //DataGrid中要绑定的字段

  public string DataField ;


  public string DataShowField;
        

  public DropDownListColumn()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
    

  }

  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("请设置字段");
   }
  }

  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("请选择","-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("数据源不对。");
   }

  }

   
 }
}

 

Aspx页面调用。

先注册:

<%@ Register TagPrefix="DrpColumn" NameSpace="CustomColumn" Assembly="DropDownListColumn" %>

调用<%@ Register TagPrefix="DrpColumn" NameSpace="CustomColumn" Assembly="DropDownListColumn" %><%@ Register TagPrefix="DrpColumn" NameSpace="CustomColumn" Assembly="DropDownListColumn" %><%@ Register TagPrefix="DrpColumn" NameSpace="CustomColumn" Assembly="DropDownListColumn" %>

<asp:DataGrid id="dgTeacher" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True"
          CssClass="DataGrid" BorderColor="#333333" BorderStyle="Solid">
          <Columns>
           <DrpColumn:DropDownListColumn HeaderText="导师"></DrpColumn:DropDownListColumn>
           <asp:BoundColumn DataField="SpecName2" ReadOnly="True" HeaderText="主修专业"></asp:BoundColumn>
           <DrpColumn:DropDownListColumn DataShowField="SpecName" DataField="SpecGUID" HeaderText="兼岗专业"></DrpColumn:DropDownListColumn>
           <asp:BoundColumn DataField="CreatedDT" ReadOnly="True" HeaderText="创建日期" DataFormatString="{0:d}"></asp:BoundColumn>
           <asp:BoundColumn DataField="CreatedBY" ReadOnly="True" HeaderText="创建者"></asp:BoundColumn>
           <asp:BoundColumn DataField="SrcIP" ReadOnly="True" HeaderText="来源IP"></asp:BoundColumn>
           <asp:BoundColumn DataField="UpdatedBY" ReadOnly="True" HeaderText="修改者"></asp:BoundColumn>
           <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="操作" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
           <asp:ButtonColumn Text="&lt;span OnClick='return confirmdel();'&gt;删除&lt;/span&gt;" HeaderText="操作"
            CommandName="Delete"></asp:ButtonColumn>
          </Columns>
          <PagerStyle Mode="NumericPages"></PagerStyle>
         </asp:DataGrid>

 

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭