DataGrid 完全攻略之五(使用DropDownList)

前台代码:html
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="UseDropDown.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.UseDropDown"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > DataGrid使用举例 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio 7.0" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
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" >
None.gif                    
< SelectedItemStyle  ForeColor ="GhostWhite"  BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
None.gif                    
< AlternatingItemStyle  BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
None.gif                    
< HeaderStyle  Font-Bold ="True"  BackColor ="Tan" ></ HeaderStyle >
None.gif                    
< FooterStyle  BackColor ="Tan" ></ FooterStyle >
None.gif                    
< Columns >
None.gif                        
< asp:BoundColumn  DataField ="StudentID"  ReadOnly ="True"  HeaderText ="学生ID" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentName"  HeaderText ="学生姓名" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentPass"  HeaderText ="密码" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Sex"  HeaderText ="性别" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Birthday"  HeaderText ="生日"  DataFormatString ="{0:yyyy-M-d}" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Email"  HeaderText ="邮件地址" ></ asp:BoundColumn >
None.gif                        
< asp:TemplateColumn  HeaderText ="性别模板列" >
None.gif                            
< ItemTemplate >
None.gif                                
< asp:DropDownList  id ="ddlSexI"  runat ="server"  Enabled ="False" >
None.gif                                    
< asp:ListItem  Value ="1" > </ asp:ListItem >
None.gif                                    
< asp:ListItem  Value ="0" > </ asp:ListItem >
None.gif                                
</ asp:DropDownList >
None.gif                            
</ ItemTemplate >
None.gif                            
< EditItemTemplate >
None.gif                                
< asp:DropDownList  id ="ddlSexE"  runat ="server" >
None.gif                                    
< asp:ListItem  Value ="1" > </ asp:ListItem >
None.gif                                    
< asp:ListItem  Value ="0" > </ asp:ListItem >
None.gif                                
</ asp:DropDownList >
None.gif                            
</ EditItemTemplate >
None.gif                        
</ asp:TemplateColumn >
None.gif                        
< asp:ButtonColumn  Text ="选择"  HeaderText ="选择"  CommandName ="Select" ></ asp:ButtonColumn >
None.gif                        
< asp:EditCommandColumn  ButtonType ="LinkButton"  UpdateText ="更新"  HeaderText ="操作"  CancelText ="取消"  EditText ="编辑" ></ asp:EditCommandColumn >
None.gif                        
< asp:ButtonColumn  Text ="删除"  HeaderText ="删除"  CommandName ="Delete" ></ asp:ButtonColumn >
None.gif                    
</ Columns >
None.gif                    
< PagerStyle  HorizontalAlign ="Center"  ForeColor ="DarkSlateBlue"  BackColor ="PaleGoldenrod" ></ PagerStyle >
None.gif                
</ asp:DataGrid ></ FONT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >

后台代码:cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  MsDataGrid
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class UseDropDown : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgShow;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
InBlock.gif                BindData();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void BindData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
InBlock.gif            SqlConnection con 
= new SqlConnection(strCon);
InBlock.gif            SqlDataAdapter da 
= new SqlDataAdapter("Select * from tbStudentinfo",con);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            da.Fill(ds,
"studentinfo");
InBlock.gif            dgShow.DataSource 
= ds.Tables["studentinfo"].DefaultView;
InBlock.gif            dgShow.DataBind();
InBlock.gif            
foreach(DataGridItem dgi in dgShow.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//以下绑定非编辑状态下拉列表 
InBlock.gif
                DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
InBlock.gif                
if(ddI!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
InBlock.gif                    
if(bSex)
InBlock.gif                        ddI.SelectedIndex 
= 0;
InBlock.gif                    
else
InBlock.gif                        ddI.SelectedIndex 
= 1;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//以下绑定编辑状态下拉列表 
InBlock.gif
                DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
InBlock.gif                
if(ddE!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
InBlock.gif                    
if(bSex)
InBlock.gif                        ddE.SelectedIndex 
= 0;
InBlock.gif                    
else
InBlock.gif                        ddE.SelectedIndex 
= 1;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
InBlock.gif            
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
InBlock.gif            
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
InBlock.gif            
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
InBlock.gif            
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dgShow.EditItemIndex 
= e.Item.ItemIndex;
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dgShow.EditItemIndex 
= -1;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dgShow.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(dgShow.Items.Count==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dgShow.CurrentPageIndex!=0)
InBlock.gif                    dgShow.CurrentPageIndex 
= dgShow.CurrentPageIndex-1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
InBlock.gif            ExecuteSql(strSql);
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/
InBlock.gif        //说明:执行制定SQL语句/
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**////
InBlock.gif        private void ExecuteSql(string strSql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
InBlock.gif
                SqlConnection conn =new SqlConnection(strconn);
InBlock.gif                SqlCommand com 
= new SqlCommand(strSql,conn);
InBlock.gif                conn.Open();
InBlock.gif                com.ExecuteNonQuery();
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
InBlock.gif                            
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
InBlock.gif
            string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
InBlock.gif
            string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
InBlock.gif            
string strSex = ((DropDownList)(e.Item.FindControl("ddlSexE"))).SelectedItem.Value;
InBlock.gif            
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
InBlock.gif            
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
InBlock.gif            
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
InBlock.gif            strSql 
+=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
InBlock.gif            ExecuteSql(strSql);
InBlock.gif            dgShow.EditItemIndex 
= -1;
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值