Unable To set row visible false of a datagridview

对着背影说爱祢 提交于 2020-01-08 17:16:52

问题


I have a DataGridView where I set DataSource:

taskerEntities te = new taskerEntities();
var OMsMasterDescriptiveIndicators = te.MyTable.Select(x => new lccls {Id = x.Id, name = x.name }).ToList();
MyGrid.DataSource = OMsMasterDescriptiveIndicators;

with my class lccls as

public class lccls
    {
        public string Id { get; set; }
        public Nullable<decimal> name { get; set; }
    }

At a certain event I want to make the current row invisible:

 MyGrid.Rows[5].Visible = false;

But I am unable to do this. Instead an exception is thrown with the following error message:

Row associated with the currency manager's position cannot be made invisible

I suspect the reason is related to setting DataSource, but why?


回答1:


After searching a lot, I got the solution

CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];  
currencyManager1.SuspendBinding();
MyGrid.Rows[5].Visible = false;
currencyManager1.ResumeBinding();



回答2:


Cannot set yourDataGridView row visible property to false when current row index Will encounter such error if trying to hide current cell

soulution :

when yourDataGridView Data source is not null :

  CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[yourDataGridView.DataSource];
                       currencyManager1.SuspendBinding();
                       yourDataGridView.Rows[Target Index].Visible = false;
                       currencyManager1.ResumeBinding();

when yourDataGridView Data source is null :

 yourDataGridView.CurrentCell = null;
 yourDataGridView.Rows[Target Index].Visible = false;



回答3:


I have an example for U. I have a datagridview that may multiselected row. When i click the button to visible false row that selected. Try this:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            CurrencyManager currencyManager1 =(CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                dataGridView1.CurrentCell = null;
                row.Visible = false;
        }
        dataGridView1.Refresh();

Remember to set property SelectionMode: FullRowSelect




回答4:


Example

        foreach (DataGridViewRow rw in dataGridView1.Rows)
        {

        if (rw.Cells[14].Value.ToString() == "") // this Cell have a TEXT, 
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                rw.Visible = false; 
                currencyManager1.ResumeBinding();

            }
        }

this show only the row that have in the cell index 14, if this is blank or empty the whole row not show




回答5:


Maybe a little late to answer this topic but I suggest you to use DataTable.DefaultView.RowFilter property to filter what you need to show on the bounded DataGridView. Please check the following link for more informtion: https://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter?redirectedfrom=MSDN&view=netframework-4.8#System_Data_DataView_RowFilter

regards.



来源:https://stackoverflow.com/questions/18942017/unable-to-set-row-visible-false-of-a-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!