绑定时动态更改列类型
本文关键字:类型 定时 动态 绑定 | 更新日期: 2023-09-27 18:30:22
我有一个绑定到我的一个绑定适配器的DataGridView
。我的网格中有一列对应于附件的"type"
(即".pdf")。这在网格视图列中显示为文本(如预期的那样)。我希望能够将列的值更改为图像以表示类型。例如,如果类型是 PDF
,我希望列中有PDF
文档的图像,而不是文本".pdf"
。
有没有办法在添加单元格时动态执行此操作?还是想在所有单元格加载后完成?
干杯。
是的,只需使用图像并具有一些具有相应名称的图标。
例如.pdf.png,单词.png
然后像这样构建链接:
<img src="<%# LinkRoot + Eval("type").ToString() + ".png" %>" height="32" width="32" />
你必须
自己在列type
绘制图像,当然绘制的图像对应于text
(描述文件类型,例如:.pdf
,.txt
,...)。您必须自己准备所有图像,如果没有任何未知文件类型的相应图像,则可以使用Unknown file type image
。要在单元格上绘制图像,您必须处理事件CellPainting
,这是您可以尝试的代码:
//Dictionary to store the pairs of `text` and the corresponding image
Dictionary<string, Image> dict = new Dictionary<string, Image>(StringComparer.CurrentCultureIgnoreCase);
//load data for your dict
dict["Unknown"] = yourUnknownImage;//This should always be added
dict[".pdf"] = yourPdfImage;
dict[".txt"] = yourTxtImage;
//.....
//CellPainting event handler for your dataGridView1
//Suppose the column at index 1 is the type column.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
if(e.ColumnIndex == 1 && e.RowIndex > -1){
var image = dict["Unknown"];
if(e.Value != null) {
Image img;
if(dict.TryGetValue(e.Value.ToString(), out img)) image = img;
}
//Draw the image
e.Graphics.DrawImage(image, new Rectangle(2,2, e.Bounds.Height-4, e.Bounds.Height-4));
}
}