DataGridView不可见行变为可见 [英] DataGridView invisible rows turn visible

查看:81
本文介绍了DataGridView不可见行变为可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是问题的示例:

当我点击button1时,我将一行变为不可见:

When i click button1 i turn a row to be invisible:

private void button1_Click(object sender, EventArgs e)
{
   myGrid.Rows[2].Visible = false;
}




行中的行消失。


And the row disapears from the grid.

当我点击列标题时,行被排序,但行[2]变为可见!

我的表单仅包含"myGrid"和button1。

网格数据源是一个简单的表。我在一个更大的应用程序中遇到了这个问题,但问题在这个简单的应用程序中得到了恢复。

这是我的代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataTable myTable;
        public Form1()
        {
            InitializeComponent();
        }
        void InitMyTable()
        {
            myTable = new DataTable();
            myTable.Columns.Add(
                new DataColumn("FirstName", typeof(string)));
            myTable.Columns.Add(
                new DataColumn("LastName", typeof(string)));
            myTable.Columns.Add(
                new DataColumn("Key", typeof(string)));
            AddRow("1", "Nadav" , "Vi");
            AddRow("2", "Yana", "Hod");
            AddRow("3", "Gal", "Hodin");
            AddRow("4", "Yanko", "Yanooki");
        }
        void AddRow(string key, string firsName, string lastName)
        {
            DataRow row = myTable.NewRow();
            row["Key"] = key;
            row["FirstName"] = firsName;
            row["LastName"] = lastName;
            myTable.Rows.Add(row);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            InitMyTable();
            myGrid.DataSource = myTable;
            myGrid.ReadOnly = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            myGrid.Rows[2].Visible = false;
        }
    }
}

如何我可以避免看不见行的不必要外观吗?

谢谢。

推荐答案


这是一个问题的例子:

Here is an example of the problem:

当我点击button1时,我将一行变为不可见:

When i click button1 i turn a row to be invisible:

private void button1_Click(object sender, EventArgs e)
{
   myGrid.Rows[2].Visible = false;
}




并且该行从网格中消失。


And the row disapears from the grid.

当我点击列标题时,行被排序,但行[2]变为可见!

这很正常。

为什么?因为当你隐藏一行时,这并不意味着你删除了它。它只是不可见。

Why? Because when you hide a row, this doesnt mean you deleted it. It just simply is not visible.

当您点击列标题时,接下来发生了什么?您开始重新排序行。而之前隐藏它的行的值现在又出现了,但是在另一行上。这很正常。 

What happened next, when you clicked on a column header? You started re-sorting rows. And thevalue from the row that you hide it before, now appeared again, but that on a different row. This is just normal. 

你想隐藏什么?如果您要删除它,则删除它(如果从dgv或dgv的数据源删除)。如果没有,他们只需按下dgv列标题即可禁用排序。

What is that you want to do with hiding? If you mean to delete it, then delete it (remove if from dgv or from data source of dgv). If not, them simply disable sorting by pressing on dgv column header.


这篇关于DataGridView不可见行变为可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