如何访问 GridView 中的特定行并更改其 属性?

How to access specific row in GridView and change its property?

在页面加载时,我正在执行这些代码行:

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>

你是这个意思吗?

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

如果您在 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