Results 1 to 11 of 11

Thread: unknown dynamic dgv name

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    unknown dynamic dgv name

    hello dear community, i have a problem which i am trying to solve since 1 week now.

    i have 2 textboxes OUTSIDE of the tabcontrol. and a tabcontrol with 3 tabpages. on every tabpage there is a dgv. the dgv names are dgv0 dgv1 dgv2. so every tabpage has a dgv with the number of the tabpage index. i want to be able to use the 2 textboxes to change the headertext of the dgv. so if user selects tabpage0 and types in to the textboxes something the headertext of the dgv0 shoudl be changed. so i want to have 2 textboxes and i want to be able to edit all dgv headertexts. what i tryied is:

    Code:
    Dim selecteddgv As DataGridView = Nothing
            selecteddgv.Name = "dgv" + mytabcontrol.SelectedIndex.ToString
            selecteddgv.Columns(0).HeaderText = TextBox1.Text

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: unknown dynamic dgv name

    create the name of the grid and use the Controls.Find method to get it...

    Code:
    Dim gridName As String = "dgv" & mytabcontrol.SelectedIndex.ToString
    selecteddgv = myTabControl.Controls.Find(gridName,True)
    If you can guarantee the grid is in the tabcontrol then that code will suffice. It will however throw an exception if the grid is not found.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: unknown dynamic dgv name

    If every TabPage has one and only one DataGridView, there is no need to know its Name property to retrieve a reference to the DataGridView.

    Code:
    Dim dgv As DataGridView = TabControl1.SelectedTab.Controls.OfType(Of DataGridView).First
    dgv.Columns(0).HeaderText = TextBox1.Text

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: unknown dynamic dgv name

    TnTinMN your code works perfect but i have 2 dgv now on each tabpage so it doesnt work anymore or i dont know how to modify it.and kebo´s code doesnt work it is underlined :/

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: unknown dynamic dgv name

    Quote Originally Posted by moxid View Post
    TnTinMN your code works perfect but i have 2 dgv now on each tabpage so it doesnt work anymore or i dont know how to modify it.and kebo´s code doesnt work it is underlined :/
    What does that mean? How does it not work? What is underlined? Have you tried to fix it? Hover the mouse over it. Does a pop-up appear telling you what the problem is? If so what does it say?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: unknown dynamic dgv name

    dear kebo
    Code:
     Dim gridName As String = "dgv" & tabcontrol1.SelectedIndex.ToString
     selecteddgv = tabcontrol1.Controls.Find(gridName, True)
    "selecteddgv has not been declared" is the error . i hope you can help me with this errormessage.

  7. #7
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: unknown dynamic dgv name

    So what have you called the second dgv on each tab page?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: unknown dynamic dgv name

    Quote Originally Posted by moxid View Post
    dear kebo
    Code:
     Dim gridName As String = "dgv" & tabcontrol1.SelectedIndex.ToString
     selecteddgv = tabcontrol1.Controls.Find(gridName, True)
    "selecteddgv has not been declared" is the error . i hope you can help me with this errormessage.
    When something is not declared, it means that no variable with that name has been created. Take a look at the code in your first post.... now look at the code I posted. Do you see a difference? Do you see the part that is missing?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: unknown dynamic dgv name

    yes i tried it also, but it didn´t work:

    Code:
    Dim selecteddgv As DataGridView
            Dim gridName As String = "dgv" & tabcontrol_speisen.SelectedIndex.ToString
            selecteddgv = tabcontrol_speisen.Controls.Find(gridName, True)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: unknown dynamic dgv name

    Dear NeedSomeAnswers, the 2. dgvs on each tabpage are called dgv_speisen and they have index of tabpage like dgv_speisen0 dgv_sepisen1 and so on.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: unknown dynamic dgv name

    Errormessage: Array of Type System.Windows.Forms.Control can not be converted to System.Windows.Forms.Datagridview

    This is what i have so far:

    Code:
    Dim selecteddgv As DataGridView
    Dim gridName As String = "dgv" & tabcontrol_speisen.SelectedIndex.ToString
    selecteddgv = tabcontrol_speisen.Controls.Find(gridName, True) << ERROR HERE

Posting Permissions

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



Click Here to Expand Forum to Full Width