Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use cellpainting event every time i drag item from my listbox to datagridview.
when i drag item from the listbox my previous cellpainting event in datagridview disappears.
How can i keep the cellpainting events on the datagridview?

here is my code:


C#
public int x;
public int y;


C#
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
      {
          Point p = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
          DataGridView.HitTestInfo info = this.dataGridView1.HitTest(p.X, p.Y);

          if (info.RowIndex != -1 && info.ColumnIndex != -1)
          {
              Object value = (Object)e.Data.GetData(typeof(string));
              this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = value;

              x = info.RowIndex;
              y = info.ColumnIndex;

                  dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

              dataGridView1.Invalidate();

          }
      }


C#
protected void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
       {

           if (e.RowIndex == x && e.ColumnIndex >= y && e.ColumnIndex < y + 4)
           {
               e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
               e.PaintContent(e.CellBounds);
               e.Handled = true;
           }
       }
Posted
Updated 28-Apr-12 4:28am
v2
Comments
[no name] 28-Apr-12 10:03am    
What have you tried? What errors are you getting? Where is the code that demonstrates your problem?

1 solution

Why are you adding an event handler every time you drop an item?
Wouldn't it make more sense to have just the single one handler, particularly since you are putting them all to the same handler method.
Instead, set up the handler once, in form load, or at design time, and try setting the row and column first:
C#
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
      {
          Point p = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
          DataGridView.HitTestInfo info = this.dataGridView1.HitTest(p.X, p.Y);
          if (info.RowIndex != -1 && info.ColumnIndex != -1)
          {
              Object value = (Object)e.Data.GetData(typeof(string));
              x = info.RowIndex;
              y = info.ColumnIndex;
              this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = value;
          }
      }



"i drag Courses from the listbox into timetable(datagridview). when i did it like u worte, still the previous cellpainting event in datagridview disappears and only last event appears."


Which is exactly what I would expect - that is what you have asked it to do.
If you want to preserve previous cells as well, you need to replace your "x" and "y" with a list (or other collection) to hold the ones you want to highlight:
C#
  private List<Point> makeRed = new List<Point>();
  private void dataGridView1_DragDrop(object sender, DragEventArgs e)
  {
      Point p = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
      DataGridView.HitTestInfo info = this.dataGridView1.HitTest(p.X, p.Y);
      if (info.RowIndex != -1 && info.ColumnIndex != -1)
      {
          Object value = (Object)e.Data.GetData(typeof(string));
          makeRed.Add(new Point(e.RowIndex, e.ColumnIndex));
          this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = value;
      }
  }
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (makeRed.Contains(new Point(e.RowIndex, e.ColumnIndex)))
    {
        e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
        e.PaintContent(e.CellBounds);
        e.Handled = true;
    }
}
 
Share this answer
 
v2
Comments
yehiad 28-Apr-12 11:02am    
i drag Courses from the listbox into timetable(datagridview). when i did it like u worte, still the previous cellpainting event in datagridview disappears and only last event appears.
OriginalGriff 28-Apr-12 11:45am    
Answer updated
yehiad 28-Apr-12 13:03pm    
line: makeRed.Add(new Point(e.RowIndex, e.ColumnIndex));

Error: DragEventArgs not contain a definition for e.RowIndex and e.ColumnIndex
yehiad 28-Apr-12 13:07pm    
it's work with: makeRed.Add(new Point(info.RowIndex, info.ColumnIndex));

thank's
OriginalGriff 28-Apr-12 15:12pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900