XuGang

记录一个程序员的成长

 

VS 2003中对DataGrid的增、删、改

该页面引用了一个简单的CSS,放在同级目录中的css文件夹内,文件为:css.css

.style_1 {
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: bold;
    color
: #FFFFFF;
    text-decoration
: none;
}
.style_2 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-weight
: normal;
    color
: #333333;
}
.style_3 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: normal;
    color
: #333333;
}
.style_4 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: normal;
    color
: #FFFFFF;
}
.style_5 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: normal;
    color
: #333333;
}
.style6 
{
    font-family
: "宋体";
    font-size
: 12px;
    color
: #005BA7;
    text-decoration
: none;
}
.style_7 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-weight
: normal;
    color
: #FFFFFF;
}
.style_8 
{
    font-family
: "宋体";
    color
: #FF0000;
    text-decoration
: none;
    font-size
: 14px;
}
.style_9 
{
    font-family
: "宋体";
    font-size
: 18px;
    color
: #FFFFFF;
}
.style_10 
{
    font-family
: "宋体";
    font-size
: 24px;
    font-weight
: bold;
    color
: #FFFFFF;
}
.tizi_1 
{
    font-family
: "宋体";
    font-size
: 12px;
    line-height
: normal;
    color
: #3A3A3A;
}
.input 
{
    background-color
: #FFFFFF;
    margin
: 1px;
    padding
: 1px;
    border
: 1px solid #686868;
}
.ziti_2 
{
    font-family
: "宋体";
    font-size
: 12px;
    color
: #FF4800;
}
.tizi_3 
{
    font-family
: "宋体";
    font-size
: 12px;
    color
: #003C72;
}
.ks_1 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-weight
: bold;
    color
: #0154A0;
}
.ks_2 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-weight
: bold;
    color
: #000000;
}
.ks_3 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-weight
: bold;
    color
: #FF0000;
}
.ks_4 
{
    font-family
: "宋体";
    font-size
: 16px;
    font-weight
: bold;
    font-variant
: normal;
    color
: #FFFFFF;
}
.ks_5 
{
    font-family
: "宋体";
    font-size
: 16px;
    font-weight
: bold;
    color
: #FFFF00;
}
.ks_6 
{
    font-family
: "宋体";
    font-size
: 16px;
    font-weight
: normal;
    color
: #FEEED7;
}
.ks_7 
{
    font-family
: "宋体";
    font-size
: 24px;
    color
: #0067BD;
}
.ks_8 
{
    font-family
: "宋体";
    font-size
: 14px;
    font-style
: normal;
    line-height
: 20px;
    color
: #000000;
}
.ks_9 
{
    font-family
: "宋体";
    font-size
: 36px;
    color
: #FFFFFF;
}
.xtdl_1 
{
    font-family
: "宋体";
    font-size
: 12px;
    color
: #000000;
}
.xtdl_2 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: bold;
    color
: #0024FF;
}
.xtdl_3 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-weight
: bold;
    color
: #FF4701;
}
.jt-9 
{
    font-size
: 12px;
    line-height
: 20px;
    color
: #000000;
}
.input 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-style
: normal;
    font-weight
: normal;
    margin
: 1px;
    padding
: 1px;
    border
: 1px solid #336699;
    background-color
: #EFFAFE;
    list-style-type
