更改DataGridView行显示方式. [英] Changing the DataGridView Rows Display Way.
问题描述
你好朋友.
(WinForm)
我需要在Columns标头的行和datagridview的第一行之间创建一个自定义空间.在C#2010中(Win Form).这样我就可以在创建的空间中放置一些控件.
我创建了一个新类(从DataGridViewRow继承),并重写了Base类的OnPaint()方法.但是它只会向下移动行,而当我单击向下移动的行之一(在运行时在DataGridView中)时,它永远不会以正确的格式和颜色显示行.
Hi friend.
(WinForm)
I need to create a custom space between Columns header''s row and the first row of the datagridview . in C# 2010 (Win Form) . so that i''ll be able to put some controls in the created space .
I create a new Class (inherited from DataGridViewRow) and overrided the OnPaint() method of the Base class . But it only Shift the rows Down and when i click on one of the shifted down rows (In DataGridView at run time) it never display the row in correct format and colors.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Collections;
using System.Reflection;
namespace DRC.Controls
{
class Class1Row : DataGridViewRow
{
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
{
rowBounds = new Rectangle(rowBounds.X, rowBounds.Y+22, rowBounds.Width, rowBounds.Height);
clipBounds = new Rectangle(clipBounds.X, clipBounds.Y + 22, clipBounds.Width, clipBounds.Height);
base.Paint(graphics, clipBounds, rowBounds, rowIndex, rowState, isFirstDisplayedRow, isLastVisibleRow);
}
}
}
这是我的工作和我所需要的图片.
http://hospitalaria.ir/Resource/UploadFiles/Gallery/orginal/eq5ogcht.ufd.jpg
这是我的问题以及需要解决的问题的图片.
http://hospitalaria.ir/Resource/UploadFiles/Gallery/orginal/pies2y5t.54d.jpg
然后在我的DataGridView中
我写了这段代码
This is the picture of my work and what i need .
http://hospitalaria.ir/Resource/UploadFiles/Gallery/orginal/eq5ogcht.ufd.jpg
This is the picture of my problem and what i need to be solved .
http://hospitalaria.ir/Resource/UploadFiles/Gallery/orginal/pies2y5t.54d.jpg
And in My owen DataGridView
I wrote this codes
public partial class DRCDataGridView : DataGridView
{
public DRCDataGridView()
{
InitializeComponent();
RowTemplate = new Class1Row();
}
public DRCDataGridView(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
Rectangle ClipRectangle = new Rectangle(pe.ClipRectangle.X, pe.ClipRectangle.Y + 22, pe.ClipRectangle.Width, pe.ClipRectangle.Height);
PaintEventArgs p = new PaintEventArgs(pe.Graphics, ClipRectangle);
base.OnPaint(pe);
}
protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
{
e.ClipBounds = new Rectangle(e.ClipBounds.X, e.ClipBounds.Y + 22, e.ClipBounds.Width, e.ClipBounds.Height);
base.OnRowPrePaint(e);
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
e.ClipBounds = new Rectangle(e.ClipBounds.X, e.ClipBounds.Y + 22, e.ClipBounds.Width, e.ClipBounds.Height);
base.OnRowPostPaint(e);
}
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
base.OnCellBeginEdit(e);
}
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
Rectangle Clipbound = new Rectangle(e.ClipBounds.X,e.ClipBounds.Y+22,e.ClipBounds.Width,e.ClipBounds.Height);
Rectangle CellBound = new Rectangle(e.CellBounds.X, e.CellBounds.Y+22, e.CellBounds.Width, e.CellBounds.Height);
DataGridViewCellPaintingEventArgs k = new DataGridViewCellPaintingEventArgs(this, e.Graphics, Clipbound, CellBound, e.RowIndex, e.ColumnIndex, e.State, e, e, "", e.CellStyle, e.AdvancedBorderStyle, e.PaintParts);
base.OnCellPainting(k);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
}
protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
{
base.OnColumnAdded(e);
}
}
请帮助.
Please Help .
推荐答案
您也可以使用标题行单元格来添加控件.
像这样的东西:
You can use headers row cells to add controls as well.
Something like this:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBound += new EventHandler(GridView1_DataBound);
GetData();
}
static void GridView1_DataBound(object sender, EventArgs e)
{
var gw = ((GridView)sender);
foreach (TableCell cell in gw.HeaderRow.Cells)
{
var l = new Literal()
{
Text = "<div>" + cell.Text + "<div>"
};
var b = new Button() { Text = cell.Text };
cell.Controls.Add(l);
cell.Controls.Add(b);
}
}
private void GetData()
{
var dt = new DataTable();
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Salary", typeof(float));
dt.Columns.Add("Home Office Employee", typeof(bool));
dt.Rows.Add(new object[] { "Balmer", 65, 100000, true });
dt.Rows.Add(new object[] { "Gates", 58, 200000, false });
dt.Rows.Add(new object[] { "Black", 30, 50000, true });
dt.Rows.Add(new object[] { "Chu", 34, 55000, true });
dt.Rows.Add(new object[] { "Maria", 24, 70000, false });
this.GridView1.DataSource = dt;
GridView1.DataBind();
}
</div></div>
它不是一个优雅的代码,只是一个方向
it''s not an elegant code, just a direction
这篇关于更改DataGridView行显示方式.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!