在datagridview中冻结顶行和前两列

在datagridview中冻结顶行和前两列

本文介绍了在datagridview中冻结顶行和前两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,我试图实现以下目的:
1.在垂直滚动时应该冻结顶行。
2.前两列应该被冻结,可以水平滚动。



我应用了column.Freeze = true,它的工作正常,但是当应用行[0 ] .freeze = true,它不适用于行冻结。

解决方案

当我创建DataGridView时,将其与一个DataTable,然后立即将前两行设置为冻结。行不冻结。但是,如果我处理一个按钮单击并设置要冻结的按钮单击,行成功冻结。那么如何在关联表之后立即冻结这些行?



这里有一些代码:

  DataTable dataTable = new DataTable(); 

//只需添加一堆列
(int i = 0; i {
dataTable.Columns.Add( Col+ i.ToString(),typeof
(string));
}

//添加一堆行到DataTable,一些虚拟

(int i = 0; i {
DataRow row = dataTable.NewRow(); (int j = 0; j {
row [Col+ j.ToString()] =Val+ i.ToString()+
- + j.ToString();
}
dataTable.Rows.Add(row);
}

gridView.DataSource = dataTable;

gridView.Rows [1] .Frozen = true;

这不行。行不冻结。但是,如果我在按钮事件处理程序中粘贴 gridView.Rows [1] .Frozen = true; 行,它可以正常工作。那么我怎么做,而不需要用户的事件触发?我看到两个解决方案:


  1. 以这种方式绑定数据:



    <$对于(int i = 0; i {
    DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn(); p $ p>
    c.Name =Col+ i.ToString();
    gridView.Columns.Add(c);
    }
    gridView.Rows.Add(100); (int i = 0; i (int j = 0; j gridView.Rows [i] .Cells [j ] .Value =Val+ i.ToString()+ - + j.ToString();
    gridView.Rows [0] .Frozen = true;


  2. 在此事件中选择冻结的行:

      private void gridView_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
    {
    FrozeFirstRow();
    }



I have a datagridview and am trying to achieve the following:1. Top row should be frozen while scrolling vertically.2. First two columns should be frozen wile scrolling horizontally.

I applied the column.Freeze = true and its working fine, but when applying row[0].freeze = true, it doesn't work for row freezing.

解决方案

When I create the DataGridView, I associate it with a DataTable and then immediately set the first two rows to freeze. The rows don't freeze. However, if I handle a button click and set the rows to freeze in that button click, the rows successfully freeze. How, then, do I freeze the rows immediately upon associating the table?

Here's some code:

 DataTable dataTable = new DataTable();

    // Just add a bunch of columns
    for (int i = 0; i < 15; i++)
    {
    dataTable.Columns.Add("Col" + i.ToString(), typeof
    (string));
    }

    // Add a bunch of rows to the DataTable, with some dummy
    values
    for (int i = 0; i < 100; i++)
    {
    DataRow row = dataTable.NewRow();
    for (int j = 0; j < 15; j++)
    {
    row["Col" + j.ToString()] = "Val" + i.ToString() +
    "-" + j.ToString();
    }
    dataTable.Rows.Add(row);
    }

    gridView.DataSource = dataTable;

    gridView.Rows[1].Frozen = true;

This won't work. The rows are not frozen. However if I stick the gridView.Rows[1].Frozen = true; line in a button event handler, it works. How would I do this, then, without requiring an event trigger from the user? I see two solutions:

  1. Bind the data this way:

    for (int i = 0; i < 15; i++)
    {
    DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
    c.Name = "Col" + i.ToString();
    gridView.Columns.Add(c);
    }
    gridView.Rows.Add(100);
    for (int i = 0; i < 100; i++)
    for (int j = 0; j < 15; j++)
    gridView.Rows[i].Cells[j].Value = "Val" + i.ToString() + "-" + j.ToString();
    gridView.Rows[0].Frozen = true;
    

  2. Select frozen rows in this event:

    private void gridView_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
    {
    FrozeFirstRow();
    }
    

这篇关于在datagridview中冻结顶行和前两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:27