datagridview cellpainting事件 [英] datagridview cellpainting event

查看:134
本文介绍了datagridview cellpainting事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次将项目从列表框拖到datagridview时,我都会使用cellpainting事件.
当我从列表框中拖动项目时,我先前在datagridview中的cellpainting事件消失了.
如何将cellpainting事件保留在datagridview上?

这是我的代码:


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:


public int x;
public int y;





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();

          }
      }





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

推荐答案

为什么每次删除项目时都添加事件处理程序?
仅拥有一个处理程序不是更有意义,尤其是因为您将它们全部置于同一个处理程序方法中.
相反,应在表单加载时或在设计时设置一次处理程序,然后尝试首先设置行和列:
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:
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;
          }
      }




我将课程"从列表框中拖到时间表(datagridview)中.当我像u worte那样进行操作时,datagridview中的上一个单元绘画事件仍然消失,并且仅出现最后一个事件."


这正是我所期望的-这就是您要求它执行的操作.
如果还想保留以前的单元格,则需要用列表(或其他集合)替换"x"和"y"以容纳要突出显示的单元格:




"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:

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


这篇关于datagridview cellpainting事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