2017-10-25 2 views
1

내 코드를 사용하지만, 편집 열이 거짓 활성화 모두, 내가 거짓 editbutton을 원하는 statuse 값에 따라foreach는 gridview에 행,있는 CommandField 편집은 참 또는 거짓 여기

foreach (GridViewRow row in GridView1.Rows) 
{ 
    statuse = dt.Rows[0].ItemArray[6].ToString(); 
    if (statuse != null || statuse != "") 
    { 
     if (control is LinkButton) 
     { 
      LinkButton btn = control as LinkButton; 
      btn.Enabled = btn.CommandName.Equals("Edit"); 
      btn.Enabled = false; 
     } 
    } 
} 
+0


당신이 찾고 계십니까? 'btn.Enabled = statuse == null || statuse == ""' – Reniuz

+0

이 경우 RowDataBound 이벤트를 사용합니다. – VDWWD

+0

예 저는 rowdatabound를 사용했습니다. 메신저를 찾고 : statuse value = ""면 commandfieled가 false로 설정됩니다. 하지만 내가 시도한 후에는 모든 명령 편집이 false가되었는데 상태 값이 없거나 값이없는 경우 –

답변

1

이 그것이 보통 RowDataBound 이벤트에 어떻게하는지입니다 . 편집 버튼이 자동으로 생성되어있는 경우

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //cast the dataitem back to a row 
     DataRowView row = e.Row.DataItem as DataRowView; 

     //find the edit button in the row with findcontrol 
     LinkButton btn = e.Row.FindControl("EditButtonID") as LinkButton; 

     //check the status of the correct data field and set the button properties 
     if (row["statuse"].ToString() == "Closed") 
     { 
      btn.Enabled = false; 
     } 
     else 
     { 
      btn.Enabled = true; 
     } 
    } 
} 

UPDATE

, 당신은 열 인덱스를 기반으로하는 LinkButton을 찾을 수있다.

LinkButton btn = e.Row.Cells[i].Controls[0] as LinkButton; 
+0

CommandField를 사용하여 메신저이므로 "EditButtonID"를 찾을 수 없습니다. –

+0

내 대답이 업데이트되었습니다. edit 버튼이 첫 번째 열에 있으면 'i'는'0'입니다 – VDWWD

+0

객체 참조가 객체의 인스턴스로 설정되지 않았습니다. –

0

// 여기 내 영문 gridview에

   <asp:TemplateField HeaderText = "No" ItemStyle-Width="50" ItemStyle-HorizontalAlign="Center"> 
    <ItemTemplate> 
     <asp:Label ID="lblRowNumberA" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

<asp:BoundField DataField="ProposedBy" HeaderText="Proposer" ReadOnly="true" 
      ItemStyle-Width="10%" /> 

     <asp:BoundField DataField="Date" HeaderText="Date" dataformatstring="{0:d}" 
      ItemStyle-Width="100px" ReadOnly="True" /> 


     <asp:TemplateField HeaderText="Note" ItemStyle-Width="70%" ItemStyle-HorizontalAlign="Left"> 
      <ItemTemplate> 
      <asp:Label ID="Label1ert" runat="server" Text='<%#Bind("Note") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
      <asp:TextBox ID="TextBox1edit" runat="server" TextMode="MultiLine" Text='<%#Bind("Note") %>' ></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 


     <asp:TemplateField HeaderText="Compere to standard(s)" ItemStyle-Width="20%"> 
      <ItemTemplate> 
      <asp:Label ID="Label1sdf" runat="server" Text='<%#Bind("Justify") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Recommendation" ItemStyle-Width="100px"> 
      <ItemTemplate> 
      <asp:Label ID="Label1sdfqq" runat="server" Text='<%#Bind("Status") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
      <asp:DropDownList ID="DDStatus" runat="server"> 


<asp:ListItem>Yes</asp:ListItem> 
<asp:ListItem>No</asp:ListItem> 
</asp:DropDownList> 
      </EditItemTemplate> 
     </asp:TemplateField> 

     <asp:BoundField DataField="HRStatus" HeaderText="Status" ReadOnly="true" 
      ItemStyle-Width="250px" /> 

    <asp:CommandField ButtonType="Link" ShowEditButton="true" ItemStyle-Width="100px" /> 

    </Columns>