Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView(e.Row.RowState和DataControlRowState.Edit)>;0不工作_C#_Asp.net_Gridview - Fatal编程技术网

C# GridView(e.Row.RowState和DataControlRowState.Edit)>;0不工作

C# GridView(e.Row.RowState和DataControlRowState.Edit)>;0不工作,c#,asp.net,gridview,C#,Asp.net,Gridview,我想做的是,在点击gridview的Edit按钮时,我想根据输入的内容更改一些EditItemTemplate控件的属性。然而,这些变化并没有发生,经过一些调试之后,我确定代码没有通过一行是否处于编辑模式的测试 因此,在做了一些研究之后,许多网站告诉我,检测EditItemTemplate的方法是使用上面的代码:if((e.Row.RowState&DataControlRowState.Edit)>0) 有关守则: protected void gvSchedule_RowEditing(ob

我想做的是,在点击gridview的Edit按钮时,我想根据输入的内容更改一些EditItemTemplate控件的属性。然而,这些变化并没有发生,经过一些调试之后,我确定代码没有通过一行是否处于编辑模式的测试

因此,在做了一些研究之后,许多网站告诉我,检测EditItemTemplate的方法是使用上面的代码:
if((e.Row.RowState&DataControlRowState.Edit)>0)

有关守则:

protected void gvSchedule_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string mySelect;
        gvSchedule.EditIndex = e.NewEditIndex;
        mySelect = (some SQL)
        scheduleDataSource.SelectCommand = mySelect;
        scheduleDataSource.DataBind();
    }

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
               //Altering Code
            }
        }
     }
和gridView标题:

<asp:GridView ID="gvSchedule" runat="server" AlternatingRowStyle-CssClass="GridAlternatingRowStyle" EmptyDataText="There are no item(s) to display." 
            RowStyle-CssClass="GridRo wStyle" HeaderStyle-CssClass="GridHeaderStyle" AllowSorting="True" AutoGenerateColumns="False" 
            DataSourceID="scheduleDataSource" AutoGenerateEditButton="True" OnRowDataBound = "gvSchedule_RowDataBound" OnRowEditing="gvSchedule_RowEditing" >


我肯定我忽略了一些愚蠢的事情,但我似乎找不到它。我尝试了各种不同的逻辑语句,试图超越if语句,但它就是不去。如果你需要任何其他信息,请告诉我,我会把它挂起来的。谢谢

我认为If语句的正确语法是:

if ((e.Row.RowState && DataControlRowState.Edit) > 0)
{
  //Altering Code
}
或者,如果这不起作用,请尝试以下方法:

if((e.Row.RowState > 0) && (DataControlRowState.Edit > 0))
{
  //code here
}
您要使用“&&”而不仅仅是“&”的密钥就在这里。

您必须检查

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        if ((e.Row.RowState & DataControlRowState.Edit) != 0)
        {
            //Altering Code            

        }
    }
 }
而不是

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
           //Altering Code
        }
    }
 }

您可以使用e.Row.RowState.HasFlag(DataControlRowState.Edit)

而不是(e.Row.RowState&DataControlRowState.Edit)>0


这是一个更好的构造…

值为Alternate,Edit为Alternate | Edit。HasFlag将让您知道当前值中是否包含枚举值

if (e.Row.RowType == DataControlRowType.DataRow) {
    if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) {
        //Altering Code
    }
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
    {
       //Init Code
    }
    else if (e.Row.RowState.HasFlag(DataControlRowState.Edit))               
   {
      //Alter Code
   }
}

我试试看。我的理解是,您希望使用位and运算符,而不是逻辑“and”。但是,是的,我会尝试并报告回来。尝试了这个,没有改变。值得一提的是,我一直在跟踪调试窗口中的所有数据。行状态始终为“编辑”。所以我真的不知道为什么它不会过去。请考虑扩大你的答案来解释一点点。不幸的是,我不再工作的地方,我需要这个答案。我想我最终找到了解决这个问题的办法。我很感激你给我一个答案!