2
  GridView gv = new GridView();

  BoundField farmername = new BoundField();
  farmername.HeaderText = "Farmer Name";
  farmername.DataField = "farmername";
  gv.Columns.Add(farmername);


  BoundField villagename = new BoundField();
  villagename.HeaderText = "Village Name";
 villagename.DataField = "village";
 gv.Columns.Add(villagename);

  BoundField feedtype = new BoundField();
  feedtype.HeaderText = "Feed Type";
  feedtype.DataField = "feedtype";
  gv.Columns.Add(feedtype);


  BoundField bf50kg = new BoundField();
  bf50kg.HeaderText = "50 Kg Bags";  
  bf50kg.DataField = "noof50kgsbags";
  gv.Columns.Add(bf50kg);

  CommandField cf = new CommandField();
  cf.ButtonType = ButtonType.Button;
  cf.ShowCancelButton = true;
  cf.ShowEditButton = true;
  gv.Columns.Add(cf);

  gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
  gv.RowUpdating += new GridViewUpdateEventHandler(gv_RowUpdating);
  gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);

  gv.AutoGenerateColumns = false;
  gv.ShowFooter = true;
  gv.DataSource = dtIndentDetails;
  gv.DataBind();

当我单击编辑按钮时,它不会拆分为更新、取消按钮。如何使用命令字段执行此操作。如果我在 aspx 页面中添加 gridview,它的拆分以更新和取消

4

2 回答 2

1

尝试了您的代码并发现它可以正常工作。请注意以下几点:

1.) 每次都应执行创建 GridView (和所有字段)的代码。!IsPostback表示从此代码中删除任何条件,如果存在的话。

2.)在您RowEditing 的gridview事件中设置editindex并重新绑定gridview。

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gv = sender as GridView;
        gv.EditIndex = e.NewEditIndex;
        gv.DataBind();
    }
于 2013-10-11T05:49:27.550 回答
1

试试下面的代码:

protected void gridview_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView gv = (GridView)sender;
   // Change the row state
    gv.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;

}
于 2013-10-10T09:08:19.567 回答