Unable to Set Row Visible False of a Datagridview

Unable To set row visible false of a datagridview

After searching a lot, I got the solution

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

Unable To set row visible false of a datagridview

Solution is

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

Hiding DataGridView Row 0 produces error

From my understanding I believe if that's the only row left and it's selected the row or cell must be visible. To get around this you could possibly setting the current cell to null then SuspendBinding...

Here's a simple example...

 Me.dgv.CurrentCell = Nothing
Me.cm1.SuspendBinding()
Me.dgv.Rows(index).Visible = False

Hide Row in DataGridView with Binding not working in my project

Searching on this issue solve the problem by adding DataBindingComplete event and hide the specific row. DataBindingComplete is fired 2 times, 1 at the time of binding and 2nd after form_load event complete.

public partial class frmTestGirdBinding : Form
{
CustomDataCollection cdata = new CustomDataCollection();
Random rnd = new Random();
public frmTestGirdBinding()
{
InitializeComponent();
this.dataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView1_DataBindingComplete);
}

private void frmTestGirdBinding_Load(object sender, EventArgs e)
{
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = cdata;
dataGridView1.DataSource = bindingSource1;

}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < cdata.Count; i++)
{
cdata[i].Reading = (float)rnd.NextDouble();
}
dataGridView1.Refresh(); //without this all rows are not updating
}

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//InVisible the rows
dataGridView1.Rows[2].Visible = false;
dataGridView1.Rows[3].Visible = false;
}
}

class CustomDataCollection : BindingList<CustomData>
{
public CustomDataCollection()
{
this.Add(new CustomData() { SrNo = 1, Name = "A", Reading = 11.11F });
this.Add(new CustomData() { SrNo = 2, Name = "B", Reading = 22.11F });
this.Add(new CustomData() { SrNo = 3, Name = "C", Reading = 33.11F });
this.Add(new CustomData() { SrNo = 4, Name = "D", Reading = 44.11F });
this.Add(new CustomData() { SrNo = 5, Name = "E", Reading = 55.11F });
this.Add(new CustomData() { SrNo = 6, Name = "F", Reading = 66.11F });
this.Add(new CustomData() { SrNo = 7, Name = "G", Reading = 77.11F });
}
}
class CustomData : INotifyPropertyChanged
{
int srno;

public int SrNo
{
get { return srno; }
set { srno = value; OnPropertyChanged("SrNo"); }
}

string name;

public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}

float reading;

public float Reading
{
get { return reading; }
set { reading = value; OnPropertyChanged("Reading"); }
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion

}

Hide a row in DataGridView

You could use Rows.Item() to hide specific DataGridViewRow, like:

 If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If

I am assuming you are using FullRowSelect here.

If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:

  If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If

To Unhide everything let's say from a Button Click you could have this:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub

How to change visibility of first row in datagridview

I found how to do it.

You must specify:

datagridview.CurrentCell = null;

before setting:

row.Visible = false;

because if currentCell is in the row which is wanted to be hide, then mentioned problem occurs.

regards

DataGridView ID Column Will Not Hide

Suggestion 1:
Try explicitly setting the DGV Column's Visible property to false in the FormLoad event:

dataGridView.Columns["YourIdColumn"].Visible = false;

Suggestion 2:
Try changing your column dgvActiveMinersRecordId from the first column in the DGV to the last column.



Related Topics



Leave a reply



Submit