Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView (VB.net) problems, nonvisible rows are still visible or the DataGridView is empty despite having a DataSource
 

DataGridView (VB.net) problems, nonvisible rows are still visible or the DataGridView is empty despite having a DataSource

I have several problems with rows in a DataGridView. I posted them yesterday to http://stackoverflow.com/questions/1207324/datagridview-vb-net-problems-nonvisible-rows-are-still-visible-and-the-datagri but as I have got no answer yet, I give it a try here as well! When I get an answer at either place, I'll inform about that at the other place.

Background information:
The DataGridView (DataGridViewCalib) is in one TabPage of a TabControl. Some columns of the DataGridView are automatically DataGridViewCheckBoxColumn as the DataSource have some colums which are Boolean. This is a Windows Form written in VB.Net with Visual Studio 2008. The user loads an input data file.

The problems:

1) At first arrival in the TabPage, ShowDataGridViewCalib (code below) is called. All rows are then shown in the DataGridView, despite the code saying that some rows should not be visible. Breakpoints in the code show that the code do arrive at the
Rows.Visible = False
events.
Despite all rows beeing shown a Watch in the debugger shows that:

    DataGridViewCalib.DisplayedColumnCount(True)=0
DataGridViewCalib.DisplayedColumnCount(False=0)
DataGridViewCalib.DisplayedRowCount(True)=0
DataGridViewCalib.DisplayedRowCount(False)=0

But
Columns.Visible=False

works as expected.

When running the subroutine ShowDataGridViewCalib a second time, by enforcing it from the checkbox CbUniform, the reduction in the number of rows works as it should, and the DataGridViewCalib.Displayed...Count is correct.

What causes the whole DataTable to be shown the first time?

2) The user can load another input data file. When a second input file is loaded and ShowDataGridViewCalib is run, another strange thing occur. Watch in the debugger says DataGridViewCalib.DataSource = {System.Data.DataTable} and this DataTable has the same properties as dtCatchCalib,
but
    DataGridViewCalib.Columns.Count = 0   
DataGridViewCalib.Rows.Count = 0
and nothing is shown in the DataGridView.
Before the second input file is loaded most of the data is cleared, including
DataGridViewCalib.Columns.Clear()
dtCatchCalib.Clear()
Especially for this second problem I assume that the error might be somewhere outside of ShowDataGridViewCalib, but I would be very happy for hints about what causes the DataGridView to have a DataSource (with several rows and columns) but still no rows and columns.

The code:
        Private Sub ShowDataGridViewCalib()
'[...]
Dim kolwidth As Integer = 77
DataGridViewCalib.DataSource = dtCatchCalib
DataGridViewCalib.Refresh()
Dim kol As DataGridViewColumn
For Each kol In DataGridViewCalib.Columns
kol.SortMode = DataGridViewColumnSortMode.NotSortable
If CbUniform.Checked = False Then
kol.Visible = True
kol.Width = kolwidth
If kol.Name = "CatchmentID" Then
kol.ReadOnly = True
ElseIf kol.Name = "parc0" Then
kol.HeaderText = "c0"
ElseIf kol.Name = "statr" Then
kol.Visible = False
kol.ReadOnly = True
kol.HeaderText = "r"
End If
Else
If kol.Name = "IncludeObs" Then
kol.Width = kolwidth
ElseIf kol.Name = "CatchmentID" Then
kol.ReadOnly = True
kol.Width = kolwidth
Else
kol.Visible = False
End If
End If
Next

'Dim rad As DataGridViewRow
'Dim dum As Integer
'dum = 0
'For Each rad In DataGridViewCalib.Rows
' dum += 1 ' # rows in dtCatchCalib is = # subcatchments
' DataGridViewCalib.CurrentCell = Nothing ' Unselect the current cell, needed to be able to set the row invisible
' rad.Visible = False ' TEST
' If ObsLst(dum) = True Then ' ObsLst have its first value at index 1
' rad.Visible = True
' Else
' DataGridViewCalib.CurrentCell = Nothing ' Unselect the current cell, needed to be able to set the row invisible
' rad.Visible = False
' End If
'Next

For i = 0 To dtCatchCalib.Rows.Count - 1
DataGridViewCalib.CurrentCell = Nothing
DataGridViewCalib.Rows(i).Visible = False
If ObsLst(i+1) = True Then ' ObsLst have its first value at index 1
DataGridViewCalib.Rows(i).Visible = True
Else
DataGridViewCalib.CurrentCell = Nothing
DataGridViewCalib.Rows(i).Visible = False
End If
Next
'[...]
End Sub
There are two alternative ways of handling the rows in the code. The first attempt (commented away here) is probably the "nicest".

  • Edited byThe Nile Friday, July 31, 2009 8:18 AMCorrected a bug in the code (not related to the DataGridView problems)
  •  
The Nile  Friday, July 31, 2009 6:59 AM

Hi Nile,

Could you please package your code into a test project in which the error can be reproduced and send me a email with the test project as an attachment? I need detail code to figure out the issue.

