Jump to content

Binding a datagrid so that images use a path from a database


Guest Kevin Gwyther

Recommended Posts

Guest Kevin Gwyther

--------------------------------------------------------------------------------What I'm trying to do is set up a column in the datagrid to take an initial path + the remaining path from the dataset. I had done this in Visual Studio 2003 using VB, but am trying to convert it to C# for the purpose of expanding knowledge and because the client wanted it in C#.This is my code on the HTML page pertaining to the datagrid (The datagrid is only going to be a side by side grid of images that are to be clicked to provide a link to the companies website). The code inside the image tag is based on converting what I had done in VB (as opposed to C#) into what I thought would work here. There are no errors, but it doesn't seem to be pulling the information from the datasource.

<asp:DataGrid runat="server" ID="dgCompany"><Columns><asp:TemplateColumn><ItemTemplate><asp:ImageButton runat="server" ID="imgCompanyLogo" ImageUrl='<%#"/YHIS/Company" + (Container.DataItem = "ClientLogoLocation")%>' /></ItemTemplate></asp:TemplateColumn></Columns></asp:DataGrid>

Here is the Backend code, very simply, DataGet is a class I made that just cuts off a few lines of code (the data adapter and setting the datasource up based on the sqlCommand given)

protected void Page_Load(object sender, EventArgs e){DataSet DS = new DataSet();DS = DataGet.getDataSet("SELECT ClientLogoLocation FROM tblClient");if (!Page.IsPostBack){dgCompany.DataSource = DS;dgCompany.DataBind();}else{dgCompany.DataSource = DS;}}

Fairly simply, I would greatly appreciate any advice or if I'm missing something blatantly obvious on this matter. Or if you have a different way for me to set it so that the images from the path fill the datagrid that's fine. I am sure I could do it in the backend code but I believe that would be a lot more intensive and require the use of a for loop.

Link to comment
Share on other sites

--------------------------------------------------------------------------------What I'm trying to do is set up a column in the datagrid to take an initial path + the remaining path from the dataset. I had done this in Visual Studio 2003 using VB, but am trying to convert it to C# for the purpose of expanding knowledge and because the client wanted it in C#.This is my code on the HTML page pertaining to the datagrid (The datagrid is only going to be a side by side grid of images that are to be clicked to provide a link to the companies website). The code inside the image tag is based on converting what I had done in VB (as opposed to C#) into what I thought would work here. There are no errors, but it doesn't seem to be pulling the information from the datasource.
<asp:DataGrid runat="server" ID="dgCompany"><Columns><asp:TemplateColumn><ItemTemplate><asp:ImageButton runat="server" ID="imgCompanyLogo" ImageUrl='<%#"/YHIS/Company" + (Container.DataItem = "ClientLogoLocation")%>' /></ItemTemplate></asp:TemplateColumn></Columns></asp:DataGrid>

Here is the Backend code, very simply, DataGet is a class I made that just cuts off a few lines of code (the data adapter and setting the datasource up based on the sqlCommand given)

protected void Page_Load(object sender, EventArgs e){DataSet DS = new DataSet();DS = DataGet.getDataSet("SELECT ClientLogoLocation FROM tblClient");if (!Page.IsPostBack){dgCompany.DataSource = DS;dgCompany.DataBind();}else{dgCompany.DataSource = DS;}}

Fairly simply, I would greatly appreciate any advice or if I'm missing something blatantly obvious on this matter. Or if you have a different way for me to set it so that the images from the path fill the datagrid that's fine. I am sure I could do it in the backend code but I believe that would be a lot more intensive and require the use of a for loop.

Hai , Here i am sending some sample code.Retriving images from database to datagird by appropriate path. SqlConnection conn = new SqlConnection("Data Source=sqlserver;Initial Catalog=OmArtPrint;Integrated Security=True"); DataSet ds = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindGrid(); } } private void bindGrid() { cid1 = Request.QueryString["cid"].ToString(); try { conn.Open(); String sqlstmt = "select pid,ProductList,Price,ProdDesc,Prodimglink from productlist SqlDataAdapter myda = new SqlDataAdapter(sqlstmt, conn); myda.Fill(ds); datagrid1.DataSource = ds; datagrid1.DataBind(); conn.Close(); } catch (Exception ex) { Response.Write(ex.Message.ToString()); }And Html Code <asp:DataGrid ID="datagrid1" runat ="server" AutoGenerateColumns ="False" Width="557px" CellPadding="4" ForeColor="#333333" GridLines="None" Height="147px" style="color: black" AllowPaging="True" HorizontalAlign="Center" > <Columns > <asp:TemplateColumn HeaderText ="details"> <ItemTemplate > <img src='<%#DataBinder.Eval(Container.DataItem, "Prodimglink")%>' ID="ProdImage" name="1" runat=server /> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField ="Pid" HeaderText ="Pid"></asp:BoundColumn> <asp:BoundColumn DataField ="ProductList" HeaderText ="ProductLIst"></asp:BoundColumn> <asp:BoundColumn DataField ="Price" HeaderText ="Price"></asp:BoundColumn> <asp:BoundColumn DataField ="ProdDesc" HeaderText ="ProductDesc"></asp:BoundColumn> </Columns> </asp:DataGrid>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...