1

我的 DGV 填充了用户输入的值(非数据绑定)。行数是动态的。但是,即使使用默认的五行,当我到达最后(第 5 行)/第 4 行时,从一个单元格到另一个单元格和从行到另一行会导致第一行(第 0 行)在 DGV 的“上下”消失。

我希望 5 行(或多行)始终保持在同一个位置,而不是根据光标所在的位置移动。

我不知道为什么这是这种情况下的默认行为,并且对此感到好奇,但主要只需要知道如何防止这种奇怪现象。

当用户添加额外的行时,我会计算每行的高度,如下所示:

dataGridViewPlatypi.Rows.Add();
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
    dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}

...当除法有余数/模数时,这种不精确的数学可能会导致网格底部出现一些“额外空间”,例如:当有六行时,每行的高度为 26 并留下 4 个像素在网格的底部——但同样,当默认的 5 行位于 160 像素高的 DGV(每行 32 像素)中时,甚至会发生这种令人不安的行为。

我不知道我是否需要为DGV设置一些属性或编写一些代码,一行,一个单元格,还是......?

更新

我发现如果我在设计时将 2 添加到 DGV 的高度(162 而不是 160,因此每行是 32 并且剩下 2 个像素“用于 DGV”,它适用于默认的 5 行。

然而,当用户添加额外的行时试图让它调整是有问题的。以下 kludgy 代码不起作用:

// Adding one extra pixel is not enough; 2 is the least that can be added to prevent the last phantom grey row from appearing
const int BASE_GRID_HEIGHT_SHIM = 2;
const int DEFAULT_ROW_COUNT = 5;

dataGridViewPlatypi.Rows.Add();

int shimAdjustment = dataGridViewPlatypi.RowCount - DEFAULT_ROW_COUNT;
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
int newGridHeight = (newRowHeight * dataGridViewPlatypi.RowCount) + BASE_GRID_HEIGHT_SHIM + shimAdjustment;

for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
    dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}

dataGridViewPlatypi.Size = new Size(dataGridViewPlatypi.Width, newGridHeight);

注意:在第 0 列中单击时不会发生向上推行以隐藏第 0 行并显示 DGV 的软灰色下腹部;只有在从一列切换到下一列之后,才会发生这种“向上推动”(FWIW)。

更新 2

这就是我应用丰富答案的方式。

private void buttonAddRow_Click(object sender, EventArgs e)
{
    AddAPlatypusRow();
    for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
    {
        FreezeBand(dataGridViewPlatypi.Rows[i]);
    }
}
4

2 回答 2

3

下面是使用 DataGridView 的 DataGridViewBand 类冻结任何行或列的完整示例。

简而言之,您只需要:

  1. 调用FreezeBand (YouDataGridView.Rows[0])
  2. dataGridView.ColumnHeadersVisible = false;

注意:您甚至可以根据需要冻结乐队的背景颜色。虽然在示例中是 WhiteSmoke(灰色)。

    private static void FreezeBand(DataGridViewBand band)
    {
        band.Frozen = true;
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.WhiteSmoke;
        band.DefaultCellStyle = style;
    }

要查看完整的演示,请查看 Microsoft 的DataGridViewBandDemo

要查看快速演示,请创建一个常规的 Visual Studio、C# Windows 应用程序项目并使用以下代码填充您的 Program.cs 文件。

using System.Drawing;
using System.Windows.Forms;
using System;

public class DataGridViewBandDemo : Form
{
    #region "form setup"
    public DataGridViewBandDemo()
    {
        InitializeComponent();
        InitializeDataGridView();
    }

    DataGridView dataGridView;
    FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();

    private void InitializeComponent()
    {
        FlowLayoutPanel1.Location = new Point(454, 0);
        FlowLayoutPanel1.AutoSize = true;
        FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
        AutoSize = true;
        ClientSize = new System.Drawing.Size(614, 360);
        FlowLayoutPanel1.Name = "flowlayoutpanel";
        Controls.Add(this.FlowLayoutPanel1);
        Text = this.GetType().Name;
    }
    #endregion

    #region "setup DataGridView"

    private void InitializeDataGridView()
    {
        dataGridView = new System.Windows.Forms.DataGridView();
        Controls.Add(dataGridView);
        dataGridView.Size = new Size(300, 200);

        // Create an unbound DataGridView by declaring a
        // column count.
        dataGridView.ColumnCount = 4;

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();
        columnHeaderStyle.BackColor = Color.Aqua;
        columnHeaderStyle.Font =
            new Font("Verdana", 10, FontStyle.Bold);
        dataGridView.ColumnHeadersDefaultCellStyle =
            columnHeaderStyle;

        // Set the column header names.
        dataGridView.Columns[0].Name = "Recipe";
        dataGridView.Columns[1].Name = "Category";
        dataGridView.Columns[2].Name = "Whatever";
        dataGridView.Columns[3].Name = "Rating";

        // Populate the rows.
        string[] row1 = new string[]{"Meatloaf", 
                                        "Main Dish", "boringMeatloaf", "boringMeatloafRanking"};
        string[] row2 = new string[]{"Key Lime Pie", 
                                        "Dessert", "lime juice, evaporated milk", "****"};
        string[] row3 = new string[]{"Orange-Salsa Pork Chops", 
                                        "Main Dish", "pork chops, salsa, orange juice", "****"};
        string[] row4 = new string[]{"Black Bean and Rice Salad", 
                                        "Salad", "black beans, brown rice", "****"};
        string[] row5 = new string[]{"Chocolate Cheesecake", 
                                        "Dessert", "cream cheese", "***"};
        string[] row6 = new string[]{"Black Bean Dip", "Appetizer",
                                        "black beans, sour cream", "***"};
        string[] row7 = new string[]{"Black Bean Dip", "Appetizer",
                                        "black beans, sour cream", "***"};
        string[] row8 = new string[]{"Jelly beans", "Appetizer",
                                        "black beans, sour cream", "***"};
        string[] row9 = new string[]{"Barracuda", "Appetizer",
                                        "black beans, sour cream", "***"};
        object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9 };

        foreach (string[] rowArray in rows)
        {
            dataGridView.Rows.Add(rowArray);
        }

            dataGridView.ColumnHeadersVisible = false; // This hides regular column header
        FreezeFirstRow();
        // FreezeFirstColumn(); // Uncomment this line to freeze first column

    }

    // Freeze the first row.
    private void FreezeFirstRow()
    {
        FreezeBand(dataGridView.Rows[0]);
    }

    private void FreezeFirstColumn()
    {
        FreezeBand(dataGridView.Columns[1]);
    }

    private static void FreezeBand(DataGridViewBand band)
    {
        band.Frozen = true;
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.WhiteSmoke;
        band.DefaultCellStyle = style;
    }

    #endregion

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new DataGridViewBandDemo());
    }
}

好吧,那是我最好的选择:)

于 2012-10-05T09:00:02.833 回答
1

我知道您没有使用数据绑定 DGV,但以下链接之一可能会为您提供有关解决方案的一些见解。

于 2012-10-02T21:57:59.373 回答