WinForms Datagridview change image row at runtime vs 2010
Never used the datagridview before and I cannot figure out how to change myImageColumn depending on status.
I am loading some logs in this grid and again depending on the status I would like to assign the appriopriate image.Not sure which event I should be doing it. Any suggestions or example with bound or unbound would be great.
Here is some code
public enum LogType
{
Fatal,
Error,
Warn,
Info,
Debug,
None,
}
public class Log
{
public LogType LogType { get; internal set; }
public string Message { get; set; }
}
private void LoadDataGrid()
{
// Create the image column.
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
imageCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
imageCol.ImageLayout = DataGridViewImageCellLayout.Normal;
imageCol.Frozen = true;
imageCol.Name = "Image";
imageCol.HeaderText = "";
imageCol.DisplayIndex = 0;
imageCol.Image = Properties.Resources.warning;
datagrid.Columns.Add(imageCol);
DataGridViewTextBoxColumn colMessage = new DataGridViewTextBoxColumn();
colMessage.Name = "Message";
colMessage.HeaderText = "Message";
datagrid.Columns.Add(colMessage);
datagrid.DataSource= GetAllLogs();
}
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//I am not sure about the all 开发者_如何学Cthing here
// Check if it's the Image column.
if ((dgvLogs.Columns[e.ColumnIndex].Name == "Image"))
{
object value = dgvLogs.Rows[e.RowIndex].Cells["LogType"].Value;
//TODO:Convert to enum
// switch (type)
//{
// case "fatal": e.Value=FatalImage;
// case "error": e.Value=ErrorImage;
//case "warn": e.Value=WarnImage;
//case "info": e.Value=InfoImage;
//case "debug": e.Value=DebugImage;
//}
}
Need help with some code here or link where you can see how images are determined at runtime.
thanks a lot
I found a link which maybe helpful for you.
http://www.informit.com/articles/article.aspx?p=446453&seqNum=14
Hth.
精彩评论