DataGrid使用举例

代码用的是Microsoft  WebCast上面的讲课代码!

前台代码如下:

 1 None.gif<%@ Page language="c#" Codebehind="AdjustWidth.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.AdjustWidth" %>
 2 None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3 None.gif<HTML>
 4 None.gif    <HEAD>
 5 None.gif        <title>DataGrid使用举例</title>
 6 None.gif        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
 7 None.gif        <meta name="CODE_LANGUAGE" Content="C#">
 8 None.gif        <meta name="vs_defaultClientScript" content="JavaScript">
 9 None.gif        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10 None.gif    </HEAD>
11 None.gif    <body MS_POSITIONING="GridLayout">
12 None.gif        <form id="Form1" method="post" runat="server">
13 None.gif            <FONT face="宋体">
14 None.gif                <asp:DataGrid id="dgShow" style="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px" runat="server" Width="842px" Height="172px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow" CellPadding="2" GridLines="None" ForeColor="Black" PageSize="1" AutoGenerateColumns="False">
15 None.gif                    <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
16 None.gif                    <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
17 None.gif                    <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
18 None.gif                    <FooterStyle BackColor="Tan"></FooterStyle>
19 None.gif                    <Columns>
20 None.gif                        <asp:BoundColumn DataField="StudentID" ReadOnly="True" HeaderText="学生ID"></asp:BoundColumn>
21 None.gif                        <asp:BoundColumn DataField="StudentName" HeaderText="学生姓名"></asp:BoundColumn>
22 None.gif                        <asp:BoundColumn DataField="StudentPass" HeaderText="密码"></asp:BoundColumn>
23 None.gif                        <asp:BoundColumn DataField="Sex" HeaderText="性别"></asp:BoundColumn>
24 None.gif                        <asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
25 None.gif                        <asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn>
26 None.gif                        <asp:TemplateColumn HeaderText="性别模板列">
27 None.gif                            <ItemTemplate>
28 None.gif                                <asp:RadioButton id=RadioButton2 runat="server" Text="男"  Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>' Enabled="False">
29 None.gif                                </asp:RadioButton>
30 None.gif                                <asp:RadioButton id=RadioButton1 runat="server" Text="女"  Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' Enabled="False">
31 None.gif                                </asp:RadioButton>
32 None.gif                            </ItemTemplate>
33 None.gif                            <EditItemTemplate>
34 None.gif                                <asp:RadioButton id=cbSex runat="server" Text="男"  Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>' GroupName="Sex">
35 None.gif                                </asp:RadioButton>
36 None.gif                                <asp:RadioButton id=RadioButton4 runat="server" Text="女"  Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' GroupName="Sex">
37 None.gif                                </asp:RadioButton>
38 None.gif                            </EditItemTemplate>
39 None.gif                        </asp:TemplateColumn>
40 None.gif                        <asp:ButtonColumn Text="选择" HeaderText="选择" CommandName="Select"></asp:ButtonColumn>
41 None.gif                        <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="操作" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
42 None.gif                        <asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
43 None.gif                        <asp:HyperLinkColumn Text="点击查看" DataNavigateUrlField="StudentID" DataNavigateUrlFormatString="Show.aspx?ID={0}" DataTextField="StudentName" HeaderText="详细信息"></asp:HyperLinkColumn>
44 None.gif                    </Columns>
45 None.gif                    <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
46 None.gif                </asp:DataGrid></FONT>
47 None.gif        </form>
48 None.gif    </body>
49 None.gif</HTML>
50 None.gif

我感觉加高亮的地方是这个DataGrid界面设计应该注意的地方。

