using System;
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 _Default : System.Web.UI.Page 
{

    private DataTable dt = new DataTable();

    private SqlConnection CreatConnection()
    {
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=MysqlData;server=(local)";
        return connection;
    }

    private void BindList()//绑定控件
    {
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataKeyNames = new string[] { "供应商编号" };//设置主键字段 
        SqlDataAdapter adapter = new SqlDataAdapter("select * from 应付账", CreatConnection());
        adapter.Fill(dt);
        if (!IsPostBack)
        {
            BindList();
        }
    }
protected void  GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    this.GridView1.EditIndex = e.NewEditIndex;
        BindList();   
}
protected void  GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    try
        {
            TextBox txtPrice = GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            if (txtPrice != null)
            {
                Response.Write(string.Format("把供应商编号为&nbsp;&nbsp;{0}&nbsp;&nbsp;的供应商名更改为&nbsp;&nbsp;&nbsp;&nbsp;{1}",
                    this.GridView1.DataKeys[e.RowIndex].Value.ToString(), txtPrice.Text));
            }
        }
        catch
        {
            e.Cancel = true;
        }
        this.GridView1.EditIndex = -1;
        BindList();
}
protected void  GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
        {
            Response.Write("删除供应商编号是&nbsp;&nbsp;&nbsp;&nbsp;"
+ this.GridView1.DataKeys[e.RowIndex].Value.ToString());
        }
        catch
        {
            e.Cancel = true;
        }
        this.GridView1.EditIndex = -1;
        BindList();
}
protected void  GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
     Response.Write("当前行的&nbsp;&nbsp;供应商编号：  " + this.GridView1.Rows[e.NewSelectedIndex].Cells[1].Text);
        Response.Write("<br>");
        Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;供应商名：  " + this.GridView1.Rows[e.NewSelectedIndex].Cells[2].Text);
        BindList();
}
protected void  GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
     this.GridView1.EditIndex = -1;
        BindList();
}
}
