C# 无法设置数据网格视图的行可见假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18942017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable To set row visible false of a datagridview
提问by Amit Bisht
I have a DataGridView
where I set DataSource
:
我有一个DataGridView
我设置的地方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
和我class lccls
一样
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?
我怀疑原因与设置有关DataSource
,但为什么呢?
采纳答案by Amit Bisht
回答by Wnqs Winnerneverquits
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:
我有一个 U 的例子。我有一个可以多选行的 datagridview。当我单击按钮以显示所选的错误行时。尝试这个:
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
记得设置属性 SelectionMode:FullRowSelect
回答by Moory Pc
Cannot set yourDataGridView row visible property to false when current row index Will encounter such error if trying to hide current cell
当前行索引时无法将 yourDataGridView 行可见属性设置为 false 如果尝试隐藏当前单元格会遇到此类错误
soulution :
解决方案:
when yourDataGridView Data source is not null :
当 yourDataGridView 数据源不为 null 时:
CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[yourDataGridView.DataSource];
currencyManager1.SuspendBinding();
yourDataGridView.Rows[Target Index].Visible = false;
currencyManager1.ResumeBinding();
when yourDataGridView Data source is null :
当 yourDataGridView 数据源为 null 时:
yourDataGridView.CurrentCell = null;
yourDataGridView.Rows[Target Index].Visible = false;
回答by Cristian UYT
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
这仅显示单元格索引 14 中的行,如果这是空白或空整行不显示
回答by HassanRezai
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
回答这个话题可能有点晚了,但我建议你使用 DataTable.DefaultView.RowFilter 属性来过滤你需要在有界 DataGridView 上显示的内容。请查看以下链接以获取更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter ?redirectedfrom =MSDN &view =netframework-4.8#System_Data_DataView_RowFilter
regards.
问候。