Hi I have a datagridview in my winform application and I have 2events related to it.
one is Cellformatting which is formatting a row in datagridview as per different values.
and another is UserdeletedRow event which allows to delete a particular row.
but there is a conflict as I have checked,I am not allowed to delete a record as cellformatting event shows error"object not set to instance" .
If i do not use cellformatting event then UserdeletedRow event works fine.
But I want to keep both events.what I require to do.
I used CellEndEdit event and remove handler for cellformatting then also
row is not getting deleted.I do not get any error message.
which event has to be used so there is no conflict?

Recommended Answers

All 2 Replies

Do you have a code snippet we can look at to help troubleshoot?

Do you have a code snippet we can look at to help troubleshoot?

i have tried to remove handler on both events for removing conflicts but still can not delete record with event.If cellformatting event is not there then it deletes the row.
code:

private void StdExamResultGrid_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            this.StdExamResultGrid.CellFormatting -= new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.StdExamResultGrid_CellFormatting);

             StdExamResultGrid.AllowUserToAddRows = true;
            if ((!e.Row.IsNewRow))
            {
                DialogResult response = MessageBox.Show("Are you sure you want to delete this Receipt?", "Delete  Receipt Entry?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if ((response == DialogResult.Yes))
                {
                    e.Row.Cells.Clear();
                    myadap.Update(ds, "ExamResultTbl");
                    StdExamResultGrid.Refresh();
                }
                }
        }


//for formatting cell when viewed the data from database as per cell value
  private void StdExamResultGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            this.StdExamResultGrid.UserDeletedRow -= new System.Windows.Forms.DataGridViewRowEventHandler(this.StdExamResultGrid_UserDeletedRow);

            foreach (DataGridViewRow rw in StdExamResultGrid.Rows)
            {

                if (rw.Cells[6].Value.ToString() == "Fail")
                {
                    rw.DefaultCellStyle.BackColor = System.Drawing.Color.Gray;
                    rw.DefaultCellStyle.ForeColor = System.Drawing.Color.White;

                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.