: disc;
}
.ziti 
{
    font-family
: "宋体";
    font-size
: 12px;
    font-style
: normal;
    line-height
: normal;
    font-weight
: normal;
    color
: #000000;
    text-decoration
: none;
}
.contentpanel
{text-align:left;margin:0 auto!important; margin:0px;}
.Ptitle
{height:18px;text-align:left;margin:0px 9px 0px 9px;padding:3px 0px 0px 13px;font-weight:bold;font-size:12px;font-family:Arial;color:#000;border:1px solid #FF8C00;border-left: 5px solid #FF8C00;}  
.Pcontent
{text-align:left;padding-left:15px;padding-top:3px;font-size: 12px;}  
.Pfoot
{margin:10px 20px 0px 0px;text-align:right;font-size: 12px;a:link,a:visited{text-decoration:none;color: #FF8C00;}


DWBMset.aspx

<%@ Page language="c#" Codebehind="DWBMset.aspx.cs" AutoEventWireup="false" Inherits="FineYYWeb.Competence.DWBMset" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>DWBMset</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">
        
<LINK href="css/css.css" type="text/css" rel="Stylesheet">
    
</HEAD>
    
<body topMargin="0" MS_POSITIONING="GridLayout">
        
<form id="Form1" style="TEXT-ALIGN: center" method="post" runat="server">
            
<TABLE class="style_3" id="Table1" style="HEIGHT: 253px" cellSpacing="0" cellPadding="0"
                width
="300" border="0">
                
<TR>
                    
<TD class="style_3" style="BACKGROUND-IMAGE: url(../images/you_zlbg.gif); HEIGHT: 36px; colSpan: "
                        colSpan
="2">
                        
<align="center"><font color="white"><b>精细化管理考核用户注册</b></font></P>
                    
</TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; TEXT-ALIGN: right"><FONT face="宋体">辖区编码</FONT></TD>
                    
<TD><FONT face="宋体">&nbsp; </FONT><INPUT class="jt-9" id="user_Text" style="WIDTH: 133px; HEIGHT: 21px" type="text" name="Text1"
                            runat
="server"></TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; TEXT-ALIGN: right"><FONT face="宋体">单位名称</FONT></TD>
                    
<TD><FONT face="宋体">&nbsp; </FONT><INPUT class="jt-9" id="name_Text" style="WIDTH: 133px; HEIGHT: 21px" type="text" name="Text1"
                            runat
="server"></TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; TEXT-ALIGN: right"><FONT face="宋体">单位部门编码</FONT></TD>
                    
<TD><FONT face="宋体">&nbsp; </FONT><INPUT class="jt-9" id="number_Text" style="WIDTH: 133px; HEIGHT: 21px" type="text" name="Text2"
                            runat
="server"></TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; HEIGHT: 25px; TEXT-ALIGN: right">
                        
<P><FONT face="宋体">部门名称</FONT></P>
                    
</TD>
                    
<TD style="HEIGHT: 25px"><FONT face="宋体">&nbsp;&nbsp;<INPUT id="Password1" style="WIDTH: 133px; HEIGHT: 22px" type="text" size="16" name="Text1"
                                runat
="server"> </FONT>
                    
</TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; HEIGHT: 25px; TEXT-ALIGN: right">
                        
<P><FONT face="宋体">机关代码</FONT></P>
                    
</TD>
                    
<TD style="HEIGHT: 25px"><FONT face="宋体">&nbsp;&nbsp;<INPUT id="JGDM_Text" style="WIDTH: 133px; HEIGHT: 22px" type="text" size="16" name="Text1"
                                runat
="server"> </FONT>
                    
</TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; TEXT-ALIGN: right"><FONT face="宋体">单位中队编码</FONT></TD>
                    
<TD><FONT face="宋体">&nbsp; </FONT><INPUT class="jt-9" id="DWZDBM_Text" style="WIDTH: 133px; HEIGHT: 21px" type="text" name="Text6"
                            runat
="server"></TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px; TEXT-ALIGN: right"><FONT face="宋体">中队名称</FONT></TD>
                    
<TD><FONT face="宋体">&nbsp; </FONT><INPUT class="jt-9" id="auditNum_Text" style="WIDTH: 133px; HEIGHT: 21px" type="text" runat="server"></TD>
                
</TR>
                
<TR>
                    
<TD style="WIDTH: 149px"><FONT face="宋体"></FONT></TD>
                    
<TD><FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        
</FONT>
                        
<asp:button class="jt-9" id="submitButton" runat="server" Text="增 加"></asp:button></TD>
                
</TR>
                
<TR>
                    
<TD colSpan="2"><FONT face="宋体"><asp:datagrid id="myDataGrid" runat="server" Width="824px" AllowPaging="True" CssClass="jt-9"
                                AutoGenerateColumns
="False" DataKeyField="ID">
                                
<Columns>
                                    
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="确定" HeaderText="修改" CancelText="取消" EditText="修改"></asp:EditCommandColumn>
                                    
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
                                    
<asp:BoundColumn Visible="False" DataField="ID" HeaderText="ID"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="XQBM" HeaderText="辖区编码"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="DW" HeaderText="单位名称"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="DWBMBM" HeaderText="单位部门编码"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="BM" HeaderText="部门名称"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="JGDM" HeaderText="机关代码"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="DWZDBM" HeaderText="单位中队编码"></asp:BoundColumn>
                                    
<asp:BoundColumn DataField="ZD" HeaderText="中队名称"></asp:BoundColumn>
                                
</Columns>
                                
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
                            
</asp:datagrid></FONT></TD>
                
</TR>
            
</TABLE>
        
</form>
    
</body>
</HTML>

DWBMset.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace FineYYWeb.Competence
{
    
/// <summary>
    
/// DWBMset 的摘要说明。
    
/// </summary>
    public class DWBMset : System.Web.UI.Page
    {
    
        
public SqlConnection conn;
        
public SqlCommand comm;
        
public SqlDataAdapter dap;
        
public DataSet myDataSet;
        
protected System.Web.UI.HtmlControls.HtmlInputText user_Text;
        
protected System.Web.UI.HtmlControls.HtmlInputText number_Text;
        
protected System.Web.UI.HtmlControls.HtmlInputText audit_Text;
        
protected System.Web.UI.WebControls.Button submitButton;
        
protected System.Web.UI.HtmlControls.HtmlInputText name_Text;
        
protected System.Web.UI.HtmlControls.HtmlInputText auditNum_Text;
        
protected System.Web.UI.WebControls.DataGrid myDataGrid;
        
public string m_connString="user id=sa;password=5518008;initial catalog=jxhpt;data source=127.0.0.1;Connect Timeout=30";

        
string str_qxbm=null;
        
string loginName=null;
        
string str_UPName=null;
        
protected System.Web.UI.HtmlControls.HtmlInputText Password1;
        
protected System.Web.UI.HtmlControls.HtmlInputText DWZDBM_Text;
        
protected System.Web.UI.HtmlControls.HtmlInputText JGDM_Text;
        
string str_UPJH=null;

        
private void Page_Load(object sender, System.EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
            if(!IsPostBack)
            {
                
//myHashtable();  //判断是否登陆,并传递用户的相关信息
                Grid_Bind();  //myDataGrid数据源
            }
        }
        
public void myHashtable()  //登陆_数据源
        {
            
if(Session["ht_user_msg"]!=null)
            {            
                Hashtable myHT 
= null;
                myHT 
= (Hashtable)Session["ht_user_msg"];
                loginName 
= myHT["DLNAME"].ToString();  //用户名
                str_UPName = myHT["TRUENAME"].ToString();//用户_姓名
                str_UPJH = myHT["DWZDBM"].ToString();  //用户_单位中队编码
                str_qxbm=myHT["QXBM"].ToString();

                
if(str_qxbm!="002")
                { Response.Redirect(
"../Error.aspx?ID=2"); }
            }
            
else
            { Response.Redirect(
"../Error.aspx?ID=1"); }
        }

        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
            
this.myDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.myDataGrid_PageIndexChanged);
            
this.myDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_CancelCommand);
            
this.myDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_EditCommand);
            
this.myDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_UpdateCommand);
            
this.myDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_DeleteCommand);
            
