Unanswered DataGridView invisible rows turn visible

  • Sunday, April 01, 2012 10:59 PM
     
     

    I turn some of my grid rows invisible.

    When i click the header column, my invisible rows turn visible. Why?

    My datagrid has a DataSource.

    DGV.DataSource = myTable.DefaultView;

    Ndav_vi

    • Moved by CoolDadTxMVP Tuesday, April 03, 2012 6:07 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Monday, April 02, 2012 7:19 AM
     
     

    Try to row  visible = false on Datagridview event "DataBindingComplete"

    private void yourDataGirdName_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {

    if (yourDataGirdName.DataSource != null)

    {

    if (yourDataGirdName.Rows.Count > 0)

    {

    for(int i=0;i<yourDataGirdName.Rows.Count;i++)

    {

    //Your condition for Visibiltiy of row

    if(visible = false)

    {

    yourDataGirdName.Row[i].visible = false;

    }

    }

    }

    }

    }

  • Monday, April 02, 2012 7:45 AM
     
     
    How did you set the rows to invisible? Show us the code.

    Mitja

  • Tuesday, April 03, 2012 7:15 AM
     
      Has Code

    Hi Ndav_vi,

    Welcome to the MSDN forum.

    I think Dhaval is right, in addition, you can also do something with DataGridView.ColumnHeaderMouseClick Event. You can try the sample below.

            void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                invisible();
            }
    
            void invisible()
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    int rowCount = cm.Count;
                    for (int row = 0; row < rowCount; ++row)
                    {
                        DataGridViewRow dgvr = dataGridView1.Rows[row];
                        if (dgvr.Cells[0].Value.ToString() == "bbc")
                        {
                            cm.SuspendBinding();
                            dgvr.Visible = false;
                        }
                    }
                }
     
            }

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, April 16, 2012 7:32 PM
     
      Has Code

    Hi,

    Bob Shen,

    Dhaval Chauhan,

    Thanks for your reply.

    I thought it works, but:

    Current cell cannot be set to an invisible cell. (The new problem).

    So i turned to work with RowFilter.

    I wonder why the code raises the exception meantioned above.

    (after i sort the grid)

    How can I avoid it or fix the code.

    Here is my test app code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            bool searchMode = false;
            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)
            {
                invisible();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                searchMode = true;
            }
            private void myGrid_DataBindingComplete(object sender, 
                DataGridViewBindingCompleteEventArgs e)
            {
                if (!searchMode) 
                    return;
                invisible();
            }
            void invisible()
            {
                if (myGrid.Rows.Count > 0)
                {
                    CurrencyManager cm = 
                        (CurrencyManager)BindingContext[myGrid.DataSource];
                    int rowCount = cm.Count;
                    for (int row = 0; row < rowCount; ++row)
                    {
                        DataGridViewRow dgvr = myGrid.Rows[row];
                        if (dgvr.Cells[0].Value.ToString() == "Gal")
                        {
                            cm.SuspendBinding();
                            dgvr.Visible = false;
                        }
                    }
                }
            }
            private void button3_Click(object sender, EventArgs e)
            {
                myTable.DefaultView.RowFilter =
                    "not (key = '2') and not (key = '3')";
            }
        }
    }

    Thanks

    Ndav_vi