功能齐全的DataGrid

// 目前功能:编辑、排序、单选项、复选项、合并单元格、产生序列号、联动下拉框、删除、超连接、鼠标移动改变颜色、奇偶项颜色区分、排序、分页

1.html代码
< HTML >
    
< HEAD >
        
< title > DatagridTest </ title >
        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
        
< script  language ="javascript" >
            
function fun_option(obj)
            
{
                document.Form1.hiduserid.value
=obj.value;
            }
            
            
function chkAll_true()
            
{
                
var chkall= document.all["chkAll"];
                
var chkother= document.getElementsByTagName("input");
                
for (var i=0;i<chkother.length;i++)
                
{
                    
if( chkother[i].type=='checkbox')
                    
{
                        
if(chkother[i].id.indexOf('chkExport')>-1)
                        
{
                            
if(chkall.checked==true)
                            
{
                                chkother[i].checked
=true;
                            }

                            
else
                            
{
                                chkother[i].checked
=false;
                            }

                        }

                    }

                }

            }

            
            
function Delete()
            
{
                
var  al = new Array();
                
var chkother= document.getElementsByTagName("input");
                
for(var i=0,j=0;i<chkother.length;i++)
                
{
                    
if( chkother[i].type =='checkbox')
                    
{
                        
if(chkother[i].id.indexOf('chkExport')>-1)
                        
{
                            
if(chkother[i].checked==true)
                            
{
                                al[j] 
=chkother[i].value;
                                j
++;
                            }

                        }

                    }

                }

                
if (al == "")
                
{
                    alert('You need to select a row 
in the list before selecting Delete./r/nPlease select a row and try again.');
                }

                
else if (window.confirm("Are you sure to delete all these?"))
                
{
                    
var deleteID='<%=btndelete.ClientID%>';
                    window.document.all(deleteID).click();
                }

                else { return false; }
               
            }

        
</ script >
    
</ HEAD >
    
< body >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< TABLE  id ="Table1"  height ="100%"  cellSpacing ="1"  cellPadding ="1"  width ="100%"  border ="1" >
                
< tr >
                    
< td >< asp:dropdownlist  id ="DropDownList1"  runat ="server"  AutoPostBack ="True" ></ asp:dropdownlist >< asp:dropdownlist  id ="DropDownList2"  runat ="server"  AutoPostBack ="True" ></ asp:dropdownlist >< asp:dropdownlist  id ="DropDownList3"  runat ="server" ></ asp:dropdownlist >< asp:textbox  id ="TextBox1"  runat ="server" ></ asp:textbox >< asp:checkbox  id ="CheckBox1"  runat ="server"  Text ="可见否" ></ asp:checkbox >< asp:button  id ="Button1"  runat ="server"  Text ="保存" ></ asp:button >< asp:button  id ="Button2"  runat ="server"  Text ="导出Excel" ></ asp:button ></ td >
                
</ tr >
                
< tr  height ="30" >
                    
< td >< INPUT  id ="chkAll"  onclick ="chkAll_true()"  type ="checkbox" > 全选/取消 < asp:button  id ="btndelete"  runat ="server"  Text ="删除" ></ asp:button ></ td >
                
</ tr >
                
< TR  vAlign ="top" >
                    
< TD  width ="100%"  colSpan ="5" >< asp:datagrid  id ="DataGrid1"  runat ="server"  OnDeleteCommand ="Delete"  ForeColor ="Black"  GridLines ="None"
                            CellPadding
="2"  BackColor ="LightGoldenrodYellow"  BorderWidth ="1px"  BorderColor ="Tan"  AllowSorting ="True"  AutoGenerateColumns ="False"
                            DataKeyField
="UserID"  OnUpdateCommand ="Update"  OnCancelCommand ="Cancel"  OnEditCommand ="Edit"  PageSize ="5"  AllowPaging ="True" >
                            
< FooterStyle  BackColor ="Tan" ></ FooterStyle >
                            
< SelectedItemStyle  ForeColor ="GhostWhite"  BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
                            
< AlternatingItemStyle  BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
                            
< HeaderStyle  Font-Bold ="True"  BackColor ="Tan" ></ HeaderStyle >
                            
< Columns >
                                
< asp:TemplateColumn  HeaderText ="序号" >
                                    
< HeaderStyle  Width ="10px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
< INPUT  id =radio  onclick =fun_option(this)  type =radio  value ='<%#  DataBinder.Eval(Container.DataItem,"UserID") % > ' name=radio>
                                    
</ ItemTemplate >
                                    
< FooterTemplate >
                                    
</ FooterTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn  HeaderText ="序号" >
                                    
< HeaderStyle  Width ="10px" ></ HeaderStyle >
                                    
< FooterTemplate >
                                    
</ FooterTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn >
                                    
< HeaderStyle  Width ="10px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
< asp:CheckBox  id ="chkExport"  Runat ="server" ></ asp:CheckBox >
                                    
</ ItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:BoundColumn  DataField ="UserID"  SortExpression ="UserID"  ReadOnly ="True"  HeaderText ="UserID" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                
</ asp:BoundColumn >
                                
< asp:TemplateColumn  SortExpression ="UserName"  HeaderText ="姓名" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
< href ='form1.aspx?id=<%#  DataBinder.Eval(Container.DataItem,"UserID") % > &name = <% # DataBinder.Eval(Container.DataItem,"UserName" %> ' target="_blank">
                                            
<% # DataBinder.Eval(Container.DataItem,"UserName" %>
                                        
</ a >
                                    
</ ItemTemplate >
                                    
< EditItemTemplate >
                                        
< asp:TextBox  id =UserName  Text ='<%#  DataBinder.Eval(Container.DataItem,"UserName") % > ' Width="100%" Runat="server">
                                        
</ asp:TextBox >
                                    
</ EditItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn  SortExpression ="province"  HeaderText ="省市" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
<% # DataBinder.Eval(Container.DataItem,"province" %>
                                    
</ ItemTemplate >
                                    
< EditItemTemplate >
                                        
< asp:DropDownList  id ="province"  Width ="100%"  Runat ="server"  AutoPostBack ="True"  OnSelectedIndexChanged ="ddlprovince_SelectedIndexChanged" ></ asp:DropDownList >
                                    
</ EditItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn  SortExpression ="city"  HeaderText ="县市" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
<% # DataBinder.Eval(Container.DataItem,"city" %>
                                    
</ ItemTemplate >
                                    
< EditItemTemplate >
                                        
< asp:DropDownList  id ="city"  Runat ="server"  Width ="100%"  AutoPostBack ="True"  OnSelectedIndexChanged ="ddlcity_SelectedIndexChanged" ></ asp:DropDownList >
                                    
</ EditItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn  SortExpression ="area"  HeaderText ="乡镇" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
<% # DataBinder.Eval(Container.DataItem,"area" %>
                                    
</ ItemTemplate >
                                    
< EditItemTemplate >
                                        
< asp:DropDownList  id ="area"  Runat ="server"  Width ="100%" ></ asp:DropDownList >
                                    
</ EditItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:TemplateColumn  SortExpression ="Enabled"  HeaderText ="可见否" >
                                    
< HeaderStyle  Width ="70px" ></ HeaderStyle >
                                    
< ItemTemplate >
                                        
<% # DataBinder.Eval(Container.DataItem,"Enabled" %>
                                    
</ ItemTemplate >
                                    
< EditItemTemplate >
                                        
< asp:CheckBox  id =chkenabled  Runat ="server"  Checked ='<%#  DataBinder.Eval(Container.DataItem,"Enabled") % > '>
                                        
</ asp:CheckBox >
                                    
</ EditItemTemplate >
                                
</ asp:TemplateColumn >
                                
< asp:EditCommandColumn  ButtonType ="PushButton"  UpdateText ="更新"  HeaderText ="编辑"  CancelText ="取消"  EditText ="编辑" ></ asp:EditCommandColumn >
                                
< asp:ButtonColumn  Text ="删除"  ButtonType ="PushButton"  HeaderText ="删除"  CommandName ="Delete" ></ asp:ButtonColumn >
                            
</ Columns >
                            
< PagerStyle  NextPageText ="下一页"  PrevPageText ="上一页"  HorizontalAlign ="Right"  ForeColor ="DarkSlateBlue"
                                BackColor
="PaleGoldenrod"  Mode ="NumericPages" ></ PagerStyle >
                        
</ asp:datagrid ></ TD >
                
</ TR >
                
< tr  height ="30" >
                    
< td ></ td >
                
</ tr >
            
</ TABLE >
            
< INPUT  id ="hiduserid"  type ="hidden"  runat ="server" >
        
</ form >
    
</ body >
</ HTML >
2.cs代码
using  System.Data.SqlClient;
using  System.Text;

public   class  DatagridTest : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
protected System.Web.UI.HtmlControls.HtmlInputHidden hiduserid;
        
protected System.Web.UI.WebControls.Button btndelete;
        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
        
protected System.Web.UI.WebControls.DropDownList DropDownList2;
        
protected System.Web.UI.WebControls.DropDownList DropDownList3;
        
protected System.Web.UI.WebControls.CheckBox CheckBox1;
        
protected System.Web.UI.WebControls.TextBox TextBox1;
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.Button Button2;

        
private string constring="";        
        
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                ListProvince();
                ListCity();
                ListArea();

                btndelete.Attributes.Add(
"onclick","return Delete();");
                SortExpression
="";
                sort
="desc";
                DataBind();
            }
            
        }



        