this.myDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.myDataGrid_ItemDataBound);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
public void Grid_Bind()  //myDataGrid数据源
        { 
            
string sqlString="select * from SP_JJH_DICT_DWBM";
            dap
=new SqlDataAdapter(sqlString,m_connString);
            myDataSet
=new DataSet();
            dap.Fill(myDataSet);
            myDataGrid.DataSource
=myDataSet.Tables[0].DefaultView;
            myDataGrid.DataBind();            
        }

        
private void submitButton_Click(object sender, System.EventArgs e)
        {
            
string user = user_Text.Value.Trim();  //辖区编码
            string name = name_Text.Value.Trim();  //单位名称
            string number = number_Text.Value.Trim();  //单位部门编码
            string password = Password1.Value.Trim();  //Password1
            string strJGDM = JGDM_Text.Value.Trim();
            
string strDWZDBM = DWZDBM_Text.Value.Trim(); //单位中队编码
            string auditNum = auditNum_Text.Value.Trim();//中队名称

            
if(user=="" || name=="" || number=="" || password==""|| auditNum=="" ||strJGDM=="" ||strDWZDBM=="")
            {
                
this.RegisterStartupScript("js","<script>alert('请输入完整的信息!');</script>");
                
return;
            }
            
            
//查询 单位名称 是否存在
            string sqlString = "select XQBM from SP_JJH_DICT_DWBM where JGDM='"+strJGDM+"'";
            dap
=new SqlDataAdapter(sqlString,m_connString);
            myDataSet
=new DataSet();
            dap.Fill(myDataSet);

            
if (myDataSet.Tables[0].Rows.Count == 0//用户不存在
            {
                sqlString 
= "insert into SP_JJH_DICT_DWBM (XQBM,DW,DWBMBM,BM,JGDM,DWZDBM,ZD) ";
                sqlString 
+= " values('"+user+"','"+name+"','"+number+"','"+password+"','"+strJGDM+"','"+strDWZDBM+"','"+auditNum+"')";
                
//dap=new SqlDataAdapter(sqlString,m_connString);
                
//dap.InsertCommand();
                conn = new SqlConnection(m_connString);
                comm 
= new SqlCommand(sqlString,conn);
                
if(conn.State.ToString()=="Closed" )conn.Open();
                comm.ExecuteNonQuery();
                comm.Dispose();
                conn.Close();
                conn.Dispose();

                Grid_Bind();  
//myDataGrid数据源
            }
            
else
            {
                   
this.RegisterStartupScript("js","<script>alert('机关代码已存在!');</script>");
            }
        }

//        private void myDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
//        {
//            user_Text.Value = myDataGrid.SelectedItem.Cells[3].Text.Trim();//.Replace("&nbsp;","");   
//            name_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();//.Replace("&nbsp;","");    
//            number_Text.Value = myDataGrid.SelectedItem.Cells[5].Text.Trim();//.Replace("&nbsp;","");  
//            //loginNum_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();
//            Password1.Value = myDataGrid.SelectedItem.Cells[6].Text.Trim();
//            //departNum_Text.SelectedValue = myDataGrid.SelectedItem.Cells[8].Text.Trim();
//            audit_Text.Value = myDataGrid.SelectedItem.Cells[9].Text.Trim();
//            auditNum_Text.Value = myDataGrid.SelectedItem.Cells[10].Text.Trim();
//        }

        
private void myDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            myDataGrid.CurrentPageIndex
=e.NewPageIndex;
            Grid_Bind();
        }
        