后台核心代码如下:

  1 None.gif private   void  Page_Load( object  sender, System.EventArgs e)
  2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
  3InBlock.gif            // 在此处放置用户代码以初始化页面
  4InBlock.gif            if(!IsPostBack)
  5InBlock.gif                BindData();
  6InBlock.gif            
  7ExpandedBlockEnd.gif        }

  8 None.gif         private   void  BindData()
  9 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 10InBlock.gif            string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
 11InBlock.gif            SqlConnection con = new SqlConnection(strCon);
 12InBlock.gif            SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
 13InBlock.gif            DataSet ds = new DataSet();
 14InBlock.gif            da.Fill(ds,"studentinfo");
 15InBlock.gif            dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
 16InBlock.gif            dgShow.DataBind();
 17InBlock.gif            dgShow.Columns[0].Visible = false;            
 18InBlock.gif            
 19ExpandedBlockEnd.gif        }

 20 ContractedBlock.gifExpandedBlockStart.gif         Web Form Designer generated code #region Web Form Designer generated code
 21InBlock.gif        override protected void OnInit(EventArgs e)
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 23InBlock.gif            //
 24InBlock.gif            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
 25InBlock.gif            //
 26InBlock.gif            InitializeComponent();
 27InBlock.gif            base.OnInit(e);
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif        
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 31InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 32InBlock.gif        /// 此方法的内容。
 33ExpandedSubBlockEnd.gif        /// </summary>

 34InBlock.gif        private void InitializeComponent()
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 36InBlock.gif            this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
 37InBlock.gif            this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
 38InBlock.gif            this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
 39InBlock.gif            this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
 40InBlock.gif            this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
 41InBlock.gif            this.dgShow.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgShow_ItemDataBound);
 42InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 43InBlock.gif
 44ExpandedSubBlockEnd.gif        }

 45ExpandedBlockEnd.gif        #endregion

 46 None.gif
 47 None.gif         private   void  dgShow_EditCommand( object  source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 48 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 49InBlock.gif            dgShow.EditItemIndex = e.Item.ItemIndex;
 50InBlock.gif            BindData();
 51InBlock.gif
 52ExpandedBlockEnd.gif        }

 53 None.gif    
 54 None.gif         private   void  dgShow_CancelCommand( object  source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 55 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 56InBlock.gif            dgShow.EditItemIndex = -1;
 57InBlock.gif            BindData();
 58ExpandedBlockEnd.gif        }

 59 None.gif         private   void  dgShow_PageIndexChanged( object  source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
 60 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 61InBlock.gif            dgShow.CurrentPageIndex = e.NewPageIndex;
 62InBlock.gif            BindData();
 63ExpandedBlockEnd.gif        }

 64 None.gif         private   void  dgShow_DeleteCommand( object  source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 65 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 66InBlock.gif            if(dgShow.Items.Count==1)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif                if(dgShow.CurrentPageIndex!=0)
 69InBlock.gif                    dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
 70ExpandedSubBlockEnd.gif            }

 71InBlock.gif            string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
 72InBlock.gif            ExecuteSql(strSql);
 73InBlock.gif            BindData();
 74InBlock.gif
 75ExpandedBlockEnd.gif        }

 76 ExpandedBlockStart.gifContractedBlock.gif         /**/
 77 None.gif          // 说明:执行制定SQL语句 /
 78 ExpandedBlockStart.gifContractedBlock.gif          /**/ ///
 79 None.gif          private   void  ExecuteSql( string  strSql)
 80 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 81InBlock.gif            try
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
 84InBlock.gif                SqlConnection conn =new SqlConnection(strconn);
 85InBlock.gif                SqlCommand com = new SqlCommand(strSql,conn);
 86InBlock.gif                conn.Open();
 87InBlock.gif                com.ExecuteNonQuery();
 88InBlock.gif                conn.Close();
 89ExpandedSubBlockEnd.gif            }
 
 90InBlock.gif            catch(Exception e)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
 93InBlock.gif                            
 94ExpandedSubBlockEnd.gif            }

 95ExpandedBlockEnd.gif        }

 96 None.gif        
 97 None.gif         private   void  dgShow_UpdateCommand( object  source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 98 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 99InBlock.gif            string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
100InBlock.gif            string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
101InBlock.gif            string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
102InBlock.gif            string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
103InBlock.gif            string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
104InBlock.gif            string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
105InBlock.gif            string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
106InBlock.gif            strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
107InBlock.gif            ExecuteSql(strSql);
108InBlock.gif            dgShow.EditItemIndex = -1;
109InBlock.gif            BindData();
110InBlock.gif
111ExpandedBlockEnd.gif        }

112 None.gif
113 None.gif         private   void  dgShow_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
114 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
115InBlock.gif            if (e.Item.ItemType == ListItemType.EditItem) 
116ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
117InBlock.gif                for (int i=0;i<e.Item.Cells.Count;i++)
118ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
119InBlock.gif                    if(e.Item.Cells[i].Controls.Count>0)
120ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
121InBlock.gif                        try
122ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
123InBlock.gif                            TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
124InBlock.gif                            t.Width=130;
125ExpandedSubBlockEnd.gif                        }

126InBlock.gif                        catch(Exception ee)
127ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
128ExpandedSubBlockEnd.gif                        }

129ExpandedSubBlockEnd.gif                    }

130ExpandedSubBlockEnd.gif                }

131ExpandedSubBlockEnd.gif            }

132ExpandedBlockEnd.gif        }

有兴趣的朋友,我们可以讨论一下DataGrid方面的应用!
posted on 2006-06-29 16:52 Field 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/field/archive/2006/06/29/438849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值