Link to home
Start Free TrialLog in
Avatar of Darren Brown
Darren BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Formatting datagrid row not working when running a loop within a loop

I'm trying to hide certain datagridview rows within a loop, which is part of another loop that creates the tab page and datagridview.  The loop appears to be work as expected if I display the data from the row, which I would like to hide, in a message box.  However, if I change the code to hide the relevant row then only the first tab\datagrid created works.

For i As Integer = 0 To DGV_DetailTabs.Rows.Count - 1

    Dim mytabPage As TabPage = New TabPage
    mytabPage.Name = Me.DGV_DetailTabs.Rows(i).Cells("tab_code").Value
    mytabPage.Text = Me.DGV_DetailTabs.Rows(i).Cells("tab_desc").Value
    Dim dgv As New DataGridView
    dgv.Name = Me.DGV_DetailTabs.Rows(i).Cells("tab_code").Value
    dgv.Left = 10
    dgv.Top = 10
    dgv.Height = 300
    dgv.Width = 400

    Dim SQLCMD2 As New SqlCommand("SET NOCOUNT ON Declare @TMP_Det TABLE
       (Expand char(10), Seqno int, Parentid int, Tabcode int, Tabdesc char(20), Casegroup char(40), Detaildesc char(60),
       Casetext char(60), Casedate datetime, Casevalue decimal(13,2), Casemkr char(1), Rectyp char(1))
       insert into @TMP_Det
       select ...
       Select * from @TMP_Det", SQLCONN)
    Using sda As New SqlDataAdapter(SQLCMD2)
        Dim dt As New DataTable()
        dgv.DataSource = dt
    End Using
    dgv.Columns("Seqno").Visible = False
    dgv.Columns("Parentid").Visible = False
    dgv.Columns("Tabcode").Visible = False
    dgv.Columns("Tabdesc").Visible = False
    dgv.Columns("CaseGroup").Visible = False
    dgv.Columns("Rectyp").Visible = False
    'dgv.Enabled = False
    dgv.ColumnHeadersVisible = False
    dgv.AllowUserToAddRows = False

    For j = 0 To dgv.RowCount - 1
        If dgv.Rows(j).Cells("Parentid").Value <> "0" Then
            dgv.Rows(j).Visible = False
            'MessageBox.Show("Hidden ", dgv.Rows(j).Cells("Detaildesc").Value)
        ElseIf dgv.Rows(j).Cells("Parentid").Value = "0" Then
            'MessageBox.Show("Not Hidden ", dgv.Rows(j).Cells("Detaildesc").Value)dgv.Rows(j).DefaultCellStyle.BackColor = Color.Red
        End If

Open in new window

Avatar of Darren Brown
Darren Brown
Flag of United Kingdom of Great Britain and Northern Ireland image


Thanks, I tried to do that but it only blocked part of the code.
Avatar of gr8gonzo
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello and thanks for the reply.  If I run the code with a message box to display the description of each row that should be hidden, then it works and displays each record.  However, if I change the code to hide the row then only the rows on the first tab work, so you might be onto something where the other tabs won't work if not displayed.  I've also tried changing the back colour of the row and that has the same result, where it just works on the first tab.  I have a control already when changing tabs, so will add it to this instead to see if it works.  Thanks for your help.
Sorry and yes it does work if I display any message box, regardless of the data.  Thanks
Just tested and the workaround seems to be to add the formatting to the tab selected index changed, which works fine.  Thanks again for your help.

Glad to hear it worked!