答案 0 :(得分:3)
将Rows
添加到DataGridView
以填补空间有很多问题:
最明显的是您无法添加它们,因为您的DGV为DataBound
。因此,您必须向DataSource
添加行。哪个不太好。
不太明显:行不仅看起来像行一样,它们 实际行行为就像实行一样即可点击,可选择等。即使您阻止编辑,这对用户来说也会让人感到困惑,但仍然会引起不真实合理的互动。
最后:您仍然需要处理右侧的灰色区域,至少如果有一些或者用户可以调整列的大小。
以下是我建议做的事情:
要使灰色区域看起来不引人注目,只需将DataGridView
' s BackColor
设置为正常细胞的颜色:
yourDGV.BackgroundColor = yourDGV.DefaultCellStyle.BackColor;
答案 1 :(得分:0)
将背景颜色设置为透明或与表单颜色相同将是一件好事,但如果您不想这样做,则必须为此创建自定义数据网格视图。您应该查看此Thread
答案 2 :(得分:0)
创建自定义DataGridView
,我已经测试过它在其他AutoSizeColumnsMode
上对我有点麻烦,所以将它设置为DataGridViewAutoSizeColumnsMode.Fill;
,但无论如何:
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);
}
}
}
}
将您的private DataGridView dataGridView;
更改为private GridLineDataGridView dataGridView;