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

  • Home
  • SEARCH
  • 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 6328655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:29:55+00:00 2026-05-24T17:29:55+00:00

Whenever the user clicks a button I want to get the selected row in

  • 0

Whenever the user clicks a button I want to get the selected row in a DataGrid and change its background color?
I can get the index of the selected row using the SelectedIndex property but I do not know how to change the background of the same.

I use WPF, C# and .Net 4 in VS2010.

Thanks…

  • 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-24T17:29:56+00:00Added an answer on May 24, 2026 at 5:29 pm

    It’s better to use triggers for this sort of things but try the following

    private void button_Click(object sender, RoutedEventArgs e)
    {
        DataGridRow dataGridRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex) as DataGridRow;
        if (dataGridRow != null)
        {
            dataGridRow.Background = Brushes.Green;
        }
    }
    

    Edit
    The selected DataGridCells will still override that background so you would probably have to handle that as well, using the Tag property on the parent DataGridRow for example

    <DataGrid ...>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},
                                                   Path=Tag}" Value="ChangedBackground">
                        <Setter Property="Background" Value="Transparent" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
        <!--...-->
    </DataGrid>
    
    private void button_Click(object sender, RoutedEventArgs e)
    {
        DataGridRow dataGridRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex) as DataGridRow;
        if (dataGridRow != null)
        {
            dataGridRow.Background = Brushes.Green;
            dataGridRow.Tag = "ChangedBackground";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want a sound to play whenever a user clicks a button. Said
I want to get the text of the button whenever I click on it.
I want to get the date from datepicker whenever user choose the date in
Whenever a user clicks a menu item I want to show a tooltip: //
I want my web page to beep whenever a user exceeds the maximum character
So I want to trigger an event (pausing/unpausing some media) whenever the user presses
Background: For some reason, whenever a user tries to open an xslx (excel 2007)
in my ios app when the user clicks a button it opens email part
Whenever a user does something a a series of scripts are executing, after everything
Whenever the user scrolls map or zooms in/out, this method gets called instantaneously. I

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.