dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    108

    Resolved [RESOLVED] DatagridView row number into TAB control

    Hi all,
    I created two windows forms where in first I collected all data in several datatables and put each data in tab control using datagridview:
    In First Form I inserted this code:

    Code:
    Output_Form2.t1 = New TabControl
    
            Output_ Form2.mytabPage = New TabPage
            Output_ Form2.t1.Dock = DockStyle.Fill
            grid.Dock = DockStyle.Fill
            Output_Form2.TableLayoutPanel1.Controls.Add(Output_Form2.t1)
            Output_Form2.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    
            grid.DataSource = table_Form2_UtranCell
            Output_Form2.mytabPage.Controls.Add(grid)
            Output_Form2.t1.Controls.Add(Output_Form2.mytabPage)
            Output_Form2.mytabPage.Text = "UUtranCellFDD"
            grid.RowHeadersVisible = True
    In second Form and exactly when I load the form I reported following code:

    Code:
    For z = 1 To t1.TabPages.Count
                SelectedGrid = New DataGridView
    For Each ctl As Control In t1.TabPages(z - 1).Controls                 	
               If TypeOf ctl Is DataGridView Then
                        t1.TabPages(z - 1).Select()
                        SelectedGrid = ctl
    
                        Dim cc = t1.TabPages(z - 1).Text
                        Dim rowNumber As Integer = 1
                        
                        For i = 0 To SelectedGrid.RowCount - 1
                            SelectedGrid.Rows(i).HeaderCell.Value = CStr(i + 1)
                        Next
                End If
    
            Next
     Next
    But I visualized cell number only in the first tab page and not in the other ones but I don't understand why.

    Can you help me?
    Thanks in advacend
    gio

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,705

    Re: DatagridView row number into TAB control

    You can't edit or even access controls in tabpages that are not the selectedtab. You need to set each tabpage as selectedtab before editing the datagridview

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    108

    Re: DatagridView row number into TAB control

    Hi Paul,
    thanks for you reply!
    So I have to specify
    Code:
    t1.TabPages(z - 1).Select()
    before ctl control.
    Is it correct?
    Last edited by giodepa; Jan 30th, 2018 at 02:09 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,705

    Re: DatagridView row number into TAB control

    something along these lines


    Code:
    For z = 1 To t1.TabPages.Count
        t1.SelectedTab = t1.TabPages(z - 1)
        SelectedGrid = New DataGridView
        For Each ctl As Control In t1.TabPages(z - 1).Controls                 	
               If TypeOf ctl Is DataGridView Then                    
                        SelectedGrid = ctl
    
                        Dim cc = t1.TabPages(z - 1).Text
                        Dim rowNumber As Integer = 1
                        
                        For i = 0 To SelectedGrid.RowCount - 1
                            SelectedGrid.Rows(i).HeaderCell.Value = CStr(i + 1)
                        Next
                End If
    
            Next
     Next

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    108

    Re: DatagridView row number into TAB control

    thanks Paul!!!
    It works fine

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    108

    Re: DatagridView row number into TAB control

    Hi Paul,
    now I just want to develop Row Number with this function:
    Code:
     Private Sub selectedGrid_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles SelectedGrid.RowPostPaint
                         Using b As SolidBrush = New SolidBrush(SelectedGrid.RowHeadersDefaultCellStyle.ForeColor)
    
                            e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture) + 1, SelectedGrid.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
    
                        End Using
                          
    
        End Sub
    I tried to modify it without success as follow:

    Code:
    Private Sub selectedGrid_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles SelectedGrid.RowPostPaint
    For z = 1 To t1.TabPages.Count
                For Each ctl As Control In t1.TabPages(z - 1).Controls
                    t1.SelectedTab = t1.TabPages(z - 1)
                    If TypeOf ctl Is DataGridView Then
    
                        SelectedGrid = ctl
    
    
                        Using b As SolidBrush = New SolidBrush(SelectedGrid.RowHeadersDefaultCellStyle.ForeColor)
    
                            e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture) + 1, SelectedGrid.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
    
                        End Using
                    End If
    
                Next
            Next
    End sub
    Meanwhile if I change 'z-1' with '0' Row numbers are created only in datagridview of first TAB.

    What is my mistake?

    Thanks in advanced
    gio

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,705

    Re: [RESOLVED] DatagridView row number into TAB control

    The problem is that you're using a single DGV event and trying to modify other DGVs in that event. If the first code block works, then you should handle the RowPostPaint event of all of your DGVs in one handler...

    Code:
    Private Sub selectedGrid_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles grid1.RowPostPaint, grid2.RowPostPaint, grid3.RowPostPaint, etc...
    .   Dim selectedGrid as DataGridView = DirectCast(sender, DataGridView)
                         Using b As SolidBrush = New SolidBrush(SelectedGrid.RowHeadersDefaultCellStyle.ForeColor)
    
                            e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture) + 1, SelectedGrid.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
    
                        End Using
                          
    
        End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    108

    Re: [RESOLVED] DatagridView row number into TAB control

    thanks Paul,
    I had to rename all DGVs for each tab. So now it works

    gio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.