简体   繁体   English

在GridView中显示图像c#asp.net

[英]display image in gridview c# asp.net

I'm trying to display an image in a gridview, I saw other answers and tried them but they don't seem to work for my method. 我试图在gridview中显示图像,我看到了其他答案并尝试了它们,但它们似乎不适用于我的方法。 i fill the gridview using this method i found on aspsnippets. 我使用在assnippetpets上发现的这种方法填充gridview。 my code was modified to fill the gridview from my mongoDB database, which it does, but only with text, this is just a snippet of my code, sorry if it wasn't enough to understand what I'm doing. 我的代码经过修改以填充mongoDB数据库中的gridview,但确实只有文本,这只是我的代码的一部分,如果还不足以了解我在做什么,那么抱歉。 anywhere it says "text" is unimportant, just a column that already fills properly. 它说“文本”不重要的任何地方,只要一列已经正确填充即可。

<asp:GridView ID="GridMultiD" runat="server"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true"  
    PageSize = "10" Caption = "Multi-Dimensional Array" >
  <Columns>
    <asp:BoundField ItemStyle-Width = "150px"
    DataField = "Name" HeaderText = "Name" />
    <asp:BoundField ItemStyle-Width = "150px"
    DataField = "Age" HeaderText = "Age" />
    <asp:BoundField ItemStyle-Width = "150px"
     DataField = "City" HeaderText = "City" />
    <asp:BoundField ItemStyle-Width = "150px"
    DataField = "Country" HeaderText = "Country" />
  </Columns>
</asp:GridView>

C# code C#代码

string[,] arrMultiD = {
                    { "John", "21", "Berlin", "Germany" },
                    { "Smith", "33" ,"London", "UK"},
                    { "Ryder", "15" ,"Sydney", "Australia"},
                    { "Jake", "18", "Tokyo", "Japan"},
                    { "Tom","34" , "Mumbai", "India"}
                 };
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Age", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));    
for (int i = 0; i < 5; i++)
{
    dt.Rows.Add();
    dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 0];
    dt.Rows[dt.Rows.Count - 1]["Age"] = arrMultiD[i, 1];
    dt.Rows[dt.Rows.Count - 1]["City"] = arrMultiD[i, 2];
    dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 3];  
}
GridMultiD.DataSource = dt;
GridMultiD.DataBind(); 

which i modified to fill text in some columns, and what i would have hoped was an image in others. 我将其修改为在某些栏中填充文本,而我希望在其他栏中输入图片。 something like this 像这样的东西

         DataTable dt = new DataTable();
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend1");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend2");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend3");
            dt.Columns.Add("text");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend4");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend5");
            dt.Columns.Add("text");
            dt.Columns.Add("Trend6");
            dt.Columns.Add("text");
            dt.Columns.Add("text");


            int counter = 0;
            foreach (string[] arr in verList)
            {
                dt.Rows.Add();
                if (arr[counter] == "↑")
                {
                    dt.Rows[dt.Rows.Count - 1]["Trend1"] = "<IMG SRC=\"up.jpg\" ALT=\"Up\">";
                }
                else if (arr[counter] == "↓")
                {
                    dt.Rows[dt.Rows.Count - 1]["Trend1"] = "<IMG SRC=\"down.jpg\" ALT=\"Down\">";
                }
                else
                    dt.Rows[dt.Rows.Count - 1]["Trend1"] = "<img src=\"NoChange.jpg\" alt=\"NoChange\" style=\"width:36px;height:36px;\">";
                counter++;
                if (arr[counter] == "↑")
                {
                    dt.Rows[dt.Rows.Count - 1]["Trend2"] = "<img src=\"up.jpg\" alt=\"Up\" style=\"width:36px;height:36px;\">";
                }
                else if (arr[counter] == "↓")
                {
                    dt.Rows[dt.Rows.Count - 1]["Trend2"] = "<img src=\"down.jpg\" alt=\"Down\" style=\"width:36px;height:36px;\">";
                }
                else
                    dt.Rows[dt.Rows.Count - 1]["Trend2"] = "<img src=\"NoChange.jpg\" alt=\"NoChange\" style=\"width:36px;height:36px;\">";
                counter++;
                counter = 0;
            }

            GridMultiD.PageSize = dt.Rows.Count;
            GridMultiD.DataSource = dt;
            GridMultiD.DataBind();

but I just get the HTML code as text in the gridview cell, though I've made sure the image is there so path is correct. 但我只是在Gridview单元格中以文本形式获得了HTML代码,尽管我已确保图像在其中,所以路径是正确的。 I've tried other methods I found that solved some problems for others, but haven't for me. 我尝试了其他方法,这些方法为其他人解决了一些问题,但对我却没有。 Any suggestions would be appreciated! 任何建议,将不胜感激!

You can try to add a template field column to the gridview and add an Image control to this template, then Assign the path of your image to the Image controls property. 您可以尝试将模板字段列添加到gridview并将图像控件添加到此模板,然后将图像的路径分配给“图像控件”属性。 I can't access the code right now but just google how to add template field column to gridview 我现在无法访问代码,只是谷歌如何将模板字段列添加到gridview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM