在孟子E章看到的http://dotnet.aspx.cc/article/47654f82-6a22-4b25-98a0-830e26a7e75e/read.aspx现在我要将输入的值循环加入到数据库中。
在GridView rowdatabound 的时候 ,有几列是空的(单价,数量未输入),页面刷新后才输入的,因此数量和单价列是空的  for (int i = 0; i < this.GridView1.Rows.Count; i++)
  {
          string cInvCode = null;
          string cInvName = null;
          string iUnitCost = null; //单价
            string quantity = null;//数量
      if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
      {
          cInvCode = GridView1.Rows[i].Cells[1].Text;//获取第i行第1列的内容
           cInvName = GridView1.Rows[i].Cells[2].Text;
          iUnitCost = GridView1.Rows[i].Cells[5].Text.ToString();//为空
          quantity = GridView1.Rows[i].Cells[6].Text.ToString();//为空
              //iUnitCost = Request.Form["txt" + i.ToString() + "_" + GridView1.Rows[i].Cells[5].Text.ToString()];  这也为空
            //quantity = Request.Form["txt" + i.ToString() + "_" + GridView1.Rows[i].Cells[6].Text.ToString()];这也为空     }
}
请问各位一定要按以下方式取值吗?
    string s = String.Empty;
      for (int i = 0; i < 5; i++)
      {
          for (int j = 0; j < 5; j++)
          {
              s = s + "<li>第 " + i.ToString() + " 行第 " + j.ToString() + " 列的值是:" + Request.Form["txt" + i.ToString() + "_" + j.ToString()];
          }
      }
      ret.Text = s;

解决方案 »

  1.   

    我就想实现在gridview 中进行增加操作。
    首先,孟子E章中http://dotnet.aspx.cc/article/47654f82-6a22-4b25-98a0-830e26a7e75e/read.aspx 
    实现了点击某个Gridview 的cell ,该cell 成为一个textbox,并可以输入值。(主要采用javascript)
    循环可以取出各个值。  
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dv = (DataRowView)e.Row.DataItem;            for (int i = 1; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onclick", "showEdit(" + e.Row.RowIndex.ToString() + "," + i.ToString() + ")");
                               e.Row.Cells[i].Text = "<input onblur='lostfocus(this)'  name='txt" + e.Row.RowIndex.ToString() + "_" + i.ToString() + "' readonly='readonly' class='noborder' value='" + dv[i].ToString() + "'/>";
                 
                }
            }    }    function showEdit(r, c) {
          document.forms[0].elements["txt" + r + "_" + c].readOnly = false;
          document.forms[0].elements["txt" + r + "_" + c].className = 'hasborder';
          document.forms[0].elements["txt" + r + "_" + c].select();
        }
        function lostfocus(o) {
          o.className = 'noborder'
          o.readOnly = true;
        }
    添加的数据,一些是从另一页面获取的,
    所以在GridView rowdatabound 的时候 ,这几列是有值的,而另几列,我从另一页面获取值后才输入的(刷新了), 因此这几列(数量和单价列)是空的 
    //循环添加的时候,单价数量为空<input name='xxx' value=''>
    for (int i = 0; i < this.GridView1.Rows.Count; i++)
      {
              string cInvCode = null;
              string cInvName = null;
              string iUnitCost = null; //单价
                string quantity = null;//数量
          if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
          {
              cInvCode = GridView1.Rows[i].Cells[1].Text;//获取第i行第1列的内容
               cInvName = GridView1.Rows[i].Cells[2].Text;
              iUnitCost = GridView1.Rows[i].Cells[5].Text.ToString();//为空
              quantity = GridView1.Rows[i].Cells[6].Text.ToString();//为空
                  //iUnitCost = Request.Form["txt" + i.ToString() + "_" + GridView1.Rows[i].Cells[5].Text.ToString()];  这也为空
                //quantity = Request.Form["txt" + i.ToString() + "_" + GridView1.Rows[i].Cells[6].Text.ToString()];这也为空     }
    }