winform datagridview 显示内容重叠,很久了没不到解决方法!

ken_flash 2009-10-21 10:05:49
首先有个Datagridview,每页显示20行数据,可能有50多页

在bindcompleting事件中我设置了个别列得显示(用图片代替文字显示)
在rowprepaint中根据不同值设置了行的背景色(ColorDialog中最基本的那些颜色)

在翻页的过程中,比如第二行背景色连续很多页都是浅蓝色,那么显示的内容就重叠一起,除非鼠标去点击,但是点击后背景色就变成SelecetdRow的背景色,焦点离开后没有变后要求的颜色(浅蓝色)。

目前只是个别颜色是这样,其他的颜色不知道是翻页不够还是。。。


希望大家帮忙搞定一下,处理过这样问题的朋友,分享下经验


代码如下
#region 绑定数据后重画行背影色
private void gvRecd_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
foreach (DataGridViewRow row in gvRecd.Rows)
{
CaseRecordEntity recd = (CaseRecordEntity)row.DataBoundItem;



#region 根据对方通话类型控制背景色
switch (recd.OppType)
{
case OppType.1:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_ServiceNum);
break;
case OppType.2:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_CommonNum);
break;
case OppType.3:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_CtManNum);
break;
case OppType.4:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_BlacklistNum);
break;
case OppType.5:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_MntedNum);
break;
case OppType.6:
row.DefaultCellStyle.BackColor = Color.FromArgb(SysRunTime.CurrentUserSettings.AlarmColor_MntingNum);
break;
}
#endregion

}
}

private void gvRecd_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in gvRecd.Rows)
{
CaseRecordEntity recd = (CaseRecordEntity)row.DataBoundItem;

//显示对象姓名
row.Cells["TagtNameCol"].Value = SysRunTime.CurrentUserTagtDic[recd.TargetID];


#region 显示呼叫方向/数据类型的图片
switch (recd.CallDirection)
{
case CallDirection.主叫:
row.Cells["CallDircCol"].Value = global::NsaNet.Operator.Properties.Resources.calling;
break;
case CallDirection.被叫:
row.Cells["CallDircCol"].Value = global::NsaNet.Operator.Properties.Resources.called;
break;
}

row.Cells["DataTypeCol"].Value=SysRunTime.GetRecdDataTypeImg(recd.DataType);
#endregion
}
}
#endregion




...全文
752 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken_flash 2009-10-21
  • 打赏
  • 举报
回复
ken_flash 2009-10-21
  • 打赏
  • 举报
回复
void DGV_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
DataGridView DGV = (DataGridView)sender;
int i = Convert.ToInt32(DGV.Rows[e.RowIndex].Cells[0].Value) % 3;

switch (i)
{
case 0: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(-8323073); break;
case 1: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(0); break;
case 2: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(-16744320); break;
//case 3: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(-32704); break;
//case 4: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(-32768); break;
}
}





ken_flash 2009-10-21
  • 打赏
  • 举报
回复
我试下你的代码
wartim 2009-10-21
  • 打赏
  • 举报
回复
没有你说的问题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication100
{
public partial class Form1 : Form
{
int PageIndex = 0;
DataGridView DGV = new DataGridView();

public Form1()
{
InitializeComponent();

DGV.Parent = this;
DataGridViewTextBoxColumn Column = new DataGridViewTextBoxColumn();
Column.HeaderText = "c1";
Column.Name = "c1";
Column.DataPropertyName = "c1";
DGV.Columns.Add(Column);
DGV.RowPrePaint += new DataGridViewRowPrePaintEventHandler(DGV_RowPrePaint);
DGV.DataSource = GetDateTable(PageIndex);
DGV.AllowUserToAddRows = false;

Button BtnPriorPage = new Button();
BtnPriorPage.Parent = this;
BtnPriorPage.Text = "上一页";
BtnPriorPage.Click += new EventHandler(BtnPriorPage_Click);
BtnPriorPage.Location = new Point(0, 200);

Button BtnNextPage = new Button();
BtnNextPage.Parent = this;
BtnNextPage.Text = "下一页";
BtnNextPage.Click += new EventHandler(BtnNextPage_Click);
BtnNextPage.Location = new Point(100, 200);
}

void BtnNextPage_Click(object sender, EventArgs e)
{
DGV.DataSource = GetDateTable(PageIndex + 1);
}

void BtnPriorPage_Click(object sender, EventArgs e)
{
DGV.DataSource = GetDateTable(PageIndex - 1);
}

DataTable GetDateTable(int NewPageIndex)
{
DataTable DT = new DataTable();

this.PageIndex = NewPageIndex < 0 ? 0 : NewPageIndex > 30 ? 30 : NewPageIndex;

DT.Columns.Add("c1", typeof(int));
for (int i = this.PageIndex * 3; i < (this.PageIndex + 1) * 3; i++)
DT.Rows.Add(new Object[] { i });

return DT;
}

void DGV_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
DataGridView DGV = (DataGridView)sender;
int i = Convert.ToInt32(DGV.Rows[e.RowIndex].Cells[0].Value) % 5;

switch (i)
{
case 0: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; break;
case 1: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Green; break;
case 2: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue; break;
case 3: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow; break;
case 4: DGV.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; break;
}
}
}
}
ken_flash 2009-10-21
  • 打赏
  • 举报
回复
我再找找吧

实际使用时连续10+页都是同一颜色这个情况比较少。。但做程序有BUG不改掉,总做的不爽
足球中国 2009-10-21
  • 打赏
  • 举报
回复
因为没有代码看到的只能有这么多。
我们在这边根本没法有你那边的调试环境。

从现在的代码看,还是看不出问题的。
ken_flash 2009-10-21
  • 打赏
  • 举报
回复
刚测试时又发现了奇怪的事。。。

当前页没有重叠,下一页会重叠显示的话,按回到前一页 它是不重叠的(大约10页后就会发生重叠的现象)
yanm7788 2009-10-21
  • 打赏
  • 举报
回复
Up个...
ken_flash 2009-10-21
  • 打赏
  • 举报
回复
CaseRecordEntity

很简单的一个实体类,

“RowPrePaint 这个事件本身就是要绘制的行了。你干嘛又在循环所有的行。”

这个大意了,我改下。
足球中国 2009-10-21
  • 打赏
  • 举报
回复
同意楼上r楼上
足球中国 2009-10-21
  • 打赏
  • 举报
回复
CaseRecordEntity
这个类能发一下嘛??

RowPrePaint 这个事件本身就是要绘制的行了。你干嘛又在循环所有的行。
从你的绘制代码看没啥问题。


foreach (DataGridViewRow row in dataGridView1.Rows)
{
int i = row.Index % 6;



#region 根据对方通话类型控制背景色
switch (i)
{
case 1:
row.DefaultCellStyle.BackColor = Color.Red;
break;
case 2:
row.DefaultCellStyle.BackColor = Color.Yellow;
break;
case 3:
row.DefaultCellStyle.BackColor = Color.Wheat;
break;
case 4:
row.DefaultCellStyle.BackColor = Color.YellowGreen;
break;
case 5:
row.DefaultCellStyle.BackColor = Color.Tomato;
break;
case 6:
row.DefaultCellStyle.BackColor = Color.SteelBlue;
break;
}
#endregion

}


因为没有实际的东西。只能这样试一下。没问题。
ncjcz 2009-10-21
  • 打赏
  • 举报
回复
把代码放上来,改起来方便点
足球中国 2009-10-21
  • 打赏
  • 举报
回复
最好能把代码贴出来一点。
是不是你重绘的事件搞错了。

行重会用这个事件RowPrePaint

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