DataBind

        
Web Form Designer generated code

        
GetDataSet
        
        
DataGrid1_ItemDataBound

        
DataGrid1_SortCommand

        
DataGrid1_ItemCreated

        
property

        
Edit

        
Cancel

        
Update

        
Delete event

        
ddlprovince_SelectedIndexChanged

        
ddlcity_SelectedIndexChanged

        
btndelete_Click

        
delete

        
ListProvince

        
ListCity

        
ListArea

        
save

        
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            ListCity();
        }


        
private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            ListArea();
        }


        
private void Button2_Click(object sender, System.EventArgs e)
        
{
            ToExcel(
this.DataGrid1,"meng");
        }


        
ToExcel
        
        
private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        
{
            
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
            DataBind();
        }

    }

3. 数据库教本(或下载真实数据 /Files/singlepine/area.rar)
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[TestGrid] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ TestGrid ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[province] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ province ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[city] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ city ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[area] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ area ]
GO

CREATE   TABLE   [ dbo ] . [ TestGrid ]  (
    
[ UserID ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
    
[ UserName ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ provinceID ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ cityID ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ areaID ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ Enabled ]   [ bit ]   NULL  
ON   [ PRIMARY ]
GO

CREATE   TABLE   [ dbo ] . [ province ]  (
    
[ id ]   [ int ]   NOT   NULL  ,
    
[ provinceID ]   [ nvarchar ]  ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ province ]   [ nvarchar ]  ( 40 ) COLLATE Chinese_PRC_CI_AS  NULL  
ON   [ PRIMARY ]
GO

CREATE   TABLE   [ dbo ] . [ city ]  (
    
[ id ]   [ int ]   NOT   NULL  ,
    
[ cityID ]   [ nvarchar ]  ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ city ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ father ]   [ nvarchar ]  ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL  
ON   [ PRIMARY ]
GO

CREATE   TABLE   [ dbo ] . [ area ]  (
    
[ id ]   [ int ]   NOT   NULL  ,
    
[ areaID ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ area ]   [ nvarchar ]  ( 60 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ father ]   [ nvarchar ]  ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL  
ON   [ PRIMARY ]
GO
4.源代码下载 /Files/singlepine/datagrid.rar
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值