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>

 

 

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语句就可以了
    }

posted on 2010-12-10 12:12  一麦  阅读(448)  评论(0编辑  收藏  举报