功能齐全的DataGrid

来源:互联网 发布:毕业论文软件题目简单 编辑:程序博客网 时间:2024/04/29 20:36
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.all["chkExport"];
                
if(chkother.length>0)
                
{
                    
for (var i=0;i<chkother.length;i++)
                    
{
                        
if(chkall.checked==true)
                        
{
                            chkother[i].checked
=true;
                        }

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

                    }

                }

            }

            
            
function Delete()
            
{
                
debugger;
                
var chkother= document.all["chkExport"];
                
var temp="";
                
if(chkother.length>0)
                
{
                    
for (var i=0;i<chkother.length;i++)
                    
{
                        
if(chkother[i].checked=true)
                        
{
                            temp
+=","+chkother[i].value;
                        }
                        
                    }

                    temp
=temp.substring(1,temp.length);
                }

                
if (temp == "")
                
{
                    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?"))
                
{
                    document.Form1.hiduserid.value
=temp;
                    
var deleteID='<%=btndelete.ClientID%>';
                    window.document.all(deleteID).click();
                }

                event.cancelBubble
=true;                
            }

        
</script>
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<TABLE id="Table1" height="100%" cellSpacing="1" cellPadding="1" width="100%" border="1">
                
<tr height="30">
                    
<td><INPUT id="chkAll" onclick="chkAll_true()" type="checkbox">全选/取消</td>
                    
<td><asp:button id="btndelete" runat="server" Text="删除"></asp:button></td>
                    
<td></td>
                    
<td></td>
                    
<td></td>
                
</tr>
                
<TR vAlign="top">
                    
<TD width="100%" colSpan="5"><asp:datagrid id="DataGrid1" runat="server" OnEditCommand="Edit" OnCancelCommand="Cancel" OnUpdateCommand="Update"
                            DataKeyField
="UserID" AutoGenerateColumns="False" AllowSorting="True" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow"
                            CellPadding
="2" GridLines="None" ForeColor="Black" OnDeleteCommand="Delete" 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="序号">
                                    
<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="序号">
                                    
<FooterTemplate>
                                    
</FooterTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn>
                                    
<ItemTemplate>
                                        
<INPUT id="chkExport" type="checkbox" value='<%# DataBinder.Eval(Container.DataItem,"UserID") %>'>
                                    
</ItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:BoundColumn Visible="False" DataField="UserID" SortExpression="UserID" ReadOnly="True" HeaderText="UserID"></asp:BoundColumn>
                                
<asp:TemplateColumn SortExpression="UserName" HeaderText="姓名">
                                    
<ItemTemplate>
                                        
<href="http://singlepine.cnblogs.com/articles/266538.html" target="_blank">
                                            
<%# DataBinder.Eval(Container.DataItem,"UserName"%>
                                        
</a>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:TextBox id=UserName Text='<%# DataBinder.Eval(Container.DataItem,"UserName") %>' Width="88px" Runat="server">
                                        
</asp:TextBox>
                                    
</EditItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn SortExpression="province" HeaderText="省市">
                                    
<ItemTemplate>
                                        
<%# DataBinder.Eval(Container.DataItem,"province"%>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:DropDownList id="province" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlpovince_SelectedIndexChanged"></asp:DropDownList>
                                    
</EditItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn SortExpression="city" HeaderText="县市">
                                    
<ItemTemplate>
                                        
<%# DataBinder.Eval(Container.DataItem,"city"%>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:DropDownList id="city" Runat="server"></asp:DropDownList>
                                    
</EditItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn SortExpression="Enabled" HeaderText="可见否">
                                    
<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"></PagerStyle>
                        
</asp:datagrid></TD>
                
</TR>
                
<tr height="30">
                    
<td></td>
                
</tr>
            
</TABLE>
            
<INPUT id="hiduserid" type="hidden" runat="server">
        
</form>
    
</body>
</HTML>

2.cs代码
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;
        
private string constring="";
        
        
//目前功能:排序、单选项、复选项、合并单元格、产生序列号、联动下拉框、分页、删除、超连接、鼠标移动改变颜色、奇偶项颜色区分
        private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                btndelete.Attributes.Add(
"onclick","Delete();");
                SortExpression
="";
                sort
="desc";
                DataBind();
            }
            
        }



        
DataBind

        
Web Form Designer generated code

        
GetDataSet
        
        
DataGrid1_ItemDataBound

        
DataGrid1_SortCommand

        
DataGrid1_ItemCreated

        
property

        
Edit

        
Cancel

        
Update

        
Delete event

        
ddlpovince_SelectedIndexChanged

        
btndelete_Click

        
delete

        
DataGrid1_PageIndexChanged        
    }

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].[city]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
drop table [dbo].[city]
GO

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

CREATE TABLE [dbo].[TestGrid] (
    
[UserID] [int] NOT NULL ,
    
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[State] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[Enabled] [bit] 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].[povince] (
    
[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
原创粉丝点击