简体   繁体   中英

DataGridView CellFormatting Event Not Firing

I've added a handler for the CellFormatting event on a DataGridView to modify the background color based on the content of the row.

It doesn't seem to be firing even as data gets inserted into the table. I added the event handler by doubleclicking in the IDE on the CellFormatting event which seemed to create the code properly.

   private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        // this never gets called
        MessageBox.Show("Event fired");
    }

What could I be doing wrong?

You could try the RowValidated event:

 private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
 {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
 }

NOTE: This event will fire when you click on rows and when you close the form.

I think you cannot use CellFormating event for your case. It occurs when the contents of a cell need to be formatted for display.

Try CellValueChanged event instead (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)

Or

Select other appropriate event from http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx

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