博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGrid 完全攻略之七(实现选择、编辑和修改)
阅读量:7143 次
发布时间:2019-06-29

本文共 7088 字,大约阅读时间需要 23 分钟。

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.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class UserDelete : System.Web.UI.Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgShow;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
InBlock.gif                BindData();
InBlock.gif            
ExpandedSubBlockEnd.gif        }
InBlock.gif        
private void BindData()
ExpandedSubBlockStart.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();
ExpandedSubBlockEnd.gif        }
ContractedSubBlock.gif        
Web Form Designer generated code
InBlock.gif
InBlock.gif        
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.EditItemIndex 
= e.Item.ItemIndex;
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif        
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }
InBlock.gif        
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.EditItemIndex 
= -1;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(dgShow.Items.Count==1)
ExpandedSubBlockStart.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.gif        
InBlock.gif        //说明:执行制定SQL语句/
ExpandedSubBlockStart.gif
        ///
InBlock.gif        private void ExecuteSql(string strSql)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.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.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.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 = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
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        
private void dgShow_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
switch(e.Item.ItemType)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
case ListItemType.Item:
InBlock.gif                
case ListItemType.EditItem:
InBlock.gif                
case ListItemType.AlternatingItem:
InBlock.gif                    Button       myDeleteButton 
= (Button)e.Item.FindControl("btnDelete");
InBlock.gif                    myDeleteButton.Text 
= "删除此行";
InBlock.gif                    myDeleteButton.Attributes.Add(
"onclick""return confirm('您真的要删除第 " + e.Item.ItemIndex.ToString() + " 行吗?');");
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
private void dgShow_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(e.CommandName=="UserDelete")
InBlock.gif                dgShow_DeleteCommand(source,e);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
前台代码:html

ExpandedBlockStart.gif
<%
@ Page language="c#" Codebehind="UserDelete.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.UserDelete" 
%>
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: 69px; POSITION: absolute; TOP: 90px"
 runat
="server"
 Width
="842px"
 Height
="172px"
 BorderColor
="Tan"
 BorderWidth
="1px"
 BackColor
="LightGoldenrodYellow"
 CellPadding
="2"
 GridLines
="None"
 ForeColor
="Black"
 AutoGenerateColumns
="False"
 AllowPaging
="True"
>
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
="生日"
></
asp:BoundColumn
>
None.gif                        
<
asp:BoundColumn 
DataField
="Email"
 HeaderText
="邮件地址"
></
asp:BoundColumn
>
None.gif                        
<
asp:TemplateColumn 
HeaderText
="性别模板列"
>
None.gif                            
<
ItemTemplate
>
None.gif                                
<
asp:RadioButton 
id
=RadioButton2 
runat
="server"
 Text
="男"
 Checked
='<%# 
DataBinder.Eval(Container, "DataItem.Sex") %
>
' Enabled="False">
None.gif                                
</
asp:RadioButton
>
None.gif                                
<
asp:RadioButton 
id
=RadioButton1 
runat
="server"
 Text
="女"
 Checked
='<%# 
!(bool)DataBinder.Eval(Container, "DataItem.Sex") %
>
' Enabled="False">
None.gif                                
</
asp:RadioButton
>
None.gif                            
</
ItemTemplate
>
None.gif                            
<
EditItemTemplate
>
None.gif                                
<
asp:RadioButton 
id
=cbSex 
runat
="server"
 Text
="男"
 Checked
='<%# 
DataBinder.Eval(Container, "DataItem.Sex") %
>
' GroupName="Sex">
None.gif                                
</
asp:RadioButton
>
None.gif                                
<
asp:RadioButton 
id
=RadioButton4 
runat
="server"
 Text
="女"
 Checked
='<%# 
!(bool)DataBinder.Eval(Container, "DataItem.Sex") %
>
' GroupName="Sex">
None.gif                                
</
asp:RadioButton
>
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                        
<
asp:TemplateColumn 
HeaderText
="自定义删除"
>
None.gif                            
<
ItemTemplate
>
None.gif                                
<
asp:Button 
id
="btnDelete"
 runat
="server"
 CommandName
="UserDelete"
 Text
="删除"
></
asp:Button
>
None.gif                            
</
ItemTemplate
>
None.gif                        
</
asp:TemplateColumn
>
None.gif                        
<
asp:HyperLinkColumn 
Text
="点击查看"
 DataNavigateUrlField
="StudentID"
 DataNavigateUrlFormatString
="Show.aspx?ID={0}"
 DataTextField
="StudentName"
 HeaderText
="详细信息"
></
asp:HyperLinkColumn
>
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
>
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/12/253218.html,如需转载请自行联系原作者
你可能感兴趣的文章
[洛谷P2839][国家集训队]middle
查看>>
《求一个数组的连续的最大子数组之和》
查看>>
设置行间距,自适应文字大小
查看>>
资金流学习-广州发展
查看>>
python基础3(元祖、字典、深浅copy、集合、文件处理)
查看>>
正确编写Designated Initializer的几个原则
查看>>
iOS播放动态GIF图片
查看>>
获取版本号
查看>>
使用jdk自带的visualVM监控远程监控was
查看>>
集合视图UICollectionView 介绍及其示例程序
查看>>
JsLint 的安装和使用
查看>>
合并傻子//区间dp
查看>>
让IE和Chrome都以隐身模式启动
查看>>
MyPython-->进阶篇-->类
查看>>
unity remote 连接设置
查看>>
2018 NOIP备战计划
查看>>
教你如何迅速秒杀掉:99%的海量数据处理面试题
查看>>
Silverlight如何调用淘宝API
查看>>
ESP8266- AP模式的使用
查看>>
JBoss开发者框架: JBoss技术文档中心
查看>>