GridView无代码分页排序,编辑,取消,删除,下拉菜单DropDownList结合

GridView无代码分页排序
GridView编辑,取消,删除
GridView和下拉菜单DropDownList结合

1.GridView无代码分页排序:
效果图:

1.jpg
 注意   1、设置AllowSorting属性为True;
            2、默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。
            3、添加GridView1_PageIndexChanging事件,加入以下代码:
                   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
                       {
                           GridView1.PageIndex = e.NewPageIndex;
                           Bind();
                      }


2、GridView编辑,取消,删除

2.jpg
注意:更新时一定要在 if(!ispostback) 里绑定,负责更新不了的,理由就不说了,大家都明白的。

3、GridView和下拉菜单DropDownList结合
  
     注意;获取DropDownList1的当前值:string name2 = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue.ToString();


后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con;
        SqlCommand cmd;
        string Strcon = "Data Source=672C9A1630B84F7;Initial Catalog=db_news;User ID=sa";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
           }
        //绑定
        protected void Bind()
        {
            string strsql = "select * from tbNews";
            con = new SqlConnection(Strcon);
            SqlDataAdapter myda = new SqlDataAdapter(strsql, con);
            DataSet ds = new DataSet();
            con.Open();
            myda.Fill(ds);
             GridView1.DataSource = ds;
            GridView1.DataKeyNames=new string[]{"ID"};
            GridView1.DataBind();
            con.Close();
 
        }
    
        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bind();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            con = new SqlConnection(Strcon);
            string name1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
            string name2 = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue.ToString();
            string strsql = "update tbnews set Title='" +name1+"',Type='" +name2+ "' where ID="+GridView1.DataKeys[e.RowIndex].Value.ToString()+"";
           
            con.Open();
            cmd = new SqlCommand(strsql, con);
            cmd.ExecuteNonQuery();
           
            GridView1.EditIndex = -1;
            con.Close();
            Bind();
       
        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            con = new SqlConnection(Strcon);
            string strsql = "delete from tbnews where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            con.Open();
            cmd = new SqlCommand(strsql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Bind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind();
        }
        //说明:RowDataBound事件是对行经行数据绑定后激发;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlt = (DropDownList)e.Row.FindControl("DropDownList1");
                if (ddlt != null)
                {
                    string strsql = "select * from leibie";
                    con = new SqlConnection(Strcon);
                    SqlDataAdapter myda = new SqlDataAdapter(strsql, con);
                    DataSet ds = new DataSet();
                    con.Open();
                    myda.Fill(ds);
                    ddlt.DataSource = ds;
                    ddlt.DataTextField="sort";
                    ddlt.DataValueField = "sort";
                    ddlt.DataBind();
                }
            }
        }
      
       
    }
}

前台主要代码:
<asp:GridView ID="GridView1" runat="server" Height="166px" Style="font-size: 12px;
            z-index: 100; left: 52px; position: absolute; top: 41px" Width="523px" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" PageSize="5" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound">
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="编号" ReadOnly="True" />
                <asp:BoundField DataField="Title" HeaderText="标题" />
                <asp:TemplateField HeaderText="类别">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"  Width="72px">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Height="20px"  Text='<%#Bind("Type") %>'> Width="56px"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="IssueDate" HeaderText="时间" ReadOnly="True" />
                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
            </Columns>
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
posted @ 2008-06-04 04:14  Snow*Cao  阅读(1576)  评论(0编辑  收藏  举报