[英]Unable To set row visible false of a datagridview
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
,但为什么呢?
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;
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 中的行,如果这是空白或空整行不显示
I have an example for U. I have a datagridview that may multiselected row.我有一个 U 的例子。我有一个可以多选行的 datagridview。 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记得设置属性 SelectionMode:FullRowSelect
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.回答这个话题可能有点晚了,但我建议您使用 DataTable.DefaultView.RowFilter 属性来过滤您需要在有界 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
请查看以下链接以获取更多信息: https ://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter ? redirectedfrom = MSDN & view = netframework-4.8#System_Data_DataView_RowFilter
regards.问候。
I know this is an old topic, but an alternative solution (my code is vb.net, but I assume it would translate)我知道这是一个老话题,但有一个替代解决方案(我的代码是 vb.net,但我认为它会翻译)
If WO_DGV.CurrentCell.RowIndex = i Then
'you cannot make invisible the row that is 'current'
WO_DGV.CurrentCell = WO_DGV.Rows(i - 1).Cells("act")
'to get to this code I know that there is a row before i, which is why I can use i-1 as new focus
End If
WO_DGV.Rows(i).Visible = False
I tried to hide a row in a CellFormating event and it didn't worked.我试图在 CellFormating 事件中隐藏一行,但没有奏效。 It looks like the currency maneger cann't be suspended for the row that raised the event than I deal with the row before (row 0 deals with the last row)
看起来货币管理器不能为引发事件的行暂停,而不是我之前处理该行(第 0 行处理最后一行)
Private Sub DgView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DgView1.CellFormatting
If DgView1.Rows(e.RowIndex).Cells(e.ColumnIndex).OwningColumn.Name = CoID Then
Dim k = If(e.RowIndex = 0, DgView1.RowCount - 1, e.RowIndex - 1)
DgView1.Rows(k).Visible = Countries.Rows(k)("Ro")
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.