Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:14:30+00:00 2026-05-28T00:14:30+00:00

I am having trouble getting the row of a WPF grid that a textbox

  • 0

I am having trouble getting the row of a WPF grid that a textbox is in.
I have a grid that starts off with one RowDefinition. That row contains an “add” button that adds another rowdefinition to the grid below that row. This new row also contains an “add” button that performs the same function.

The problem I am having is that the function GetRow() always returns 0.

If I declare a button in the XAML that calls the same function, GetRow() returns the correct value. The problem seems to stem from the face that the buttons are created in codebehind.

This is the function that handles the click event of the “add” buttons:

Private Sub btnAddRow_Click(ByVal sender As System.Object, _
  ByVal e As System.Windows.RoutedEventArgs)
  Dim btnSender As Button = sender
  Dim row As Integer
  row = Grid.GetRow(btnSender)
  AddRow(row)
End Sub

The function “AddRow” adds a new RowDefinition to the grid, the “add” button for that row, and a few other controls (label, textbox, etc).

Private Sub AddRow(ByVal position As Integer)
    Dim rd As New RowDefinition()
    rd.Height = New GridLength(35, GridUnitType.Pixel)
    Me.Height += 35
    myGrid.RowDefinitions.Insert(position, rd)
    Dim add As New Button
    add.Content = "Add Row"
    add.HorizontalAlignment = Windows.HorizontalAlignment.Center
    add.VerticalAlignment = Windows.VerticalAlignment.Center
    AddHandler add.Click, AddressOf btnAddRow_Click
    Grid.SetColumn(add, 2)
    Grid.SetRow(add, position)
    myGrid.Children.Add(add)
End Sub

I found this thread, but using “e.Source” or “e.OriginalSource” did not solve the problem.
Grid.GetRow and Grid.GetColumn keep returning 0

EDIT:

Here is my code. I pulled it out of the project it was in and created a new project for testing.

Class MainWindow 
Private Sub btnAddRow_Click(ByVal sender As System.Object, _
                            ByVal e As System.Windows.RoutedEventArgs)
    Dim btnSender As Button = sender
    Dim row As Integer
    row = Grid.GetRow(btnSender)
    row = row + 1
    AddRow(row)
End Sub

Private Sub AddRow(ByVal position As Integer)
    If (myGrid.RowDefinitions.Count < position) Then
        position = myGrid.RowDefinitions.Count
    End If

For Each element In (From i As UIElement In myWaypointGrid.Children Where Grid.GetRow(i) >= position Select i).ToList()
            Grid.SetRow(element, Grid.GetRow(element) + 1)
        Next

    Dim rd As New RowDefinition()
    rd.Height = New GridLength(35, GridUnitType.Pixel)
    Me.Height += 35
    myGrid.RowDefinitions.Insert(position, rd)
    Dim add As New Button
    add.Content = "Add Row " & position
    add.HorizontalAlignment = Windows.HorizontalAlignment.Center
    add.VerticalAlignment = Windows.VerticalAlignment.Center
    AddHandler add.Click, AddressOf btnAddRow_Click
    Grid.SetColumn(add, 2)
    Grid.SetRow(add, position)
    myGrid.Children.Add(add)
End Sub

Private Sub MainWindow_Loaded(ByVal sender As Object, _
                              ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    AddRow(0)
End Sub

End Class

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="myGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
</Grid>

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:14:31+00:00Added an answer on May 28, 2026 at 12:14 am

    Are you ever calling the AddRow function prior to the first “Add” button click? Without more code, it’s hard to say why this is not working.

    Update to reflect the true issue:

    You don’t do an increment on the position variable which gets passed into this function so all your buttons are being added to row 0. That is why they all return 0 when you call GetRow

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble getting a table row implemented. My coding is not allowing
I'm having trouble getting a filter to work on a BindingSource that is the
I am having a lot of trouble getting an EditText within a ListView row
That sounds weird I know, but I am having trouble getting a piece of
Im having some trouble getting on with my first codeigniter project, and i have
I'm exporting a grid with NPOI v1.2.3, and am having trouble getting cell formatting
I'm having trouble getting ThinkingSphinx to recognize my conditions. My Discussion model includes the
I'm having trouble getting Spring.Net to log, using Log4Net. I'm particulary interested in seeing
I am having trouble getting routing to work on mono. The default route works
I am having trouble getting a background-image to overlay the border of another div.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.