65.9K
CodeProject 正在变化。 阅读更多。
Home

向 DataGrid 添加 CheckBox 列

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.55/5 (25投票s)

2003年5月30日

viewsIcon

328744

downloadIcon

4278

演示如何向 DataGrid 添加复选框列

Sample Image - DataGridCheckBoxCol.gif

引言

我最近参与的一个项目需要用户从列表中选择多个选项。与其使用多选列表框(这与网站的设计不太协调),我们决定创建一个可重用的控件,将复选框添加到 DataGrid 中。

使用代码

要在 DataGrid 中使用复选框列,只需在页面顶部注册该标签即可。

<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" 
    Assembly="DataGridCheckbox" %>

然后将复选框列添加到 DataGrid

<asp:DataGrid ID="dgTestGrid" Runat="server" AutoGenerateColumns=True 
    border="0" width="50%">
   <Columns>
   <chkbox:CheckBoxColumn/>
   </Columns>
  </asp:DataGrid>

CheckBoxColumn 类非常简单明了。

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataGridControls
{
 /// <summary>

 /// CheckBoxColumn Derives from DataGrid Column

 /// </summary>

 public class CheckBoxColumn : DataGridColumn
 {
    public CheckBoxColumn(): base()
      {
      }
  
      public override void InitializeCell(TableCell cell, 
        int columnIndex, ListItemType itemType) 
      {
           //let the base class initialize the cell

           base.InitializeCell(cell, columnIndex, itemType);



           //we don't want to add a checkbox to the header.

           if(    itemType == ListItemType.EditItem || 
            itemType == ListItemType.Item || 
            itemType == ListItemType.AlternatingItem || 
            itemType == ListItemType.SelectedItem){

                HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
                //assign an ID that we can use to find the control later

                checkbox.ID = "checkboxCol";
                cell.Controls.Add(checkbox);
           }
      }
      public Int32[] SelectedIndexes 
      {
           get 
           {
                ArrayList selectedIndexList = new ArrayList();
                //iterate each DataGridItem and find our checkbox

                foreach( DataGridItem item in this.Owner.Items ) 
                {
                     HtmlInputCheckBox chkBox = 
                        (HtmlInputCheckBox) item.FindControl("checkboxCol");
     
                     //If it's selected then add it to our ArrayList

                     if ( chkBox != null && chkBox.Checked )  
                     {
                          selectedIndexList.Add( item.ItemIndex );
                     } 
     
                }
                return (Int32[])selectedIndexList.ToArray(typeof( 
                        System.Int32 ) );
           }
      }
      public object[] SelectedDataKeys 
      {
           get 
           {
                //Just iterate each of the selectedindexes and 

                //match it up to the datakey field

                ArrayList dataKeyList = new ArrayList();
                //make sure the datakeys have some values

                if(this.Owner.DataKeys.Count > 0)
                {
                     foreach( Int32 selectedIndex in SelectedIndexes ) 
                     {
     
                          object DataKey = 
                              (this.Owner.DataKeys[selectedIndex].ToString());
                          dataKeyList.Add(DataKey);
                     }
                }
                return (object[])dataKeyList.ToArray(typeof( object ) );
           }
   
      }
   }
}

该类暴露 2 个属性: 

  • SelectedDataKeys: 返回一个包含 DataKey 值的 ArrayList
  • SelectedIndexes: 返回一个包含 selectedIndex 值的 Int32[]

要确定选择了哪个复选框

//On our button's Onclick


protected void btnSubmit_Click(object sender, EventArgs e)
  {
   //Get our checkboxcolumn, we know it's position is 0

   CheckBoxColumn chkbox = (CheckBoxColumn) dgTestGrid.Columns[0];
   
   foreach(object datakeyfield in chkbox.SelectedDataKeys)
   {
    Response.Write(datakeyfield.ToString() + "<br>");
   }
  }

大致就是这样了,DataGrid DataKeyField 可以是任何类型。我包含的示例将 DataTable 绑定到 DataGrid,您可以将 DataKeyField 从“ID”(int) 更改为“Name”(string)来查看代码如何处理不同类型的数据。

© . All rights reserved.