为什么Gridview“AutoGenerateEditButton = true”属性在运行时不起作用?

时间:2011-01-24 13:59:50

标签: asp.net gridview

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class ExptGridviewEdit : System.Web.UI.Page
{
    SqlCommand com;
    SqlDataAdapter da;
    DataTable dtb;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
            com = new SqlCommand("Select * from tblExpt", con);
            da = new SqlDataAdapter(com);

            dtb = new DataTable();
            da.Fill(dtb);
            if (dtb.Rows[0] != null)
            {

                BindData();
            }
            GridView1.AutoGenerateEditButton = true;
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
       // }
    }
    protected void BindData()
    {
        GridView1.DataSource = dtb;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = dtb;
        GridView1.DataBind();
    }

}

1 个答案:

答案 0 :(得分:1)

  

当支持更新的数据源控件绑定到GridView控件时,GridView控件可以利用数据源控件的功能并提供自动更新功能。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateeditbutton.aspx