为DataGrid创建自定义列控件(四)
生活随笔
收集整理的這篇文章主要介紹了
为DataGrid创建自定义列控件(四)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
全選和多選的功能在DataGrid中使用的時候很多,以前我們都是創建CheckBox模板列,然后在后臺中捕獲ChecnkBox的選擇情況來實現全選或多選.現在為了加快開發的速度,使用這個CheckBoxColumn列控件,可以很方便的實現多選或全選的功能.
??????? 代碼如下:
??????? CheckBoxColumn
public?class?CheckBoxColumn?:?DataGridColumn
????{
????????private?String?_strId=String.Empty;
????
????????public?CheckBoxColumn():?base()
????????{
????????}
????????
????????public?override?void?InitializeCell(TableCell?cell,?int?columnIndex,?ListItemType?itemType)?
????????{
??????????
????????????//重寫父類InitializeCell方法
????????????base.InitializeCell(cell,?columnIndex,?itemType);
????????????????????????
????????????//加入多選框
????????????if(itemType?==?ListItemType.EditItem?||?itemType?==?ListItemType.Item?||?itemType?==?ListItemType.AlternatingItem?||?itemType?==?ListItemType.SelectedItem)
????????????{
????????????????HtmlInputCheckBox?checkbox?=?new?HtmlInputCheckBox();
????????????????//可以自定義ID
????????????????if(_strId==String.Empty)
????????????????{
????????????????????checkbox.Name?=?"checkboxCol";
????????????????????checkbox.ID?=?"checkboxCol";
????????????????}
????????????????else
????????????????{
????????????????????checkbox.Name?=?myID;
????????????????????checkbox.ID=myID;
????????????????}
????????
????????????????cell.Controls.Add(checkbox);
????????????}
????????}
????????private?String?CreateName()
????????{
????????????Random?rnd=new?Random(0);
????????????String?_strNameValue=Convert.ToString((int)rnd.Next(100));
????????????return?_strNameValue;
????????}
????????private?String?myID
????????{
????????????get
????????????{
????????????????return?_strId;
????????????}
????????}
????
????????public?String?ID
????????{
????????????
????????????set
????????????{
????????????????_strId=value;
????????????}
????????}
????????public?override?string?HeaderText
????????{
????????????get
????????????{
????????????????return?base.HeaderText;
????????????}
????????????set
????????????{
????????????????base.HeaderText?=?value;
????????????}
????????}
????????public?override?TableItemStyle?HeaderStyle
????????{
????????????get
????????????{
????????????????TableItemStyle?t=new?TableItemStyle();
????????????????t.HorizontalAlign=HorizontalAlign.Center;
????????????????return?t;
????????????}
????????}
????????//獲得選中的Index值
????????public?Int32[]?SelectedIndexes?
????????{
????????????get?
????????????{
????????????????ArrayList?selectedIndexList?=?new?ArrayList();
????????????????//獲得DataGrid中的選擇框
????????????????foreach(?DataGridItem?item?in?this.Owner.Items?)?
????????????????{
????????????????????HtmlInputCheckBox?chkBox=null;
????????????????????if(_strId==String.Empty)
????????????????????{
????????????????????????chkBox?=?(HtmlInputCheckBox)?item.FindControl("checkboxCol");
????????????????????}
????????????????????else
????????????????????{
????????????????????????chkBox?=?(HtmlInputCheckBox)?item.FindControl(myID);
????????????????????}
????????????????????
????????????????????//如果選中,就把選中的Index值放入ArrayList中
????????????????????if?(?chkBox?!=?null?&&?chkBox.Checked?)??
????????????????????{
????????????????????????selectedIndexList.Add(?item.ItemIndex?);
????????????????????}?
????????????????????
????????????????}
????????????????return?(Int32[])selectedIndexList.ToArray(typeof(?System.Int32?)?);
????????????}
????????}
????????//獲得未選中的Index值
????????public?Int32[]?UnSelectIndexes
????????{
????????????get
????????????{
????????????????ArrayList?UnSelectIndexList=new?ArrayList();
????????????????foreach(DataGridItem?item?in?this.Owner.Items)
????????????????{
????????????????????HtmlInputCheckBox?chkBox=null;
????????????????????if(_strId==String.Empty)
????????????????????{
????????????????????????chkBox=(HtmlInputCheckBox)item.FindControl("checkboxCol");
????????????????????}
????????????????????else
????????????????????{
????????????????????????chkBox=(HtmlInputCheckBox)item.FindControl(myID);
????????????????????}
????????????????????//If?it's?not?selected?then?add?it?to?the?arraylist
????????????????????if(chkBox!=null&&chkBox.Checked==false)
????????????????????{
????????????????????????UnSelectIndexList.Add(item.ItemIndex);
????????????????????}
????????????????}
????????????????return?(Int32[])UnSelectIndexList.ToArray(typeof(System.Int32));
????????????}
????????}
????????//獲得選中的DataKeys值
????????public?object[]?SelectedDataKeys?
????????{
????????????get?
????????????{
????????????????ArrayList?dataKeyList?=?new?ArrayList();
????????????????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?)?);
????????????}
????????????
????????}
????????
????????//獲得未選中的DataKeys值
????????public?object[]?UnSelectDataKeys
????????{
????????????get
????????????{
????????????????ArrayList?UnSelDataKeyList=new?ArrayList();
????????????????if(this.Owner.DataKeys.Count>0)
????????????????{
????????????????????foreach(Int32?unSelectIndex?in?UnSelectIndexes)
????????????????????{
????????????????????????object?DataKey=(this.Owner.DataKeys[unSelectIndex].ToString());
????????????????????????UnSelDataKeyList.Add(DataKey);
????????????????????}
????????????????}
????????????????return?(object[])UnSelDataKeyList.ToArray(typeof(object));
????????????}
????????}
????????????}????????
??????? 看過我前面幾篇列控件介紹的朋友對上面的代碼一定不會有什么問題.
??????? 首先還是在DataGrid中引用這個列控件,方法和以前一樣,這里就不多說了.
??????? 然后看看具體的使用:獲得選擇行的Index值
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectIndexes)//SelectIndexes獲得選擇的Index值
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????} 獲得選擇行的DataKeys值
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectedDataKeys)//SelectedDataKeys獲得選擇的DataKeys值
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????} 獲得未選擇行的Index值和獲得未選擇行的DataKeys值
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.UnSelectIndexes)
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????}
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.UnSelectedDataKeys)
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????} 全選/取消全選
foreach(DataGridItem?item?in?this.DataGrid1.Items)
????????????{
????????????????HtmlInputCheckBox?chkBox=(HtmlInputCheckBox)item.FindControl("checkboxCol");
????????????????chkBox.Checked?=?true;????????
????????????}
//如果你自定義了列控件的ID,"checkboxCol"換成自定義的ID值 獲得選擇行的任意列的
????????????CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectIndexes)
????????????{
????????????????Response.Write(DataGrid1.Items[(int)index].Cells[1].Text);
????????????}
????????????{
????????????????CheckBox?headerCheckBox?=?new?CheckBox();
????????????????headerCheckBox.ID?=?"chkAll";
????????????????headerCheckBox.CheckedChanged?+=?new?EventHandler(this.headerCheckBox_CheckedChanged);
????????????????headerCheckBox.AutoPostBack?=?true;
????????????????headerCheckBox.Text?=?"全選/取消";
????????????????cell.Controls.Add(headerCheckBox);
????????????}
private?void?headerCheckBox_CheckedChanged(object?sender,?EventArgs?e)
????????{
????????????foreach?(DataGridItem?item?in?this.Owner.Items)
????????????{
????????????????//iterate?each?DataGridItem?and?find?our?checkbox
????????????????HtmlInputCheckBox?chkBox?=?(HtmlInputCheckBox)?item.FindControl("checkboxCol");
????????????????//now?set?that?checkboxCol?value?=?to?selected
????????????????if(chkBox.Checked?==?false)
????????????????????chkBox.Checked?=?true;
????????????????else
????????????????????chkBox.Checked?=?false;
????????????}
????????}
???
??????? 代碼如下:
??????? CheckBoxColumn
public?class?CheckBoxColumn?:?DataGridColumn
????{
????????private?String?_strId=String.Empty;
????
????????public?CheckBoxColumn():?base()
????????{
????????}
????????
????????public?override?void?InitializeCell(TableCell?cell,?int?columnIndex,?ListItemType?itemType)?
????????{
??????????
????????????//重寫父類InitializeCell方法
????????????base.InitializeCell(cell,?columnIndex,?itemType);
????????????????????????
????????????//加入多選框
????????????if(itemType?==?ListItemType.EditItem?||?itemType?==?ListItemType.Item?||?itemType?==?ListItemType.AlternatingItem?||?itemType?==?ListItemType.SelectedItem)
????????????{
????????????????HtmlInputCheckBox?checkbox?=?new?HtmlInputCheckBox();
????????????????//可以自定義ID
????????????????if(_strId==String.Empty)
????????????????{
????????????????????checkbox.Name?=?"checkboxCol";
????????????????????checkbox.ID?=?"checkboxCol";
????????????????}
????????????????else
????????????????{
????????????????????checkbox.Name?=?myID;
????????????????????checkbox.ID=myID;
????????????????}
????????
????????????????cell.Controls.Add(checkbox);
????????????}
????????}
????????private?String?CreateName()
????????{
????????????Random?rnd=new?Random(0);
????????????String?_strNameValue=Convert.ToString((int)rnd.Next(100));
????????????return?_strNameValue;
????????}
????????private?String?myID
????????{
????????????get
????????????{
????????????????return?_strId;
????????????}
????????}
????
????????public?String?ID
????????{
????????????
????????????set
????????????{
????????????????_strId=value;
????????????}
????????}
????????public?override?string?HeaderText
????????{
????????????get
????????????{
????????????????return?base.HeaderText;
????????????}
????????????set
????????????{
????????????????base.HeaderText?=?value;
????????????}
????????}
????????public?override?TableItemStyle?HeaderStyle
????????{
????????????get
????????????{
????????????????TableItemStyle?t=new?TableItemStyle();
????????????????t.HorizontalAlign=HorizontalAlign.Center;
????????????????return?t;
????????????}
????????}
????????//獲得選中的Index值
????????public?Int32[]?SelectedIndexes?
????????{
????????????get?
????????????{
????????????????ArrayList?selectedIndexList?=?new?ArrayList();
????????????????//獲得DataGrid中的選擇框
????????????????foreach(?DataGridItem?item?in?this.Owner.Items?)?
????????????????{
????????????????????HtmlInputCheckBox?chkBox=null;
????????????????????if(_strId==String.Empty)
????????????????????{
????????????????????????chkBox?=?(HtmlInputCheckBox)?item.FindControl("checkboxCol");
????????????????????}
????????????????????else
????????????????????{
????????????????????????chkBox?=?(HtmlInputCheckBox)?item.FindControl(myID);
????????????????????}
????????????????????
????????????????????//如果選中,就把選中的Index值放入ArrayList中
????????????????????if?(?chkBox?!=?null?&&?chkBox.Checked?)??
????????????????????{
????????????????????????selectedIndexList.Add(?item.ItemIndex?);
????????????????????}?
????????????????????
????????????????}
????????????????return?(Int32[])selectedIndexList.ToArray(typeof(?System.Int32?)?);
????????????}
????????}
????????//獲得未選中的Index值
????????public?Int32[]?UnSelectIndexes
????????{
????????????get
????????????{
????????????????ArrayList?UnSelectIndexList=new?ArrayList();
????????????????foreach(DataGridItem?item?in?this.Owner.Items)
????????????????{
????????????????????HtmlInputCheckBox?chkBox=null;
????????????????????if(_strId==String.Empty)
????????????????????{
????????????????????????chkBox=(HtmlInputCheckBox)item.FindControl("checkboxCol");
????????????????????}
????????????????????else
????????????????????{
????????????????????????chkBox=(HtmlInputCheckBox)item.FindControl(myID);
????????????????????}
????????????????????//If?it's?not?selected?then?add?it?to?the?arraylist
????????????????????if(chkBox!=null&&chkBox.Checked==false)
????????????????????{
????????????????????????UnSelectIndexList.Add(item.ItemIndex);
????????????????????}
????????????????}
????????????????return?(Int32[])UnSelectIndexList.ToArray(typeof(System.Int32));
????????????}
????????}
????????//獲得選中的DataKeys值
????????public?object[]?SelectedDataKeys?
????????{
????????????get?
????????????{
????????????????ArrayList?dataKeyList?=?new?ArrayList();
????????????????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?)?);
????????????}
????????????
????????}
????????
????????//獲得未選中的DataKeys值
????????public?object[]?UnSelectDataKeys
????????{
????????????get
????????????{
????????????????ArrayList?UnSelDataKeyList=new?ArrayList();
????????????????if(this.Owner.DataKeys.Count>0)
????????????????{
????????????????????foreach(Int32?unSelectIndex?in?UnSelectIndexes)
????????????????????{
????????????????????????object?DataKey=(this.Owner.DataKeys[unSelectIndex].ToString());
????????????????????????UnSelDataKeyList.Add(DataKey);
????????????????????}
????????????????}
????????????????return?(object[])UnSelDataKeyList.ToArray(typeof(object));
????????????}
????????}
????????????}????????
??????? 看過我前面幾篇列控件介紹的朋友對上面的代碼一定不會有什么問題.
??????? 首先還是在DataGrid中引用這個列控件,方法和以前一樣,這里就不多說了.
??????? 然后看看具體的使用:
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectIndexes)//SelectIndexes獲得選擇的Index值
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????}
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectedDataKeys)//SelectedDataKeys獲得選擇的DataKeys值
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????}
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.UnSelectIndexes)
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????}
CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.UnSelectedDataKeys)
????????????{
????????????????Response.Write(index.ToString()+"<br>");
????????????}
foreach(DataGridItem?item?in?this.DataGrid1.Items)
????????????{
????????????????HtmlInputCheckBox?chkBox=(HtmlInputCheckBox)item.FindControl("checkboxCol");
????????????????chkBox.Checked?=?true;????????
????????????}
//如果你自定義了列控件的ID,"checkboxCol"換成自定義的ID值
????????????CheckBoxColumn?chkColumn?=?(CheckBoxColumn)this.DataGrid1.Columns[0];
????????????foreach(object?index?in?chkColumn.SelectIndexes)
????????????{
????????????????Response.Write(DataGrid1.Items[(int)index].Cells[1].Text);
????????????}
??????? 基本的使用就介紹完了,都非常的簡單.當然我們可以在這個列控件的基礎上擴展新的功能,比如在Head加入全選/取消選擇框
???????????
if(itemType?==?ListItemType.Header)????????????{
????????????????CheckBox?headerCheckBox?=?new?CheckBox();
????????????????headerCheckBox.ID?=?"chkAll";
????????????????headerCheckBox.CheckedChanged?+=?new?EventHandler(this.headerCheckBox_CheckedChanged);
????????????????headerCheckBox.AutoPostBack?=?true;
????????????????headerCheckBox.Text?=?"全選/取消";
????????????????cell.Controls.Add(headerCheckBox);
????????????}
private?void?headerCheckBox_CheckedChanged(object?sender,?EventArgs?e)
????????{
????????????foreach?(DataGridItem?item?in?this.Owner.Items)
????????????{
????????????????//iterate?each?DataGridItem?and?find?our?checkbox
????????????????HtmlInputCheckBox?chkBox?=?(HtmlInputCheckBox)?item.FindControl("checkboxCol");
????????????????//now?set?that?checkboxCol?value?=?to?selected
????????????????if(chkBox.Checked?==?false)
????????????????????chkBox.Checked?=?true;
????????????????else
????????????????????chkBox.Checked?=?false;
????????????}
????????}
???
轉載于:https://www.cnblogs.com/jierry/archive/2005/11/07/270804.html
總結
以上是生活随笔為你收集整理的为DataGrid创建自定义列控件(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一句话经典笑话
- 下一篇: VC#2005 Starter Kit: