如何防止DGV隐藏顶行?

时间:2012-10-01 22:42:47

标签: c# winforms datagridview

我的DGV填充了用户输入的值(不是数据绑定的)。行计数是动态的。但是,即使使用默认的五行,从单元格到单元格以及从一行到另一行的选项卡导致第一行(第0行)在我到达最后一个(第五行)/第4行时“向上和向下”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个像素 - 但是,当160像素高的DGV(每行32个像素)中隐藏了5行时,甚至会发生这种令人不安的行为。

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

更新

我发现如果我在设计时将DGV的高度加上2(162而不是160,那么每行为32,并且剩下的DGV有2个像素),它可以使用默认的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]);
    }
}

2 个答案:

答案 0 :(得分:3)

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

简而言之,您只需要:

  1. 致电 FreezeBand (YouDataGridView.Rows [0])
  2. dataGridView.ColumnHeadersVisible = false;
  3. 注意:您甚至可以根据需要冻结乐队的 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());
        }
    }
    

    嗯,这是我最好的拍摄:)

答案 1 :(得分:1)

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