【问题标题】:show Image from datatable in gridview在gridview中显示数据表中的图像
【发布时间】:2016-05-26 16:45:46
【问题描述】:

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

这是从代码中得到的输出

【问题讨论】:

    标签: c# asp.net image gridview


    【解决方案1】:

    您将图像列创建为字符串,它不是字符串。您需要在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>
    

    输出:

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2012-05-27
      • 2012-11-06
      • 1970-01-01
      • 2023-04-11
      • 2012-11-25
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多