3
我已經使用實體框架從標準Northwind數據庫創建了一個模型。 現在我想填充一個類別列表Gridview,但在該類別實體(表)也是一個圖片以及。 GridView獲取了Id,Description和CategoryName,但是我沒有在gridview列表中獲取二進制數據的圖片。從實體框架的數據庫加載圖像到gridview中
任何人都知道這個解決方案嗎?
謝謝。
我已經使用實體框架從標準Northwind數據庫創建了一個模型。 現在我想填充一個類別列表Gridview,但在該類別實體(表)也是一個圖片以及。 GridView獲取了Id,Description和CategoryName,但是我沒有在gridview列表中獲取二進制數據的圖片。從實體框架的數據庫加載圖像到gridview中
任何人都知道這個解決方案嗎?
謝謝。
你可以像這樣......
你要添加新的通用處理器-------右擊Solution Explorer中增加新的通用處理程序並將其命名爲「ImageHandler.ashx 」
注意:這是關於如何加載從數據庫和顯示圖像中的GridView
只有樣品例如,這是在imagehandler.ashx代碼
<%@ WebHandler Language="C#" %>
using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
HttpRequest req = context.Request;
// string categoryID = "1";
string categoryID = req.QueryString["CategoryID"].ToString();
// Get information about the specified category
NorthwindDataContext db = new NorthwindDataContext();
var category = from c in db.Categories
where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
select c.Picture;
int len = category.First().Length;
// Output the binary data
// But first we need to strip out the OLE header
int OleHeaderLength = 78;
int strippedImageLength = len - OleHeaderLength;
byte[] imagdata = new byte[strippedImageLength];
Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);
if ((imagdata) != null)
{
MemoryStream m = new MemoryStream(imagdata);
System.Drawing.Image image = System.Drawing.Image.FromStream(m);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
和在Default.aspx頁面添加新的GridView控件使用SQLDataSource控件
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:TemplateField HeaderText="Picture" SortExpression="Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?CategoryID="+ Eval("CategoryID") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>
</div>
綁定它,我希望它會幫助你......
它在做什麼你問它做什麼,你」最有可能不會得到實際的圖像,或者如果是你不告訴gridview渲染圖像。你使用的是WPF還是Winforms? –
曾經試過谷歌?有很多關於gridviews圖像的文章。 EF作爲加載字節數組的一種方式只是一個細節。 –
這是這樣的一個副本:http://stackoverflow.com/questions/1573287/gridview-image-gallery-from-binary-data-images如果您使用EF加載類型爲byte []的成員的類,那麼沒有真正做出太大的差別 –