2016-08-19 70 views
1

在頁面加載我執行這些代碼:如何訪問GridView中的特定行並更改其屬性?

For Each row As GridViewRow In gvRD.Rows 

     If dt.Rows(0).Item("AKCIJA").ToString = "unos" Then 

      Dim myListBox As ImageButton = DirectCast(row.FindControl("btn"), ImageButton) 
      myListBox.ImageUrl = "~/Images/no_edit.png" 
      myListBox.Enabled = False 

     End If 

Next 

有了這個,我在每行改變的ImageUrl,無論是或不喜歡「UNOS」,但我只是想改變與AKCIJA行= 「UNOS」。

這是怎麼樣子的GridView:

  <asp:GridView ID="gvRD" Width="1100px" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
          OnPageIndexChanging="gvRD_PageIndexChanging" 
          OnSelectedIndexChanged= "OnSelectedIndexChanged" 
          OnRowCommand="gvRD_RowCommand"> 

       <PagerSettings Mode="NextPrevious" FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" 
           LastPageText="Last" PreviousPageImageUrl="~/Images/previous_1.png" NextPageImageUrl="~/Images/next_1.png"/> 
       <AlternatingRowStyle BackColor="#EFEFEF" Wrap="False" Font-Italic="False" Font-Names="Arial" 
            Font-Strikeout="False" Font-Underline="False" Font-Overline="False" Font-Bold="False"></AlternatingRowStyle> 
       <RowStyle BackColor="White" Wrap="False" Font-Italic="False" Font-Names="Arial" 
          Font-Strikeout="False" Font-Underline="False" Font-Overline="False" Font-Bold="False"></RowStyle> 
       <HeaderStyle BackColor="#336699" ForeColor="White" Wrap="False" Font-Italic="False" 
          Font-Strikeout="False" Font-Underline="False" Font-Overline="False" Font-Bold="False"></HeaderStyle> 

       <Columns> 

         <asp:BoundField DataField="DATE" HeaderText="Date" ItemStyle-HorizontalAlign="Center" >  
         </asp:BoundField> 

         <asp:BoundField DataField="LK" HeaderText="LK" ItemStyle-HorizontalAlign="Center" HtmlEncode="False"> 
         </asp:BoundField> 

         <asp:BoundField DataField="LIK" HeaderText="LIK" ItemStyle-HorizontalAlign="Center" >      
         </asp:BoundField> 

         <asp:BoundField DataField="DN" HeaderText="DN" ItemStyle-HorizontalAlign="Center" HtmlEncode="False" > 
         </asp:BoundField> 

         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:ImageButton ID="btn" runat="server" CommandName="EditRow" CommandArgument='<%# Bind("ID")%>' ImageUrl="~/Images/edit.png" Enabled="true" /> 
          </ItemTemplate> 
         </asp:TemplateField> 

       </Columns> 
      </asp:GridView> 

回答

2

如果包括在GridView的的DataKeyNames領域 「AKCIJA」:

<asp:GridView ID="gvRD" runat="server" DataKeyNames="AKCIJA" ... > 

可以檢索字段的值的每一行:

For Each row As GridViewRow In gvRD.Rows 
    If gvRD.DataKeys(row.RowIndex).Values("AKCIJA").ToString() = "unos" Then 
     ... 
    End If 
Next 
+0

這正是我想要的。萬分感謝。 – SeaSide

0

這是什麼意思?

For Each row As GridViewRow In gvRD.Rows 
    Dim myListBox As ImageButton = DirectCast(row.FindControl("btn"), ImageButton) 
    If myListBox.ImageUrl = "unos" Then 
     myListBox.ImageUrl = "~/Images/no_edit.png" 
     myListBox.Enabled = False 
    End If 
Next 
+0

「UNOS」只是AKCIJA欄中的一個值。我有DataTable我用來填補GridVIew和GriedView我有一個控制ImageButton應該是每一行Enabled = False在哪裏找到AKCIJA =「unos」。 – SeaSide

+0

你可以發佈添加「btn」到該行的代碼嗎?你在做一個自定義的DataGridViewColumn類嗎? – FloatingKiwi

相關問題