using   System;
using   System.Data;
using   System.Web;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;

public   partial   class   CSDN_GridMuliEdit   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                if   (!IsPostBack)
                {
                        BindTestData();
                }
        }

        protected   void   BindTestData()
        {
                //模拟出一些原始数据绑定DataGrid
                DataTable   dt1   =   new   DataTable( "Table1 ");
                dt1.Columns.Add( "ID ");
                dt1.Columns.Add( "产品 ");
                dt1.Columns.Add( "数量 ");
                dt1.Columns.Add( "日期 ");
                dt1.Columns.Add( "状态 ");

                dt1.Rows.Add(new   object[]   {   123, "产品AA ",   12,   "2006-11-14 ",   "1 "   });
                dt1.Rows.Add(new   object[]   {   124,   "产品BB ",   21,   "2006-11-13 ",   "0 "   });
                dt1.AcceptChanges();

                this.GridView1.DataSource   =   dt1;
                this.GridView1.DataBind();
        }

        protected   DataTable   GetDataFromGrid()
        {
                DataTable   dt1   =   new   DataTable( "Table1 ");
                dt1.Columns.Add( "ID ");
                dt1.Columns.Add( "产品 ");
                dt1.Columns.Add( "数量 ");
                dt1.Columns.Add( "日期 ");
                dt1.Columns.Add( "状态 ");
                for(int   i   =0;i <GridView1.Rows.Count;i++)
                {
                        GridViewRow   gRow   =   GridView1.Rows[i];
                        DataRow   newRow   =   dt1.NewRow();
                        newRow[0]   =   GridView1.DataKeys[i].Value;
                        newRow[1]   =   ((TextBox)gRow.FindControl( "TextBox1 ")).Text;
                        newRow[2]   =   ((TextBox)gRow.FindControl( "TextBox2 ")).Text;
                        newRow[3]   =   ((TextBox)gRow.FindControl( "TextBox3 ")).Text;
                        newRow[4]   =   ((DropDownList)gRow.FindControl( "DropDownList1 ")).SelectedValue;
                        dt1.Rows.Add(newRow);
                }
                dt1.AcceptChanges();
                return   dt1;
        }

        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                DataTable   dt   =   this.GetDataFromGrid();
                DataRow   newRow   =   dt.NewRow();
                newRow[ "状态 "]   =   "1 ";
                dt.Rows.Add(newRow);
                this.GridView1.DataSource   =   dt;
                this.GridView1.DataBind();
        }
        protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                DataTable   dt   =   this.GetDataFromGrid();
                foreach   (DataRow   row   in   dt.Rows)
                {
                        if   (row[ "ID "]   !=   null)
                        {
                                //更新该行记录到数据库
                        }
                        else
                        {
                                //插入该行记录到数据库
                        }
                }

                //到数据库删除   ViewState[ "DeletedID "]中记录的行
        }
        protected   void   GridView1_RowDeleting(object   sender,   GridViewDeleteEventArgs   e)
        {
                int   deletedIndex   =   e.RowIndex;
                DataTable   dt   =   this.GetDataFromGrid();

                //如果删除的是数据库中原有的记录,则把关键字记录在ViewState
                if   (dt.Rows[deletedIndex][ "ID "]   !=   null)
                {
                        string   deletedID   =   string.Empty;
                        if   (ViewState[ "DeletedID "]   !=   null)   deletedID   =   ViewState[ "DeletedID "].ToString();
                        deletedID   +=   " ' "   +   dt.Rows[deletedIndex][ "ID "].ToString()   +   " ', ";
                        ViewState[ "DeletedID "]   =   deletedID;
                }
               
                dt.Rows[deletedIndex].Delete();
                dt.AcceptChanges();
                this.GridView1.DataSource   =   dt;
                this.GridView1.DataBind();
        }
}

posted on 2011-08-10 23:23  #阿志#  阅读(723)  评论(0)    收藏  举报