Gridview

Last post 05-16-2008 12:41 AM by blurearc. 2 replies.

Sort Posts:

  • Gridview

    05-14-2008, 4:29 PM

    I have a gridview and want to add an Image field in it.

    I also need to test to see if the image is in the folder or ftp site if it is then output link to the image else output just the name of the file

    this is what I tried but is probably really wrong

     

      Dim dt As Data.DataTable = New Data.DataTable
            dt.Columns.Add("Position")
            dt.Columns.Add("LineCode")
            dt.Columns.Add("PartNo")
            dt.Columns.Add("Description")
            dt.Columns.Add("Qty")
            dt.Columns.Add("Images")
    
            For Each Row In Ds.Tables(0).Rows
                astring1 = Row.Item(4).ToString
                ' Get all Product File part numbers that match the E-Cat number
    
                Dim DsParts As New Data.DataSet
                Dim Sqlstr3 As String = "SELECT Distinct Product.PartNo, Product.LineCode, Product.EcatNo FROM Product WHERE (Product.ECatNo = ?) "
                Dim da1 As New Data.OleDb.OleDbDataAdapter(Sqlstr3, conn)
                da1.SelectCommand.Parameters.Add("?", Data.OleDb.OleDbType.VarChar).Value = Row.Item(4).ToString
                'Filling the DataSet
                If Ds.Tables(0).Rows.Count > 0 Then
                    da1.Fill(DsParts)
                End If
    
                If DsParts.Tables(0).Rows.Count = 0 Then
                    ListBox1.Items.Add(New ListItem("No Records Found"))
                End If
    
                For Each MyRow In DsParts.Tables(0).Rows
    
                    astring = MyRow.Item(0).ToString
                    'Set up Part String so that #, Description, Position & Quantity always line up                
                    '                                
    
                    Dim dr As Data.DataRow = dt.NewRow
    
                    dr("Position") = Row.Item("position")
                    dr("LineCode") = MyRow.Item("LineCode")
                    dr("PartNo") = astring
                    dr("Description") = Row.Item("partterminologyname")
                    dr("Qty") = Row.Item("Qty")
                    If System.IO.File.Exists(Server.MapPath("~/images1/" & MyRow.Item("ECatNo") & ".jpg")) Then
                        dr("Images") = "~/images1/" & MyRow.Item("ECatNo") & ".jpg"
                    Else
                        dr("Images") = astring
                    End If
                    dt.Rows.Add(dr)
                Next
    
            Next
    
            GridView1.DataSource = dt
            GridView1.DataBind()
     Any help would be great
  • Re: Gridview

    05-16-2008, 12:31 AM
    Answer

    Hi ASPworkedforme ,

    ASPworkedforme:
    I also need to test to see if the image is in the folder or ftp site if it is then output link to the image else output just the name of the file

    See my sample,

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("test");
                DataRow dr = table.NewRow();
                dr["test"] = "notexist.jpg";
                table.Rows.Add(dr);
    
                DataRow dr1 = table.NewRow();
                dr1["test"] = "AJAX.gif";
                table.Rows.Add(dr1);
    
                this.GridView1.DataSource = table;
                GridView1.DataBind();
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string astring = ((DataRowView)e.Row.DataItem)["test"].ToString();
                if (File.Exists(Server.MapPath(astring)))
                {
                    ImageButton btn = e.Row.FindControl("ImageButton1") as ImageButton;
                    btn.ImageUrl = Server.MapPath(astring);
                    btn.Visible = true;
                }
                else
                {
                    Label lbl = e.Row.FindControl("Label1") as Label;
                    lbl.Visible = true;
                }
            }
        }
      
            <asp:GridView ID="GridView1" runat="server" Width="338px" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="ImageButton1" runat="server" Visible="false" />
                            <asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

     

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Gridview

    05-16-2008, 12:41 AM
    Answer
    • Contributor
      3,068 point Contributor
    • blurearc
    • Member since 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 537

    what if there is no image.... and stilli need to have button on the grid...

    so in this case we need to make sure that I mage has to be there... and i belive most of us assume that immages are present on server... why to increase extra overhead of checking..

    and some time we don't get images in rendered page event we have them.. because of internet speeed...

    so i thik there is no such need to to do this.. just make sure you have every thing before deployment... :)

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Consultant I)
    Neudesic
    Hyd'bad
    INDIA
Page 1 of 1 (3 items)