惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
博客园 - Franky
V
V2EX
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
博客园 - 叶小钗
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
D
Docker
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志

博客园 - 一麦

团队开发之环境搭建 .Net下SqlServer命名规范 XXX公司CRM项目开发日志 Coolite Toolkit学习笔记 LINQ新特性简介及入门教程 反射学习 常用正则表达式 正则表达式基本用法 正则表达式基础 页面生命周期小结 设计模式小结 Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一) 3.x新特性 FileStream读写文件【StreamWriter 和 StreamReader】 .NET常用集合类 面向对象点滴 C#基础点滴 全新对待.net---一次全面的旅程 委托和事件--一直以来的痛
GridView的增删改查和分页 - 一麦 - 博客园
一麦 · 2010-12-10 · via 博客园 - 一麦


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