我在我的asp页面的on-load()函数上绑定数据,我可以检索所有数据,但不幸的是我无法看到图像。我附上了代码,请告诉我如何做到这一点?帮我在网格视图中显示图像。
<asp:GridView ID="GridView2" runat="server">
<AlternatingRowStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#C19E45" ForeColor="White" />
<SelectedRowStyle BackColor="#3333CC" ForeColor="Red" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = client.viewClients();
DataTable newdt = new DataTable();//for filtering data
newdt.Columns.Add("id", typeof(string));
newdt.Columns.Add("Name", typeof(string));
newdt.Columns.Add("Father/Husband", typeof(string));
newdt.Columns.Add("Cnic", typeof(string));
newdt.Columns.Add("Occupation", typeof(string));
newdt.Columns.Add("Present_Address", typeof(string));
newdt.Columns.Add("Telephone", typeof(string));
newdt.Columns.Add("Phone", typeof(string));
newdt.Columns.Add("Email", typeof(string));
newdt.Columns.Add("Permanent_address", typeof(string));
newdt.Columns.Add("Nominee_name", typeof(string));
newdt.Columns.Add("Nominee_address", typeof(string));
newdt.Columns.Add("Nominee_cnic", typeof(string));
newdt.Columns.Add("nominee_no", typeof(string));
newdt.Columns.Add("Image", typeof(string));
newdt.Columns.Add("registeration_no", typeof(string));
foreach (DataRow row in dt.Rows)
{
DataRow nrow = newdt.NewRow(); //creating newRow
// byte[] imgarray = (byte[])row["image"];
// System.Drawing.Image img = client.byteArrayToImage(imgarray);
nrow["id"]=row["id"];
nrow["Name"] = row["name"];
nrow["Father/Husband"] = row["relation_of"];
nrow["Cnic"] = row["applicant_cnic"];
nrow["Occupation"] = row["occupation"];
nrow["Present_Address"] = row["present_address"];
nrow["Telephone"] = row["telephone"];
nrow["Phone"] = row["mobile"];
nrow["Email"] = row["email"];
nrow["Permanent_address"] = row["permanent_address"];
nrow["Nominee_name"] = row["nominee_name"];
nrow["Nominee_address"] = row["nominee_address"];
nrow["Nominee_cnic"] = row["nominee_cnic"];
nrow["nominee_no"] = row["nominee_no"];
nrow["Image"] = row["image"];
nrow["registeration_no"] = row["registeration_no"];
newdt.Rows.Add(nrow);
}
GridView2.DataSource = newdt;
GridView2.DataBind();
}
答案 0 :(得分:0)
您将图像列创建为字符串,它不是字符串。您需要在GridView
控件中创建图像模板,并将模板内的图像绑定到图像的二进制数据:
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
this.BindData();
}
private void BindData()
{
string connString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
var table = new DataTable();
using(var connection = new SqlConnection(connString))
{
using(var command = new SqlCommand("SELECT Name,Image FROM Colour",connection))
{
connection.Open();
var adapter = new SqlDataAdapter(command);
adapter.Fill(table);
connection.Close();
}
}
GridView2.DataSource = table;
GridView2.DataBind();
}
<强> .ASPX:强>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<HeaderTemplate>Image</HeaderTemplate>
<ItemTemplate>
<img src='data:image/jpg;base64,<%# Eval("Image") != System.DBNull.Value ? Convert.ToBase64String((byte[])Eval("Image")) : string.Empty %>' alt="image" height="100" width="200" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<强>输出:强>
答案 1 :(得分:0)