SeaSide SeaSide - 3 months ago 14
Vb.net Question

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

On page load I am executing these lines of code:

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


With this I am changing ImageUrl on each row, whether it is or not like "unos", but I only want to change rows with AKCIJA = "unos".

This is how looks like 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>

Answer

If you include the field "AKCIJA" in the DataKeyNames of the GridView:

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

you can retrieve the value of the field for each row:

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