当前位置:编程学习 > C#/ASP.NET >>

Datatable的问题

我把一个table作为gridview2的数据源 table里面的数据是从被单击的gridview1的行来的。
现在问题来了 我单击了gridview1的一行 然后单击第2次的时候原来被单击的一行数据丢失了 也就是table里面永远只有一行
怎么才能保持单击过的全部数据啊··
代码如下

public partial class ZDWH_Indext : System.Web.UI.Page
{
    DataTable DTable=new DataTable("SubTable");
    string str = ConfigurationManager.ConnectionStrings["con"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {//构建一个表 来储存用户单击"添加到订单" 的行数据 放在这个表 然后给订单信息表提供表数据
        
          if (DTable.Columns.Count==0 )
          {
            DTable.Columns.Add("code",System.Type.GetType("System.String"));
            DTable.Columns.Add("name",System.Type.GetType("System.String"));
            DTable.Columns.Add("price", System.Type.GetType("System.Single"));
            DTable.Columns.Add("author", System.Type.GetType("System.String"));
            DTable.Columns.Add("BC", System.Type.GetType("System.String"));
            DTable.Columns.Add("cbs",System.Type.GetType("System.String"));     
            DTable.Columns.Add("subnum", System.Type.GetType("System.Int32"));
            DataColumn [] KeyColumns={DTable.Columns[0]};
            DTable.PrimaryKey = KeyColumns;
              
    }
    //    //绑定订单的gridview的数据
    GridView2.DataSource = DTable;
    GridView2.DataBind();
 

        if (!Page.IsPostBack)
        {
            ////判断用户已经登录
            if (Session["CurrUser"]==null)
            { Response.Redirect("~/Index.aspx"); }
            //出版社下拉菜单数据绑定
            this.SelBuyer.DataSource = CreateDatasoure();
         this.SelBuyer.DataTextField = "Name";
            this.SelBuyer.DataValueField = "Code";
            this.SelBuyer.DataBind();
            this.DDate.Text = DateTime.Now.ToString("yyyy-MM-dd ");
        }
           
       
    }
    //获取出版社数据主要为下拉菜单提供数据
    private DataSet CreateDatasoure()
    {
        DataSet ds = new DataSet();  
        SqlConnection con = new SqlConnection(str);
        string cmdStr = "SELECT * FROM BookStore.dbo.BuyerInfoSheet ORDER BY Code";
        SqlCommand cmd = new SqlCommand(cmdStr, con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmdStr, con);
        da.Fill(ds,"DataTable");
        return ds;
      
    }
       protected void Query_Click(object sender, EventArgs e)
    {
        if (query_content.Text != "")
        {
            string str1 = " and " +query_tj.SelectedValue + " like '%" +query_content.Text + "%'";


            SqlDataSource1.SelectCommand = "select a.*,abbrname,storeamounta,storeamountz from bookinfosheet a,pressinfosheet b,bookstoresheeta c,bookstoresheetz d where a.presscode=b.code and a.code=c.bookcode and a.code=d.bookcode " + str1 + " order by a.code";
        }
        SqlDataSource1.Select(DataSourceSelectArguments.Empty);

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {//特别要注意设置dataKey  我已经在Gridview1属性窗口里面设置好了
        //Requiredfieldvalidator1.Enabled = false;
        //Requiredfieldvalidator2.Enabled = false;
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //Requiredfieldvalidator1.Enabled = true;
        //Requiredfieldvalidator2.Enabled = true;
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {  
        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "AddToSub")
        { //获取单击的行
            Int32 index=Convert.ToInt32(e.CommandArgument);
            GridViewRow CommandRow=GridView1.Rows[index];
        
            string  CodeStr=CommandRow.Cells[0].Text;
            string NameStr= CommandRow.Cells[1].Text;
            string PriceStr=CommandRow.Cells[2].Text;
            string   AuthorStr =CommandRow.Cells[3].Text;
            string BcStr=CommandRow.Cells[4].Text;
            string CbsStr = CommandRow.Cells[5].Text;
            //下面的语句判断新加入的行有没有重复 用DataRowCollection.Contains 方法 (Object)

            DataRowCollection myDataRowCollection;
            myDataRowCollection = DTable.Rows;
           
            //判断时候有重复行
            if (myDataRowCollection.Contains(CodeStr))
            {
                String False = "<script language='javascript'>alert('数据保存不成功,有重复')</script>";
                
                Page.ClientScript.RegisterStartupScript(this.GetType(),"my1",False);
            }
                
         
            else
            {
                DataRow myDatarow = DTable.NewRow();
                myDatarow[0] = CodeStr.Trim();
                myDatarow[1] = NameStr.Trim();
               myDatarow[2] = PriceStr.Trim();
               myDatarow[3] = AuthorStr.Trim();
               myDatarow[4] = BcStr.Trim();
               myDatarow[5] = CbsStr.Trim();

                DTable.Rows.Add(myDatarow);
           
              

            
            }
        }
        GridView2.DataSource = DTable;
        GridView2.DataBind();
    }

    private string getalertinfo(string str1)
    {
        String Str2 ;
        Str2="<script language='javascript'>alert('" + str1 +"')<";
        Str2+="/";
        Str2+="script>";
        return Str2.Trim();
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
} --------------------编程问答-------------------- 请大家赶快帮忙啊·····
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,