必须在我的编辑网格视图中单击两次

时间:2010-12-17 19:51:22

标签: c# gridview

好的所以我有一个带有下拉字段,显示按钮和我可以编辑的网格视图的网络应用程序。页面加载,我选择我的下拉值,页面加载正常。当我去点击编辑按钮时,我必须点击它两次以便能够编辑或取消(也有问题,但这是一个不同的问题) 无论如何,我希望能够单击编辑以调出更新/取消编辑模式。我是C#Web应用程序的新手,所以一些见解会有所帮助。

由于

我的ASP

<asp:GridView ID="GridView1" runat="server" CssClass="styled" 
   OnRowEditing="TaskGridView_RowEditing"
   OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
   OnRowUpdating="TaskGridView_RowUpdating"   >
   <Columns>
        <asp:CommandField ShowEditButton="True" />
   </Columns>
</asp:GridView>

我的C#

protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{

    //Set the edit index.
    GridView1.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
   // BindData();


}

protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    //Reset the edit index.
    GridView1.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
    Image1.Visible = true;
    Image2.Visible = false;

}

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    System.Data.DataTable dt = (System.Data.DataTable)Session["EditDataPage"];

    //Update the values.
    GridViewRow row = GridView1.Rows[e.RowIndex];
   // dt.Rows[row.DataItemIndex]["QuoteNumber"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["ItemNumber"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
   //dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
   // dt.Rows[row.DataItemIndex]["Item"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Descp"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Route"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Unit"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["IG"] = ((TextBox)(row.Cells[7].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["EXTQTY"] = ((TextBox)(row.Cells[8].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["CSTCD"] = ((TextBox)(row.Cells[9].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["PCOST"] = ((TextBox)(row.Cells[10].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["SCOST"] = ((TextBox)(row.Cells[11].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["ACOST"] = ((TextBox)(row.Cells[12].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["TCOST"] = ((TextBox)(row.Cells[13].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["ICOST"] = ((TextBox)(row.Cells[14].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["BIZCODE"] = ((TextBox)(row.Cells[16].Controls[0])).Text;
 //   dt.Rows[row.DataItemIndex]["DeleteItem"] = ((TextBox)(row.Cells[17].Controls[0])).Text;

    //Reset the edit index.
    GridView1.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
}

private void BindData()
{

    GridView1.DataSource = Session["Sqldatasource1"];
    GridView1.DataBind();

3 个答案:

答案 0 :(得分:3)

在划船事件中,您需要设置editindex,然后设置数据绑定。你也需要在其他活动中做到这一点。见http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx

GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
如果您不必要地进行数据绑定,您的控制可能会消失。确保你只在page_load中进行一次数据绑定。

if(!Page.IsPostBack)
    GridView1.DataBind();

答案 1 :(得分:1)

我必须单击两次编辑的修复程序包括ViewState。

我的原始加载和绑定:

&#13;
&#13;
AttribGrid.DataSource = dataset;
AttribGrid.DataBind();
ViewState["CurTable"] = dataset;
&#13;
&#13;
&#13;

然后是我随后的RowEditing。

&#13;
&#13;
protected void AttribGrid_RowEditing(object sender, GridViewEditEventArgs e)
	{
		AttribGrid.EditIndex = e.NewEditIndex;
		AttribGrid.DataSource = (DataSet)ViewState["CurTable"];
		AttribGrid.DataBind();
		
	}
&#13;
&#13;
&#13;

我必须使用EditIndex = newEditIndex。当我排除它时,我仍然需要点击两次。使用上面的代码,按预期单击一次。

答案 2 :(得分:0)

您无需在编辑或取消回调中设置“GridView1.EditIndex”。

protected void My_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
// Perform a custom action in here
// But you don't need to set GridView1.EditIndex in here, that would be bad.
}