//删除
        private void myDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            
string id=e.Item.Cells[2].Text;
            
string sql="delete from SP_JJH_DICT_DWBM where ID='"+id+"'";
            SqlConnection co
=new SqlConnection(m_connString);
            SqlCommand com
=new SqlCommand(sql,co);
            co.Open();
            com.ExecuteNonQuery();
            com.Dispose();
            co.Close();
            co.Dispose();
            
            
this.Grid_Bind();

        }
        
// 修改
        private void myDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            myDataGrid.EditItemIndex 
= e.Item.ItemIndex;
            Grid_Bind();
        }
        
//取消修改
        private void myDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            myDataGrid.EditItemIndex 
= -1;
            Grid_Bind();
        }
        
//更新
        private void myDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            
string id=myDataGrid.DataKeys[e.Item.ItemIndex].ToString();

            TextBox tbXQBM
=(TextBox)e.Item.Cells[3].Controls[0];
            
string strXQBM= tbXQBM.Text.Trim();

            TextBox tbDW
=(TextBox)e.Item.Cells[4].Controls[0];
            
string  strDW = tbDW.Text.Trim();

            TextBox tbDWBMBM
=(TextBox)e.Item.Cells[5].Controls[0];
            
string  strDWBMBM = tbDWBMBM.Text.Trim();

            TextBox tbBM
=(TextBox)e.Item.Cells[6].Controls[0];
            
string  strBM = tbBM.Text.Trim();

            TextBox tbJGDM
=(TextBox)e.Item.Cells[7].Controls[0];
            
string  strJGDM = tbJGDM.Text.Trim();
            
            TextBox tbDWZDBM
=(TextBox)e.Item.Cells[8].Controls[0];
            
string  strDWZDBM = tbDWZDBM.Text.Trim();

            TextBox tbZD
=(TextBox)e.Item.Cells[9].Controls[0];
            
string  strZD = tbZD.Text.Trim();

            
string MYsqlString = "update SP_JJH_DICT_DWBM set XQBM='"+strXQBM+"',DW='"+strDW+"',DWBMBM='"+strDWBMBM+"',BM='"+strBM+"',JGDM='"+strJGDM+"',DWZDBM='"+strDWZDBM+"',ZD='"+strZD+"' where ID='"+id+"'";
            conn 
= new SqlConnection(m_connString);
            comm 
= new SqlCommand(MYsqlString,conn);
            
if(conn.State.ToString()=="Closed" )conn.Open();
            comm.ExecuteNonQuery();
            comm.Dispose();
            conn.Close();
            conn.Dispose();

            myDataGrid.EditItemIndex 
= -1;
            Grid_Bind();
        }

        
//设置编辑模式的 TextBox 的宽度
        private void myDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            
if(e.Item.ItemType   ==   ListItemType.EditItem) 
            { 
                ((TextBox)e.Item.Cells[
3].Controls[0]).Width=90
                ((TextBox)e.Item.Cells[
4].Controls[0]).Width=170
                ((TextBox)e.Item.Cells[
5].Controls[0]).Width=90
                ((TextBox)e.Item.Cells[
6].Controls[0]).Width=90
                ((TextBox)e.Item.Cells[
7].Controls[0]).Width=90
                ((TextBox)e.Item.Cells[
8].Controls[0]).Width=90
                ((TextBox)e.Item.Cells[
9].Controls[0]).Width=90
            }   
        }
    }
}

 

posted on 2007-12-20 15:05  钢钢  阅读(1596)  评论(1编辑  收藏  举报

导航