public partial class addxzflower : System.Web.UI.Page
{
   private static string connectionString = "server=.;uid=sa;pwd=1;database=flowershops";
   yyjDAL.WebClass cl = new yyjDAL.WebClass();
   protected void Page_Load(object sender, EventArgs e)
   {
   Label1.Text = Session["Username"].ToString();
   if (!IsPostBack)
   {
   GridView1.DataKeyNames = new String[] { "st_name" };
   this.GridView1.DataSource = cl.GetAllxx();
   this.GridView1.DataBind();   }
   if (!IsPostBack)
   {   DataTable newdtb = new DataTable();
   newdtb.Columns.Add("st_id", typeof(int));
   newdtb.Columns.Add("st_name", typeof(string));
   newdtb.Columns.Add("ST_Price", typeof(string));
   newdtb.Columns.Add("ST_Sales", typeof(string));
   newdtb.Columns.Add("ST_Type", typeof(string));
   newdtb.Columns["st_id"].AutoIncrement = true;   }
   }
   protected void Button1_Click(object sender, EventArgs e)
   {   DataTable newdtb = new DataTable();
   newdtb.Columns.Add("st_id", typeof(int));
   newdtb.Columns.Add("st_name", typeof(string));
   newdtb.Columns.Add("ST_Price", typeof(string));
   newdtb.Columns.Add("ST_Sales", typeof(string));
   newdtb.Columns.Add("ST_Type", typeof(string));
   newdtb.Columns["st_id"].AutoIncrement = true;
       foreach (GridViewRow item in GridView1.Rows)
   {
   CheckBox chkID = (CheckBox)item.FindControl("CheckBox1");
   if (chkID.Checked == true && item.RowType == DataControlRowType.DataRow)
   {
   DataRow newRow = newdtb.NewRow();
  newRow["st_name"] = item.Cells[1].Text.ToString();
   newRow["ST_Price"] = item.Cells[2].Text.ToString();
   newRow["ST_Sales"] = item.Cells[3].Text.ToString();
   newRow["ST_Type"] = item.Cells[4].Text.ToString();
   newdtb.Rows.Add(newRow);
   }   }
   GridView2.DataSource = newdtb;
   GridView2.DataBind();
   }
   protected void Button2_Click(object sender, EventArgs e)
   {
   string username1 = Label1.Text;
   string shname = this.TextBox1.Text.Trim();
   string shadrss = this.TextBox2.Text.Trim();
   string shyb = this.TextBox3.Text.Trim();
   string shdh = this.TextBox4.Text.Trim();
   foreach (GridViewRow it in GridView1.Rows)
   {
   if (it.RowType == DataControlRowType.DataRow)
   {
   
   string name = it.Cells[1].Text.ToString();
   string price = it.Cells[2].Text.ToString();
   string sales = it.Cells[3].Text.ToString();
   string type = it.Cells[4].Text.ToString();
     
  SqlConnection conn = new SqlConnection(connectionString);
   string sql = "insert into userju (username,st_name,ST_Price,ST_Sales,ST_Type,sh_name,sh_adrss,sh_yb,sh_dh)values('" + username1 + "','" + name + "','" + price + "','" + sales + "','" + type + "','" + shname + "','" + shadrss + "','" + shyb + "','" + shdh + "')";
   SqlCommand cmd = new SqlCommand(sql, conn);
   conn.Open();   int result = cmd.ExecuteNonQuery();
   conn.Close();
   if (result > 0)
   {
   Response.Write("成功");
   }
   else
   {
   Response.Write("失败");
   }   }
   }
   }
   GridView1的数据添加到GridView2中,然后把选择的数据某一条,添加到数据库指定的表中,但是一下子把所有的都添加进去了,郁闷呀。到底是那里原因呀

解决方案 »

  1.   

    foreach (GridViewRow it in GridView1.Rows)
    当然是添加所有了
      

  2.   

    是啊foreach中每条都进行insert当然全部添加了
      

  3.   

    你看你写的 这个 玩意写的, 
    你都有 datakeystrings 了,你直接 在 Select 事件中 处理就好了 利用 e.RowIndex 取所选择的数据。
      

  4.   

    你这里是不是应该加上checked事件,判断是否选中,没有选中的不插入就可以了。
      

  5.   

     foreach (GridViewRow it in GridView1.Rows)
            {
                CheckBox chkID = (CheckBox)it.FindControl("CheckBox1");
                if (chkID.Checked == true && it.RowType == DataControlRowType.DataRow)
                {
                    //int id = int.Parse(it.Cells[0].Text);
                    string name = it.Cells[1].Text.ToString();
                    string price = it.Cells[2].Text.ToString();
                    string sales = it.Cells[3].Text.ToString();
                    string type = it.Cells[4].Text.ToString();
                    
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "insert into userju (username,st_name,ST_Price,ST_Sales,ST_Type,sh_name,sh_adrss,sh_yb,sh_dh)values('" + username1 + "','" + name + "','" + price + "','" + sales + "','" + type + "','" + shname + "','" + shadrss + "','" + shyb + "','" + shdh + "')";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    conn.Open();                int result = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (result > 0)
                    {
                        Response.Write("成功");
                    }
                    else
                    {
                        Response.Write("失败");
                    }            }
    已解决谢谢刚才那位好心人提醒