Based on your description, the two issues have a common feature: we did set the Visible property but it did not react. The cause is mostly that the data source is bound again after we set the Visible property of the rows in the DataGridView. For example, we set all the rows invisible by setting the Visible property of them to false and expect they are hidden. But the data source might be bound again after we set the Visible property and the rows are recreated. The old rows are disposed so the setting would not make react. The new rows are initialized and their Visible property are true. So the rows are still visible.

You can trace the data source binding by track the DataBindingComplete event. You can add a handler to that event and show some message. Then you would know if the data source is bound again.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byThe Nile Wednesday, August 05, 2009 7:28 AM
  •  
Aland Li  Monday, August 03, 2009 11:25 AM

Could you please package your code into a test project in which the error can be reproduced and send me a email with the test project as an attachment? I need detail code to figure out the issue.


[...]

You can trace the data source binding by track the DataBindingComplete event. You can add a handler to that event and show some message. Then you would know if the data source is bound again.


Hi Aland,

Thanks for your answer! I solved the problem by a workaround. The DataTable I display in the DataGridView is anyway a temporary DataTable, combining columns from two different DataTables. Thus I removed the rows that should be invisible from this temporary DataTable already when I constructed it. Changes in the DataGridView, I pass on to the original DataTables in at the CellValueChanged event.

Unfortunately I have no backup of the code as it looked when I posted this question (lesson learned now, this was my first post in a programmers forum) and thus I cannot test the DataBindingComplete event or send you any test project. Thank you very much for your willingness to look further into the code.

The Nile





  • Marked As Answer byThe Nile Wednesday, August 05, 2009 7:29 AM
  •  
The Nile  Wednesday, August 05, 2009 7:28 AM

The second problem was still there also with my new DataTable with fewer rows. I solved it by setting
DataGridViewCalib.DataSource=Nothing
when I clearded the data and the forms before loading a new input files. Thus, it was not enough with
DataGridViewCalib.Columns.Clear()
which I only had before.


The Nile
  • Marked As Answer byThe Nile Wednesday, August 05, 2009 11:32 AM
  •  
The Nile  Wednesday, August 05, 2009 11:31 AM

Hi Nile,

Could you please package your code into a test project in which the error can be reproduced and send me a email with the test project as an attachment? I need detail code to figure out the issue.

Based on your description, the two issues have a common feature: we did set the Visible property but it did not react. The cause is mostly that the data source is bound again after we set the Visible property of the rows in the DataGridView. For example, we set all the rows invisible by setting the Visible property of them to false and expect they are hidden. But the data source might be bound again after we set the Visible property and the rows are recreated. The old rows are disposed so the setting would not make react. The new rows are initialized and their Visible property are true. So the rows are still visible.

You can trace the data source binding by track the DataBindingComplete event. You can add a handler to that event and show some message. Then you would know if the data source is bound again.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byThe Nile Wednesday, August 05, 2009 7:28 AM
  •  
Aland Li  Monday, August 03, 2009 11:25 AM

Could you please package your code into a test project in which the error can be reproduced and send me a email with the test project as an attachment? I need detail code to figure out the issue.


[...]

You can trace the data source binding by track the DataBindingComplete event. You can add a handler to that event and show some message. Then you would know if the data source is bound again.


Hi Aland,

Thanks for your answer! I solved the problem by a workaround. The DataTable I display in the DataGridView is anyway a temporary DataTable, combining columns from two different DataTables. Thus I removed the rows that should be invisible from this temporary DataTable already when I constructed it. Changes in the DataGridView, I pass on to the original DataTables in at the CellValueChanged event.

Unfortunately I have no backup of the code as it looked when I posted this question (lesson learned now, this was my first post in a programmers forum) and thus I cannot test the DataBindingComplete event or send you any test project. Thank you very much for your willingness to look further into the code.

The Nile





  • Marked As Answer byThe Nile Wednesday, August 05, 2009 7:29 AM
  •  
The Nile  Wednesday, August 05, 2009 7:28 AM

Hi Nile,


Thanks for sharing your answer. Welcome to ask new questions here.

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, August 05, 2009 7:31 AM

The second problem was still there also with my new DataTable with fewer rows. I solved it by setting
DataGridViewCalib.DataSource=Nothing
when I clearded the data and the forms before loading a new input files. Thus, it was not enough with
DataGridViewCalib.Columns.Clear()
which I only had before.


The Nile
  • Marked As Answer byThe Nile Wednesday, August 05, 2009 11:32 AM
  •  
The Nile  Wednesday, August 05, 2009 11:31 AM

You can use google to search for other answers

Custom Search

More Threads

• Remove Data Sources from IDE
• Providing INotifyPropertyChanged
• How to dynamically resize columns in DataGridView?
• BackgroundWorker and BindingSource Threading question
• How am I connecting to two tables according to personal parameters?
• Dynamic Connection String
• How to Find Next ????
• Selecting new value from ComboBox in edit mode doesn't work
• Winform data fields to multiple SQL tables
• System.IndexOutOfRangeException