2013-10-11 73 views
3

我有一個在gridview中的圖庫,顯示來自數據庫的圖片。Asp.net的gridview水平佈局

我需要在水平佈局中顯示gridview。

現在照片是從上到下排列的, 我該如何更改gridview,以便它將在一行中顯示從左到右的圖片?

HTML代碼:

<asp:GridView ID="GridView3" class="GridView1" runat="server" Width="400px" AutoGenerateColumns="False" Height="90px" 
        CellPadding="4" GridLines="Horizontal"> 
     <Columns> 
      <asp:ImageField DataImageUrlField="Image_path" DataImageUrlFormatString="~/Pic/{0}" HeaderText="Images"> 
       <ControlStyle Height="100px" Width="100px" /> 
      </asp:ImageField>    

     </Columns> 
</asp:GridView> 

C#代碼:

con.Open(); 

    cmd = new SqlCommand("select image_path from tblImages", con); 
    da = new SqlDataAdapter(cmd); 
    dt = new DataTable(); 
    da.Fill(dt); 
    GridView3.DataSource = dt; 
    GridView3.DataBind(); 

con.Close();

回答

4

使用DataListRepeatDirection水平代替。

因爲您想要顯示單個列,所以可以工作。

<asp:DataList id="Images" 
     RepeatDirection="Horizontal" 
     RepeatLayout="Table" 
     RepeatColumns="0" runat="server"> 
    <HeaderStyle BackColor="#aaaadd"> 
    </HeaderStyle> 
    <AlternatingItemStyle BackColor="Gainsboro"> 
    </AlternatingItemStyle> 
    <HeaderTemplate> 
     Images ... 
    </HeaderTemplate> 
    <ItemTemplate> 

     <asp:Image id="ProductImage" 
      AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>' 
      ImageUrl='<%# Eval("image_path","~/Images/{0}") %>' /> 
      runat="server"/> 

    </ItemTemplate> 

</asp:DataList> 
+0

感謝您的代碼,我把在「imageValue時」我的表分貝IMAGE_PATH列, 但圖片是空白出於某種原因,我應該放什麼「 ImageValue「? ImageUrl ='<%#DataBinder.Eval(Container.DataItem,「ImageValue」)%>' –

+0

你看了html的源代碼嗎?圖像的「src」是什麼?它們是正確的嗎? –

+0

@GiladAdar:在瀏覽器中單擊鼠標右鍵並選擇_View Source_。然後搜索ID並查看ASP.NET生成的內容。 –

0

使用此

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"> 
       </asp:DetailsView>