功能齐全的DataGrid

http://singlepine.cnblogs.com/articles/266538.html

转自小山的博客


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

posted on 2005-11-01 17:54 小山 阅读(5758) 评论(34)   编辑  收藏 收藏至365Key 所属分类: DataGrid


# re: 功能齐全的DataGrid 2005-11-19 10:30 157686528

Feedback

调试的时候出现错误了~!
<INPUT id=radio onclick=fun_option(this) type=radio value='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' name=radio>
这里!!!
能告诉我怎么回事吗?
QQ:157686528   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 11:12 rere
我想问问这个数据库怎么设置的。我一直调试不成功。
谢谢!
我的msn:julianpeng@hotmail.com

“/exercise”应用程序中的服务器错误。
--------------------------------------------------------------------------------

ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。

源错误:


行 85: SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
行 86: DataSet ds=new DataSet();
行 87: sda.Fill(ds);
行 88: return ds;
行 89: }


源文件: c:/inetpub/wwwroot/exercise/webform1.aspx.cs 行: 87
   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 12:34 小山
constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

是从web.config中读取的,在web.config配置就可以了,如下
<appSettings>
<add key="ConnectionString" value="data source=Meng;initial catalog=Document;user id=sa;password=sa;packet size=4096 ;"></add>
</appSettings>

需要修改成你自己的数据库连接   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 16:36 rere
数据库的问题解决了。
可是现在“编辑”按钮不能正常操作,
一直显示
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 140: ddlcity.DataValueField="cityID";
行 141: ddlcity.DataBind();
行 142: ddlcity.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"cityID"))).Selected=true;
行 143: }
行 144: }


   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 17:27 小山
ddlcity.DataValueField="cityID";
这个中的cityID要和你数据库字段相对应,我这里只是测试数据库,你要修改成实际的数据库字段   回复
  

#  re: 功能齐全的DataGrid 2005-12-12 10:14 Shina
ddlpovince_SelectedIndexChanged函数的意思能讲解一下吗?

如果我不需要联动下拉菜单,只需要一个下拉菜单显示数据库的内容还需要用这个汉书吗?   回复
  

#  re: 功能齐全的DataGrid 2005-12-12 13:33 小山
ddlpovince_SelectedIndexChanged是用来联动的事件,如果不需要联动,把他删掉就可以了   回复
  

#  re: 功能齐全的DataGrid 2005-12-14 15:54 Shina
如果制作三联动,应该做那些修改,请赐教!   回复
  

#  re: 功能齐全的DataGrid 2005-12-14 21:10 小山
加了三联动及保存功能   回复
  

#  re: 功能齐全的DataGrid 2005-12-15 08:49 tanke
加个分页功能啊,这点最重要了   回复
  

#  re: 功能齐全的DataGrid 2005-12-15 08:58 小山
分页功能参照
http://singlepine.cnblogs.com/articles/281425.html
帮上去就可以   回复
  

#  re: 功能齐全的DataGrid 2005-12-16 15:39 cnjack
请教:
当光标在行中移动时,在某一行上单击时(不是选中单选钮或CheckBox),如何使DropDownList1、DropDownList2和DropDownList3获得选择行相应的值呀?
或者:
在某一行上单击时,如何激活一后台事件(不是脚本)   回复
  

#  re: 功能齐全的DataGrid 2005-12-16 16:14 Shina
小山老师我做了一个三联动,但是现在出了一个问题就是选择一级栏目的内容后只对二级栏目起作用,比如我依次选择一级栏目、二级栏目、三级栏目后如果我再次选择一个一级栏目,三级栏目的内容还是原来的三级栏目内容,不更新,我不知道应该怎么修改,小山老师能指点指点吗?
我的asp源码如下:
<asp:dropdownlist id="DropDownListclass1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ljclass2"></asp:dropdownlist><asp:dropdownlist id="Dropdownlistclass2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ljclass3">
<asp:ListItem Value="" Selected="True">请选择二级栏目</asp:ListItem>
</asp:dropdownlist><asp:dropdownlist id="Dropdownlistclass3" runat="server">
<asp:ListItem Value="" Selected="True">请选择三级栏目</asp:ListItem>
</asp:dropdownlist>

asp.cs源码:
#region ljclass2
public void ljclass2(object sender, EventArgs e)
{
DropDownList ddlclass1 = sender as DropDownList;
if(ddlclass1.SelectedIndex!=-1)
{
DropDownList ddl=((Control)(((Control)sender)).Parent).FindControl("Dropdownlistclass2") as DropDownList;
string sqlcity="select * from jq_class2 where class1_name='"+ddlclass1.SelectedValue+"'";
DataSet dsclass2=GetDataSet(sqlcity);
ddl.DataSource=dsclass2;
ddl.DataTextField="class2_name";
ddl.DataValueField="class2_name";
ddl.DataBind();
ddl.Items.Insert(0,"请选择二级栏目");
ddl.Items.FindByText("请选择二级栏目").Selected=true;
}
}
#endregion
#region ljclass3
public void ljclass3(object sender, EventArgs e)
{
DropDownList ddlclass2 = sender as DropDownList;
if(ddlclass2.SelectedIndex!=-1)
{
DropDownList ddl3=((Control)(((Control)sender)).Parent).FindControl("Dropdownlistclass3") as DropDownList;
string sqlcity3="select * from jq_class3 where class1_name='"+DropDownListclass1.SelectedValue+"' and class2_name='"+ddlclass2.SelectedValue+"'";
DataSet dsclass3=GetDataSet(sqlcity3);
ddl3.DataSource=dsclass3;
ddl3.DataTextField="class3_name";
ddl3.DataValueField="class3_name";
ddl3.DataBind();
}
}
#endregion   回复
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
posted on 2005-11-01 17:54 小山 阅读(5758) 评论(34)   编辑  收藏 收藏至365Key 所属分类: DataGrid


