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 9266513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:20:12+00:00 2026-06-18T14:20:12+00:00

I am new to WPF. I am bind data table to data grid in

  • 0

I am new to WPF. I am bind data table to data grid in my wpf project. I have button present in my DataGrid and on button click event, I am trying to find the GridViewRow. But I am getting Grdrow1 as null.

My Code:

<my:DataGrid Name="GridView1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
    <my:DataGrid.Columns>
        <my:DataGridTemplateColumn Header="Vehicle Reg. No." Width="SizeToCells">
            <my:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Name="btnUserId" Cursor="Hand"  Click="btnUserId_Click" Content="{Binding Path=VehicleReg }" Style="{StaticResource LinkButton}"></Button>
                </DataTemplate>
            </my:DataGridTemplateColumn.CellTemplate>
        </my:DataGridTemplateColumn>
        <my:DataGridTextColumn Header="Vehicle Name"  Binding="{Binding Path=VehicleName}">
        </my:DataGridTextColumn>
    </my:DataGrid.Columns>
</my:DataGrid>

My C# code is:

private void btnUserId_Click(object sender, RoutedEventArgs e)
{            
    GridViewRow Grdrow1 = ((FrameworkElement)sender).DataContext as GridViewRow;
}

——-My Edited Post——

I have use the below namespace for GridViewRow:-

using System.Web.UI.WebControls;

This is current or any other I have to use?

  • 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-06-18T14:20:13+00:00Added an answer on June 18, 2026 at 2:20 pm

    First off, make sure you’re not confusing DataGrid with a GridView (of a ListView). It seems your Xaml is using DataGrid, but you’re trying to cast to a GridViewRow which is not present in DataGrids. You are probably looking for a DataGridRow instead.

    Making this change is not enough however – you’ll still be attempting a cast that won’t succeed. The sender of the Click event in WPF is simply the object that was used to hook up the event handler, which is Button in this case. In order to find the DataGridRow that contains this Button, you’ll have to walk up the WPF Visual Tree until you find it using the VisualTreeHelper class.

    Keeping the same Xaml, here is what I propose for the Click event handler. Notice that the sender is the Button. Starting from that, we just climb the Visual Tree by using the VisualTree.GetParent method until we find the first object that is of type DataGridRow.

    private void btnUserId_Click(object sender, RoutedEventArgs e)
    {            
        Button button = sender as Button;
        if (button == null)
            return;
    
        DataGridRow clickedRow = null;
        DependencyObject current = VisualTreeHelper.GetParent(button);
    
        while (clickedRow == null)
        {
            clickedRow = current as DataGridRow;
            current = VisualTreeHelper.GetParent(current);
        }
    
        // after the while-loop, clickedRow should be set to what you want
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am very new to WPF and trying to bind or assign a
I'm just finding my way with WPF/Datagrid/Linq and I'm trying to find out how
I have a WPF Data Grid bound to an observable collection, which is working
I'm new to WPF data binding. I have a ListBox on a form that
How can you 'two-way bind' a hierarchical data structure to a flat wpf datagrid?
I have a data grid in my WPF application window, which I have to
I have a WPF DataGrid with some data. You can add rows through a
While designing a new WPF application I noticed exceptions not being thrown during data
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
New to wpf and through a learning curve. I have a userControl with a

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.