GridView - insert image on run time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Garima12
    New Member
    • Mar 2007
    • 58

    #1

    GridView - insert image on run time

    Hi,
    I have a gridview and want to put image on each columns's top(first row). How can I do that? Please give some idea.
    thks
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by Garima12
    Hi,
    I have a gridview and want to put image on each columns's top(first row). How can I do that? Please give some idea.
    thks
    What image are you wanting to add to your gridview header columns? Are you wanting to add an image that will show the direction the column is sorted?

    Nathan

    Comment

    • Garima12
      New Member
      • Mar 2007
      • 58

      #3
      No, every column is showing the description of properties. I want to display the picture of property and that will be decided on run time that which picture should be called according to records fetched from the database.

      Originally posted by nateraaaa
      What image are you wanting to add to your gridview header columns? Are you wanting to add an image that will show the direction the column is sorted?

      Nathan

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by Garima12
        No, every column is showing the description of properties. I want to display the picture of property and that will be decided on run time that which picture should be called according to records fetched from the database.
        I don't understand what you are trying to do. Are you saying you have a gridview full of images? Or are you trying to show an image in the header of a gridview column based on a row column value populated from your database? If you are able to attach a screenshot of what you would like your page to look like that would be great.

        Nathan

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by Garima12
          No, every column is showing the description of properties. I want to display the picture of property and that will be decided on run time that which picture should be called according to records fetched from the database.
          You are going to have to use a TemplateColumn to do this.
          See this msdn article for quick instructions.

          If you require more assistance after reading through that article, get back to us.

          -Frinny

          Comment

          • Garima12
            New Member
            • Mar 2007
            • 58

            #6
            thks for the response of both of you.

            on button click I am writing code to execute stored procedure to fetch the data from database and it is fetching and filling the gridview correctly. Now I want to add a photo on the top row of each column in gridview. The stored procedure is also fetching the name of image file from the databse.

            I am writing following code to get the image displayed, but this is not happening.
            [code=vbnet]
            Protected Sub grdResult_RowDa taBound(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles grdResult.RowDa taBound
            If e.Row.RowType = DataControlRowT ype.DataRow Then
            If e.Row.RowIndex = 0 Then
            Dim drv As DataRowView = e.Row.DataItem
            For intCellCnt = 1 To e.Row.Cells.Cou nt - 1
            Dim img As New Image

            img.Width = Unit.Pixel(80)
            img.Height = Unit.Pixel(80)

            If intCellCnt = 1 Then img.BorderColor = Drawing.Color.R ed
            img.ImageUrl = "~/imgs/" & drv.Item(2).ToS tring
            e.Row.Cells(int CellCnt).Text = ""
            e.Row.Cells(int CellCnt).Contro ls.Add(img)
            Next
            End If
            [/code]
            when I see the properties of the image which is not being displayed, I found it is going till folder imgs, that's it.drv.item(2) is null. I think that's why image is not being displayed.

            Please suggest some solution.
            Thanks
            Last edited by Frinavale; Mar 18 '08, 04:17 PM. Reason: added [code] tags

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by Garima12
              thks for the response of both of you.

              on button click I am writing code to execute stored procedure to fetch the data from database and it is fetching and filling the gridview correctly. Now I want to add a photo on the top row of each column in gridview. The stored procedure is also fetching the name of image file from the databse.

              I am writing following code to get the image displayed, but this is not happening.
              [code=vbnet]
              Protected Sub grdResult_RowDa taBound(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles grdResult.RowDa taBound
              If e.Row.RowType = DataControlRowT ype.DataRow Then
              If e.Row.RowIndex = 0 Then
              Dim drv As DataRowView = e.Row.DataItem
              For intCellCnt = 1 To e.Row.Cells.Cou nt - 1
              Dim img As New Image

              img.Width = Unit.Pixel(80)
              img.Height = Unit.Pixel(80)

              If intCellCnt = 1 Then img.BorderColor = Drawing.Color.R ed
              img.ImageUrl = "~/imgs/" & drv.Item(2).ToS tring
              e.Row.Cells(int CellCnt).Text = ""
              e.Row.Cells(int CellCnt).Contro ls.Add(img)
              Next
              End If
              [/code]
              when I see the properties of the image which is not being displayed, I found it is going till folder imgs, that's it.drv.item(2) is null. I think that's why image is not being displayed.

              Please suggest some solution.
              Thanks
              Are simply trying to display images in the Row Header?
              If so, your line
              [code=vbnet]
              If e.Row.RowType = DataControlRowT ype.DataRow [/code]
              Should check for DataControlRowT ype.Header instead of DataControlRowT ype.DataRow.... only when you are on the Header should you be adding images.

              Also, where are your images located?
              They have to be some place that is accessible to the web page...in other words they have to be on your web server (in the wwwroot folder)...accor ding to you they are in your application's root in a folder named "imgs". Are they there?

              If this is the case why are you even storing the names of the files in the database? Why not just use the images as such?

              In the future could you please post your code in [CODE] tags (See How to Ask a Question).
              This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

              -Moderator Frinny

              Comment

              Working...