Click here to Skip to main content
15,896,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a newbie in vb.net ! my code working to show my database data on GridView it's work fine but I want to change specific value to Image? my code :
Dim img_c As String = "my image Url"
    Dim img_x As String
    Dim constr As String = ("my connection string")
    Dim con As New SqlConnection(constr)
    Dim cmd As New SqlCommand()
    cmd.CommandText = "SELECT * FROM Attendance"
    cmd.Connection = con
    Dim sda As New SqlDataAdapter(cmd)
    Dim dt As New DataTable()
    sda.Fill(dt)
    GridView1.DataSource = dt
    GridView1.DataBind()
    Dim ds As New DataSet
    For i As Integer = 1 To GridView1.Rows.Count - 2
        For s As Integer = 3 To GridView1.Columns.Count - 2
            If GridView1.Rows(i).Cells(s).text ="1" Then
                GridView1.ImageField(img_c)

            End If
        Next
    Next

my source:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
       CellPadding="4" DataKeyNames="std_num"
       Height="448px" Width="1352px" BackColor="White" BorderColor="#3366CC"
       BorderStyle="None" BorderWidth="1px" style="margin-right: 12px">
       <Columns>
           <asp:BoundField DataField="Total" HeaderText="totla" SortExpression="Total" >
           <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="4" HeaderText="4" SortExpression="4"  >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="3" HeaderText="3" SortExpression="3"  >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="2" HeaderText="2" SortExpression="2"  >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="1" HeaderText="1" SortExpression="1"  >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="std_name" HeaderText="std_name"
               SortExpression="std_name" >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="std_num" HeaderText="std_num"
               SortExpression="std_num" ReadOnly="True" >
               <HeaderStyle ForeColor="White" />
           </asp:BoundField>
           <asp:BoundField DataField="No" HeaderText="No" SortExpression="No"
               InsertVisible="False" ReadOnly="True" >
           <HeaderStyle ForeColor="White" />
           </asp:BoundField>
       </Columns>
       <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
       <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
       <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
       <RowStyle BackColor="White" ForeColor="Black" BorderColor="#333333"
           Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
       <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
       <SortedAscendingCellStyle BackColor="#EDF6F6" />
       <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
       <SortedDescendingCellStyle BackColor="#D6DFDF" />
       <SortedDescendingHeaderStyle BackColor="#002876" />
   </asp:GridView>

my issue is change field to Image , there is no GridView image properties

What I have tried:

If ds.Tables("Attendance").Rows(i - 1).Item(s).ToString = "1" Then
GridView1.Rows(i).Cells(s).Text = ImageField(img_c)

End If
Posted
Updated 22-Aug-18 3:52am

1 solution

You can't assign a value to the ImageField in GridView. However, you can probably use the DataImageUrlField attribute of ImageField and set the image URL there.

Also, if you want to have a full control over your Image, then I would suggest you to use TemplateField instead. For example:

ASP.NET
<asp:TemplateField>
      <ItemTemplate>
           <asp:image id="Image1" runat="server" />
       </ItemTemplate>
</asp:TemplateField>


Then in your code, at RowDataBound event, you can access the Image control and set the ImageUrl to whatever you want based on your condition. A quick example would be:

VB
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim img1 As Image = CType(e.Row.FindControl("Image1"), Image)

        If img1 IsNot Nothing Then
            Dim fieldToCheck = e.Row.Cells(4).Text

            If fieldToCheck.Equals("1") Then
                img1.Visible = True
                img1.ImageUrl = "Set the image path here"
            Else
                img1.Visible = False
            End If
        End If
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900