2016-05-26 86 views
0

我已經綁定了asp頁面的on-load()函數的數據,我可以檢索所有數據,但不幸的是我無法看到圖像。我附上了代碼,請給我一些想法,我該怎麼做?幫助我在網格視圖中顯示圖像。在gridview中顯示來自datatable的圖像

<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(); 
} 

這是輸出從代碼 enter image description here

回答

0

你必須作爲一個字符串創建的圖像列獲得,它不是一個string.You需要在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> 

輸出:

Showing images in grid view