Nishantha
Nishantha

Reputation: 93

How to fill datagridview grayed area using empty rows

I need to know how to fill empty rows in datagridview's rest of grayed area, after datagridview has bind the data. Anyone have a simple method for this please comment here.

screenshot

Upvotes: 3

Views: 2390

Answers (3)

TaW
TaW

Reputation: 54433

Adding Rows to a DataGridView to fill up the space has a number of problems:

  • The most obvious is that you can't add them as your DGV is DataBound. So you would have to add rows to your DataSource. Which is not really nice.

  • Less obvious: The rows would not only look like rows, they would also be real rows and behave like real rows i.e. be clickable, selectable etc.. This is confusing for users even if you prevent editing it will still invite interactions that are not really plausible.

  • Finally: You would still have to take care of the grey areas to the right, at least if there is some or if the users can resize columns.

Here is what I suggest doing instead:

To make the grey area look unobtrusive simply set the DataGridView's BackColor to the color of the normal cells:

 yourDGV.BackgroundColor = yourDGV.DefaultCellStyle.BackColor;

enter image description here enter image description here

Upvotes: 4

SᴇM
SᴇM

Reputation: 7213

Create your Custom DataGridView, I've tested it works kind of glitchy for me on other AutoSizeColumnsMode, so set it to DataGridViewAutoSizeColumnsMode.Fill;, but anyway:

public class GridLineDataGridView : DataGridView
{
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        int rowHeight = this.RowTemplate.Height;

        int h = this.ColumnHeadersHeight + rowHeight * this.RowCount;
        int imgWidth = this.Width - 2;
        Rectangle rFrame = new Rectangle(0, 0, imgWidth, rowHeight);
        Rectangle rFill = new Rectangle(1, 1, imgWidth - 2, rowHeight);
        Rectangle rowHeader = new Rectangle(2, 2, this.RowHeadersWidth - 3, rowHeight);

        Pen pen = new Pen(this.GridColor, 1);

        Bitmap rowImg = new Bitmap(imgWidth, rowHeight);
        Graphics g = Graphics.FromImage(rowImg);
        g.DrawRectangle(pen, rFrame);
        g.FillRectangle(new SolidBrush(this.DefaultCellStyle.BackColor), rFill);
        g.FillRectangle(new SolidBrush(this.RowHeadersDefaultCellStyle.BackColor), rowHeader);

        Bitmap rowImgAAlternative = rowImg.Clone() as Bitmap;
        Graphics g2 = Graphics.FromImage(rowImgAAlternative);
        rFill.X += this.RowHeadersWidth - 1;
        g2.FillRectangle(new SolidBrush(this.AlternatingRowsDefaultCellStyle.BackColor), rFill);

        int w = this.RowHeadersWidth - 1;
        for (int j = 0; j < this.ColumnCount; j++)
        {
            g.DrawLine(pen, new Point(w, 0), new Point(w, rowHeight));
            g2.DrawLine(pen, new Point(w, 0), new Point(w, rowHeight));
            w += this.Columns[j].Width;
        }

        int loop = (this.Height - h) / rowHeight;
        for (int j = 0; j < loop + 1; j++)
        {
            int index = this.RowCount + j;
            if (index % 2 == 0)
            {
                e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight);
            }
            else
            {
                e.Graphics.DrawImage(rowImgAAlternative, 1, h + j * rowHeight);
            }
        }
    }
}

change your private DataGridView dataGridView; to private GridLineDataGridView dataGridView;

Upvotes: 0

Jamshaid K.
Jamshaid K.

Reputation: 4547

Setting the background color to transparent or the same color as your form would be a good thing, but if you don't want to do that, you'll have to create a custom datagridview for that. You should probably check this Thread

Upvotes: 0

Related Questions