GridView手工实现增删改(详细)

昨晚总结了一篇关于GridView中的事件的文章,末尾留了一点“下回分解”,有那么点点味道就对了。想想今天又是个热天气,索性把这个例子做出来~....记得刚开始,一言难尽。。算了,反正做出来就好。
注意,这里只是实现功能,没有做成类..废话out!

首先建立一个ASPX文件,打开工具箱,“放”出GridView控件,然后呢...看下面


                <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                            AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
                            DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging"  OnRowDataBound="GridView1_RowDataBound" GridLines="None">
                            <Columns>
                                <asp:TemplateField HeaderText="登录名">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("admin") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("admin") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="密码">
                                    <ItemTemplate>
                                        <%# Eval("UserPassword")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserPassword") %>'></asp:TextBox>&nbsp;
                                    </EditItemTemplate>
                                    <ItemStyle Width="100px" />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="最后登录时间" DataField="logintime" ReadOnly="True" />
                                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                            </Columns>
                            <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
                            <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#999999" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        </asp:GridView>

 

 可能很奇怪,都没有数据源,怎么还能DataBind数据?....嗯.....做程序,适当忽悠一下IDE,死不了的。当然,这里绑定的数据,跟数据库字段是一致的,忽悠也得有个限度~这也是算厚道吧(Hoho~~~),在下来,就真正实现里面的
增删了

之后,在cs文件里面实现增删改内容~

 

 SqlConnection conn  =   null ;
    
string  connstr  =  System.Configuration.ConfigurationManager.AppSettings[ " Connstring " ]; // 在Config中自己搞,不会的看书
     protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
        
if (!IsPostBack)//这句不会,去S吧
        {
            GridViewBind();
//做了一个绑定数据的方法.
        }

         
    }

    
private   void  GridViewBind()
    
{
         
   
        
string SqlStr = "Select * FROM admin";
        DataSet ds 
= new DataSet();

        
try
        
{
            conn 
= new SqlConnection(connstr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlDataAdapter da 
= new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds, 
"AdminInfo");
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
      
    }




    
protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;//获取当前行
        GridViewBind();
    }

    
protected   void  GridView1_RowCancelingEdit( object  sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;//取消更新
        GridViewBind();
    }

    
protected   void  GridView1_PageIndexChanging( object  sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        GridViewBind();
    }

    
protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
    
{
        
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();//获取当前行ID
        string admin = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;//强奸转化

        
string SqlStr = "update admin set admin='" + admin + "'where id=" + id;

        
try
        
{
            SqlConnection conn 
= new SqlConnection(connstr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlCommand comm 
= new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.EditIndex 
= -1;
            GridViewBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }

    }

    
protected   void  GridView1_RowDeleting( object  sender, GridViewDeleteEventArgs e)
    
{
       
//我不想写了...因为剩下的都...
        
//类似更新操作,改一下SQL语句就可以了
    }


好了!本地调试成功了.....

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值