#  re: 功能齐全的DataGrid 2005-12-07 11:12 rere
我想问问这个数据库怎么设置的。我一直调试不成功。
谢谢!
我的msn:julianpeng@hotmail.com

“/exercise”应用程序中的服务器错误。
--------------------------------------------------------------------------------

ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。

源错误:


行 85: SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
行 86: DataSet ds=new DataSet();
行 87: sda.Fill(ds);
行 88: return ds;
行 89: }


源文件: c:/inetpub/wwwroot/exercise/webform1.aspx.cs 行: 87
   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 12:34 小山
constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

是从web.config中读取的,在web.config配置就可以了,如下
<appSettings>
<add key="ConnectionString" value="data source=Meng;initial catalog=Document;user id=sa;password=sa;packet size=4096 ;"></add>
</appSettings>

需要修改成你自己的数据库连接   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 16:36 rere
数据库的问题解决了。
可是现在“编辑”按钮不能正常操作,
一直显示
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 140: ddlcity.DataValueField="cityID";
行 141: ddlcity.DataBind();
行 142: ddlcity.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"cityID"))).Selected=true;
行 143: }
行 144: }


   回复
  

#  re: 功能齐全的DataGrid 2005-12-07 17:27 小山
ddlcity.DataValueField="cityID";
这个中的cityID要和你数据库字段相对应,我这里只是测试数据库,你要修改成实际的数据库字段   回复
  

#  re: 功能齐全的DataGrid 2005-12-12 10:14 Shina
ddlpovince_SelectedIndexChanged函数的意思能讲解一下吗?

如果我不需要联动下拉菜单,只需要一个下拉菜单显示数据库的内容还需要用这个汉书吗?   回复
  

#  re: 功能齐全的DataGrid 2005-12-12 13:33 小山
ddlpovince_SelectedIndexChanged是用来联动的事件,如果不需要联动,把他删掉就可以了   回复
  

#  re: 功能齐全的DataGrid 2005-12-14 15:54 Shina
如果制作三联动,应该做那些修改,请赐教!   回复
  

#  re: 功能齐全的DataGrid 2005-12-14 21:10 小山
加了三联动及保存功能   回复
  

#  re: 功能齐全的DataGrid 2005-12-15 08:49 tanke
加个分页功能啊,这点最重要了   回复
  

#  re: 功能齐全的DataGrid 2005-12-15 08:58 小山
分页功能参照
http://singlepine.cnblogs.com/articles/281425.html
帮上去就可以   回复
  

#  re: 功能齐全的DataGrid 2005-12-16 15:39 cnjack
请教:
当光标在行中移动时,在某一行上单击时(不是选中单选钮或CheckBox),如何使DropDownList1、DropDownList2和DropDownList3获得选择行相应的值呀?
或者:
在某一行上单击时,如何激活一后台事件(不是脚本)   回复
  

#  re: 功能齐全的DataGrid 2005-12-16 16:14 Shina
小山老师我做了一个三联动,但是现在出了一个问题就是选择一级栏目的内容后只对二级栏目起作用,比如我依次选择一级栏目、二级栏目、三级栏目后如果我再次选择一个一级栏目,三级栏目的内容还是原来的三级栏目内容,不更新,我不知道应该怎么修改,小山老师能指点指点吗?
我的asp源码如下:
<asp:dropdownlist id="DropDownListclass1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ljclass2"></asp:dropdownlist><asp:dropdownlist id="Dropdownlistclass2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ljclass3">
<asp:ListItem Value="" Selected="True">请选择二级栏目</asp:ListItem>
</asp:dropdownlist><asp:dropdownlist id="Dropdownlistclass3" runat="server">
<asp:ListItem Value="" Selected="True">请选择三级栏目</asp:ListItem>
</asp:dropdownlist>

asp.cs源码:
#region ljclass2
public void ljclass2(object sender, EventArgs e)
{
DropDownList ddlclass1 = sender as DropDownList;
if(ddlclass1.SelectedIndex!=-1)
{
DropDownList ddl=((Control)(((Control)sender)).Parent).FindControl("Dropdownlistclass2") as DropDownList;
string sqlcity="select * from jq_class2 where class1_name='"+ddlclass1.SelectedValue+"'";
DataSet dsclass2=GetDataSet(sqlcity);
ddl.DataSource=dsclass2;
ddl.DataTextField="class2_name";
ddl.DataValueField="class2_name";
ddl.DataBind();
ddl.Items.Insert(0,"请选择二级栏目");
ddl.Items.FindByText("请选择二级栏目").Selected=true;
}
}
#endregion
#region ljclass3
public void ljclass3(object sender, EventArgs e)
{
DropDownList ddlclass2 = sender as DropDownList;
if(ddlclass2.SelectedIndex!=-1)
{
DropDownList ddl3=((Control)(((Control)sender)).Parent).FindControl("Dropdownlistclass3") as DropDownList;
string sqlcity3="select * from jq_class3 where class1_name='"+DropDownListclass1.SelectedValue+"' and class2_name='"+ddlclass2.SelectedValue+"'";
DataSet dsclass3=GetDataSet(sqlcity3);
ddl3.DataSource=dsclass3;
ddl3.DataTextField="class3_name";
ddl3.DataValueField="class3_name";
ddl3.DataBind();
}
}
#endregion   回复
#  re: 功能齐全的DataGrid 2005-11-19 10:30 157686528
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值