我在将图像放入DataTable中并将该表设置为gridview的数据源时遇到了问题,我到处都找过了,但我没有运气。以下是问题所在:
private void populateGrid()
{
// Gets a datatable from a stored procedure
DataTable ragTable = ProjetoBO.HistoricoRAGStatusProjeto(Convert.ToInt32(Session["idProjeto"]));
int group= -1;
int cont = 1;
var table = GetDataTable(ragTable);
DataRow dataRow = table.NewRow();
foreach (DataRow row in ragTable.Rows)
{
cont++;
if (Convert.ToInt32(row[9]) != group)
{
cont = 1;
group= Convert.ToInt32(row[9]);
table.Rows.Add(dataRow);
dataRow = tabelaFinal.NewRow();
}
dataRow[0] = DateTime.Parse(row[2].ToString()).ToShortDateString();
//putting some random image just for testing purpose
dataRow[cont] = Properties.Resources.myIcon;
}
//my grid
histRagStatus.DataSource = table ;
histRagStatus.DataBind();
}
//Creates a dataTable with the columns I need
private DataTable GetDataTable(DataTable ragTable)
{
var newTable= new DataTable();
newTable.Columns.Add("Date");
foreach (DataRow row in ragTable.Rows)
{
if (!newTable.Columns.Cast<DataColumn>().Any(column => column.ColumnName.Equals(row[6].ToString())))
{
var newColumn= new DataColumn(row[6].ToString());
newTable.Columns.Add(newColumn);
}
}
return newTable;
}
我已经尝试了所有可能的方法,创建了一个新列,如var newColumn= new DataColumn(row6.ToString(),typeof(Bitmap));/转换为图像并放入其中/在添加到DataTable之前更改列的数据类型...但是没有运气..。我只需要正确的方式从Properties.Resources获取图像,并将其放在一个DataTable上,这将绑定到一个网格,图像将出现在网格视图上…
现在任何帮助对我来说都是宝贵的=D
发布于 2013-07-03 16:55:22
我不知道有什么方法可以在GridView中原生呈现图像文件(包括BMP)。
在本例中,我要做的是将该位图保存在虚拟目录中的本地文件中。一旦有了文件名,就可以将该文件名绑定到GridView中的ImageField
列。
发布于 2020-08-11 06:06:47
只有在使用DataGridView控件的Windows窗体中,才能在DataTable中存储二进制位图。在ASP.NET DataGrid中,您必须引用图像URL。简单的方法是在您的DataGrid中启用Event RowDataBound。
然后将代码连接到您的图像位置。
protected void GridView1_RowDataBound(object sender, GridViewRowWEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//Setup in your image column index. By Example setting 0
e.Row.Cells[0].Text=Server.HtmlDecode(@"<img src=""./Images/pdf.gif"" />");
}
}
https://stackoverflow.com/questions/17453273
复制