Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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.

C#
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);
           
        }
        
    }
}


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

C#
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 .
Posted
Updated 4-May-12 22:50pm
v7

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900