المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Accessing Non-Public Members [modified]



C# Programming
09-23-2009, 08:41 PM
I would like to know how can I get access to the Non-Public members of a class. In this case it is the
CellPaintinEventArgs of a DataGridView control that I would like to be able to access outside of the CellPainting event.
I know it's not common practice or even if it's possible, but I would like to call the CellPainting event directly and pass in the DataGrid and CellPaintingEventArgs myself.

Reason being is I am trying to improve the performance (efficiency) of the CellPainting event for the DataGridView control. From a little experimentation, I have discovered that the CellPainting event fires for cells that do not require updating. I am using DataGrids that can take up the entire display of a 19" Wide display and sometimes only a few cells need updating yet depending on the ******** of those cells, many more cells appear to be repainted unnecessarily. This noticeably slows the updating of the grid. The data is not bound to the grid and no rows or columns are added or removed from the grid. In other words the grid size remains static so there is nothing extreme happening that would force the grid to repaint most of its cells.

The code below illustrates this. Use the default names for the components on the form. Also stretch out the grid so there's no need to scroll. Just click on different cells to change the current selected cell and you will notice that the counter reveals how many times the CellPainting event is fired for something as trivial as this! Click on the first cell and then click on the last cell, the counter increments as many times as there are cells on the grid!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dgvTester
{
public partial class Form1 : Form
{
private int counter = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.RowCount = 10;
dataGridView1.ColumnCount = 10;
for (int column = 0; column < dataGridView1.ColumnCount; column++)
dataGridView1.Columns[column].Width = 50;
dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
counter++;
label1.Text = counter.ToString();
}

private void button1_Click(object sender, EventArgs e)
{
counter = 0;
label1.Text = counter.ToString();
}
}
}
Do I have to override the accessibility of the CellPaintingEventArgs to be able to do this?
I found this link, don't know if it will help or how to use it:
http://www.koders.com/csharp/fid1DBCB1E079D023FE239DE7F27C34FAC67DA643AF.aspx?s=cdef%3Adatagridview#L34

Any help is much appreciated!

john....

modified on Wednesday, September 23, 2009 2:22 AM