我有一Datagrid,重画颜色后,速度巨慢,稍微拉动一下,都要等十几秒钟,看到一个格子一个格子的重画,请问怎样解决这个问题啊(注:数据为50行42列)?相关代码如下:
// 以下为类代码
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
foreBrush, bool alignToRight)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
// Here, we color anything that begins with a letter higher than 'F'
try
{
object o = this.GetColumnValueAtRow(source, rowNum);
if( o!= null)
{
int c = ((int)o);
if( c > 0)
{
backBrush = new SolidBrush(Color.Red);
//foreBrush = new SolidBrush(Color.Black);
}
}
}
catch(Exception ex){ /* empty catch */ }
finally
{
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}// 以下为调用的代码:
public void ApplyGridStyle(DataGrid dg,string tableName)
{
string query = "SELECT * FROM UL_HISTORY_TEMP1 ORDER BY COLOR,COUNT_ALL DESC,COUNT_PART DESC";
DataSet ds = DbHandle.DbCore.GetDataSet(query,tableName);
dg.DataSource = ds;
dg.DataMember = tableName; DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = tableName;
DataGridTableStyle tableStyle = dg.TableStyles[tableName];
//tableStyle.AlternatingBackColor = Color.LightGreen;
//tableStyle.BackColor = Color.LightGray;
int numCols = ds.Tables[tableName].Columns.Count; DataGridColoredTextBoxColumn colorColumnTextColumn ;
//DataGridTextBoxColumn colorColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
colorColumnTextColumn = new DataGridColoredTextBoxColumn();
//colorColumnTextColumn = new DataGridTextBoxColumn();
colorColumnTextColumn.NullText = "";
colorColumnTextColumn.Width = 20;
colorColumnTextColumn.MappingName = ds.Tables[tableName].Columns[i].ColumnName;
switch(i)
{
case 0:
colorColumnTextColumn.Width = 0;
break;
case 1:
colorColumnTextColumn.HeaderText = "颜色";
colorColumnTextColumn.Width = 35;
break;
case 2:
colorColumnTextColumn.HeaderText = "号码";
colorColumnTextColumn.Width= 35;
break;
case 3:
colorColumnTextColumn.HeaderText = "全部次数";
colorColumnTextColumn.Width= 60;
break;
case 4:
colorColumnTextColumn.HeaderText = "38期次数";
colorColumnTextColumn.Width= 60;
break;
case 41:
case 42:
colorColumnTextColumn.Width = 0;
break;
default:
colorColumnTextColumn.HeaderText = (i-4).ToString();
break;
}
dgts.GridColumnStyles.Add(colorColumnTextColumn);
} dg.TableStyles.Clear();
dg.TableStyles.Add(dgts);
dg.Refresh();
}请高手出招了,谢了!!!!!!!!!!!!
// 以下为类代码
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
foreBrush, bool alignToRight)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
// Here, we color anything that begins with a letter higher than 'F'
try
{
object o = this.GetColumnValueAtRow(source, rowNum);
if( o!= null)
{
int c = ((int)o);
if( c > 0)
{
backBrush = new SolidBrush(Color.Red);
//foreBrush = new SolidBrush(Color.Black);
}
}
}
catch(Exception ex){ /* empty catch */ }
finally
{
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}// 以下为调用的代码:
public void ApplyGridStyle(DataGrid dg,string tableName)
{
string query = "SELECT * FROM UL_HISTORY_TEMP1 ORDER BY COLOR,COUNT_ALL DESC,COUNT_PART DESC";
DataSet ds = DbHandle.DbCore.GetDataSet(query,tableName);
dg.DataSource = ds;
dg.DataMember = tableName; DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = tableName;
DataGridTableStyle tableStyle = dg.TableStyles[tableName];
//tableStyle.AlternatingBackColor = Color.LightGreen;
//tableStyle.BackColor = Color.LightGray;
int numCols = ds.Tables[tableName].Columns.Count; DataGridColoredTextBoxColumn colorColumnTextColumn ;
//DataGridTextBoxColumn colorColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
colorColumnTextColumn = new DataGridColoredTextBoxColumn();
//colorColumnTextColumn = new DataGridTextBoxColumn();
colorColumnTextColumn.NullText = "";
colorColumnTextColumn.Width = 20;
colorColumnTextColumn.MappingName = ds.Tables[tableName].Columns[i].ColumnName;
switch(i)
{
case 0:
colorColumnTextColumn.Width = 0;
break;
case 1:
colorColumnTextColumn.HeaderText = "颜色";
colorColumnTextColumn.Width = 35;
break;
case 2:
colorColumnTextColumn.HeaderText = "号码";
colorColumnTextColumn.Width= 35;
break;
case 3:
colorColumnTextColumn.HeaderText = "全部次数";
colorColumnTextColumn.Width= 60;
break;
case 4:
colorColumnTextColumn.HeaderText = "38期次数";
colorColumnTextColumn.Width= 60;
break;
case 41:
case 42:
colorColumnTextColumn.Width = 0;
break;
default:
colorColumnTextColumn.HeaderText = (i-4).ToString();
break;
}
dgts.GridColumnStyles.Add(colorColumnTextColumn);
} dg.TableStyles.Clear();
dg.TableStyles.Add(dgts);
dg.Refresh();
}请高手出招了,谢了!!!!!!!!!!!!
解决方案 »
- C# 自定义组件开发 不能找到DLL dll缓存的位置
- c# 如何禁止修改系统时间win7?
- C#操作临时表
- 能不能用局域网中共享的数据库文件作为数据源
- 请给:新手学C#的一些建议!
- C#编程 实现系统垃圾文件清理
- 超简单的界面库,想看的请进!
- 100分请教, c# WinForm 如何运行时候,改变DataGrid 的字体背景色(依据数据值的不同)
- DevExpress的XtraTreelist控件与ORacle数据绑定问题
- 哪里出了问题,怎么读不出来,估计问题出在provider = SQLOLEDB;Integrated Security = SSPI;
- [白花花的100分啊——求一方法!]
- 高分相送,在线等待。通过Assembly创建实例的问题
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx