简体   繁体   中英

Trying to add column to dataGridView1 but getting exception InvalidCastException

dataGridView1 is added in the designer. And in the designer i added 9 columns.

Then in the constructor in form1 i did:

dataGridView1.Columns.Add(new DataGridViewImageColumn());
dataGridView1.Rows.Add(2);
DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[1].Cells[1];

Then in the Form1 Load event i did:

_thisProcess = Process.GetCurrentProcess().Id;
InitializeRefreshTimer();
PopulateApplications();

Then the timer init method:

void InitializeRefreshTimer()
        {
            _refreshTimer = new System.Timers.Timer(5000);
            _refreshTimer.SynchronizingObject = this;
            _refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);    
            _refreshTimer.Start();
        }

Then the timer elapsed event:

void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            PopulateApplications();
        }

Then the PopulateApplications method:

void PopulateApplications()
        {
            dataGridView1.Rows.Clear();            
            foreach (Process p in Process.GetProcesses())
            {
                if (p.MainWindowTitle.Length > 1)
                {
                    var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                    Image ima = icon.ToBitmap();
                    String status = p.Responding ? "Running" : "Not Responding";
                    dataGridView1.Rows.Add(p.MainWindowTitle, status);
                    cell.Value = ima;
                }
            }
        }

What i want to do is to add to the first column in dataGridView1 all the icons for each process in the cell under the first column. And the icons should be display only in the first column cells icon of each process.

I tested it i saved the variable ima to the hard disk and i saw the icons there as Images. But i can't find the way to add the icons(Images) to the dataGridView1.

And also i'm getting exception on the line:

DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[1].Cells[1];

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewImageCell'.

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewImageCell'.
  Source=HardwareMonitoring
  StackTrace:
       at HardwareMonitoring.Form1..ctor() in Form1.cs:line 132
       at HardwareMonitoring.Program.Main() inProgram.cs:line 17
  InnerException: 

It seems that the column type is not set right:

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell'

When you add new columns to your DataGridView the default selected type is DataGridViewTextBoxColumn , which results in DataGridViewTextBoxCell -type cells. Edit the columns in the designer and try switching the ColumnType to DataGridViewImageColumn . Your current cells are only for text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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