Skip to main content
 首页 » 编程设计

c#之如何防止 DGV 隐藏顶行

2024年02月20日13cmt

我的 DGV 填充了用户输入的值(非数据绑定(bind))。行数是动态的。但是,即使使用默认的五行,当我到达最后(第 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行。

但是,当用户添加额外的行时试图让它进行调整被证明是有问题的。以下笨拙的代码不起作用:

// 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]); 
    } 
} 

请您参考如下方法:

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

简而言之,你只需要:

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

注意:您甚至可以根据需要卡住 band 的 BackColor。虽然样本中是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()); 
    } 
} 

好吧,这是我最好的机会:)