GridView用法的修改和删除

(前台)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="3" Width="996px"
            Height="188px" style="text-align: center" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating"
            onselectedindexchanged="Page_Load" >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
                          <%--    <asp:BoundField DataField="Title" HeaderText="新闻标题" />
                            <asp:BoundField DataField="Contents" HeaderText="新闻内容" />
                            <asp:BoundField DataField="Type" HeaderText="新闻类型"/>
                              <asp:BoundField DataField="Author"   HeaderText="作者" />
                            <asp:BoundField DataField="IssueDate" HeaderText="发布时间" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> --%>
                             <asp:TemplateField HeaderText="新闻标题">
                                    <ItemTemplate>
                                        <%# Eval("Title") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBTitle" Text='<%# Eval("Title") %>' runat="server" Width="100px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="150px" />
                                </asp:TemplateField> 
                                <asp:TemplateField HeaderText="新闻内容">
                                    <ItemTemplate>
                                        <%# Eval("Contents") %>
                                     </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBContents" Text='<%# Eval("Contents") %>' runat="server" Width="200px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="350px" />
                                </asp:TemplateField> 
                                  <asp:TemplateField HeaderText="新闻类型">
                                    <ItemTemplate>
                                        <%# Eval("Type") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBType" Text='<%# Eval("Type") %>' runat="server" AutoPostBack="true" Width="50px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="50px" />
                                </asp:TemplateField> 
                                  <asp:TemplateField HeaderText="作者">
                                    <ItemTemplate>
                                        <%# Eval("Author")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBAuthor" Text='<%# Eval("Author") %>' runat="server" Width="100px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="70px" />
                                </asp:TemplateField> 
                               <asp:BoundField DataField="IssueDate" HeaderText="发布时间" />
                               <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                        </Columns>
                       
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#007DBB" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#00547E" />
          </asp:GridView>

(后台)

public partial class xinguanli : System.Web.UI.Page

{    

   protected void Page_Load(object sender, EventArgs e)   

  {        if (!IsPostBack)       

     {    GridView1.DataKeyNames = new string[] { "ID" };     

          bind();        

     }     

   }

  //删除    

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)    

{        SqlConnection conn = DB.createSql();   

        conn.Open();      

SqlCommand cmd = new SqlCommand("delete from T_new where ID='" + GridView1.DataKeys[e.RowIndex].Value + "'", conn);     

    int count = cmd.ExecuteNonQuery();     

    if (count > 0)      

   {             Response.Write("<script>alert('删除成功!') </script>");    

         bind();         }      

   else         {     

        Response.Write("<script>alert('删除失败!') </script>");   

      }  

   }

    //编辑     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  

   {        

    GridView1.EditIndex = e.NewEditIndex;   

      bind();  

   }    

//更新    

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  

   {        

//string ID = GridView1.DataKeys[e.RowIndex].Values.ToString();     

 //string Title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();        

//string Contents = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();        

//string Type = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();        

//string Author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();        

string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();        

string Title = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBTitle")).Text;        

string Contents = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBContents")).Text;        

string Type = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBType")).Text;        

string Author = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBAuthor")).Text;        

string Str = "update T_new set Title='" + Title + "', Contents='" + Contents + "',Type ='" + Type + "', Author ='" + Author + "' where ID=" + ID;        

SqlConnection conn = DB.createSql();        

conn.Open();        

SqlCommand cmd = new SqlCommand(Str,conn);        

int Result = cmd.ExecuteNonQuery();       

//cmd.ExecuteNonQuery();        

cmd.Dispose();        

conn.Close();        

GridView1.EditIndex = -1;        

bind();        

//try        

//{        

//    SqlConnection conn = DB.createSql();        

//    if (conn.State.ToString() == "Closed") conn.Open();        

//    SqlCommand cmd = new SqlCommand(Str, conn);        

//    cmd.ExecuteNonQuery();        

//    cmd.Dispose();      

   //    if (conn.State.ToString() == "Open") conn.Close();        

//    GridView1.EditIndex = -1;        

//    bind();        

//}        

//catch (Exception ex)       

  //{        

//    Response.Write("数据库错误,错误原因:" + ex.Message);        

//    Response.End();        

//}     }      

//取消     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {         GridView1.EditIndex = -1;

        bind();     }

    protected void bind()    

{        

SqlConnection conn = DB.createSql();       

  conn.Open();        

SqlCommand cmd = new SqlCommand("select * from T_new", conn);        

SqlDataAdapter sda = new SqlDataAdapter(cmd);        

DataTable dt = new DataTable();        

sda.Fill(dt);        

GridView1.DataSource =dt;        

GridView1.DataKeyNames = new string[]{"ID"}; //{ Convert.ToString(dt.Columns["ID"]) };       

  GridView1.DataBind();

 

    }

 

posted @ 2013-07-19 19:30  笨笨丫头~双  阅读(628)  评论(0编辑  收藏  举报