2011-09-20 61 views
0

以前從未使用過datagridview,我無法弄清楚如何根據狀態更改myImageColumn。WinForms Datagridview在運行時更改圖像行vs 2010

我在這個網格中加載一些日誌,並根據狀態我想分配appriopriate image.Not不知道我應該做這件事。 任何建議或示例綁定或解除綁定會很好。

下面是一些代碼

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 thing 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; 

     //} 
    } 

需要在這裏的一些代碼或鏈接,你可以看到圖像是如何在運行時確定幫助。

非常感謝

回答