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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:08:23+00:00 2026-05-30T22:08:23+00:00

I am trying to programatically select an entire column in a WPF DataGrid. My

  • 0

I am trying to programatically select an entire column in a WPF DataGrid. My code seems to work but it is REALLY slow! I’m guessing it is because it is continually having to call ScrollIntoView. Can someone help me with a solution to speed it up or an alternative to select the entire column?

public static void SelectColumn(DataGrid grid, int column)
{
    for (int i = 0; i < grid.Items.Count; i++)
    {
        // Select each cell in this column
        var cell = DataGridHelper.GetCell(grid, i, column);
        if (cell != null)
        {
            cell.IsSelected = true;
        }
    }

    DataGridHelper.GetCell(grid, 0, column).Focus();
}


public static DataGridCell GetCell(DataGrid grid, int row, int column)
{
    DataGridRow rowContainer = GetRow(grid, row);

    if (rowContainer != null)
    {
        DataGridCellsPresenter presenter = TreeHelper.GetVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter == null)
        {
            // may be virtualized, bring into view and try again
            grid.ScrollIntoView(rowContainer, grid.Columns[column]);
            presenter = TreeHelper.GetVisualChild<DataGridCellsPresenter>(rowContainer);
        }

        if (presenter != null)
        {
            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // may be virtualized, bring into view and try again
                grid.ScrollIntoView(rowContainer, grid.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }

            return cell;
        }
    }

    return null;
}

public static DataGridRow GetRow(DataGrid grid, int index)
{

    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // may be virtualized, bring into view and try again
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    }

    return row;
}

UPDATE:

I’m trying out the solution suggested by @ianschol. Here is what I have (I bind in code behind b/c I don’t know how many columns I need until runtime):

for (int i = 0; i < this.CurrentData.Data[0].Length; i++)
        {
            TheGrid.Columns.Add(
                new DataGridTextColumn
                {
                    Header = (this.CurrentData.Rank > 1) ? string.Format(this.culture, headerFormatString, i + 1) : string.Empty,
                    Binding = new Binding(string.Format("[{0}].DataValue", i)) { ValidatesOnDataErrors = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },
                    Width = DataGridLength.Auto,
                    ElementStyle = new Style
                    {
                        TargetType = typeof(TextBlock),
                        Triggers = { this.errorTrigger }
                    },

                    EditingElementStyle = new Style
                    {
                        TargetType = typeof(TextBox),
                        Triggers = { this.errorTrigger }
                    },

                    CellStyle = new Style
                    {
                        TargetType = typeof(DataGridCell),
                        Setters =
                        {
                            new Setter
                            {
                                Property = DataGridCell.IsSelectedProperty,
                                Value = new Binding(string.Format("[{0}].IsSelected", i)) { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },
                            }
                        },
                    }
                });
        }

and my IsSelected property:

private bool isSelected = false;
    public bool IsSelected
    {
        get
        {
            return this.isSelected;
        }

        set
        {
            this.isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

And the new SelectColumn code:

public static void SelectColumn(DataGrid grid, int column)
    {
        for (int i = 0; i < grid.Items.Count; i++)
        {
            // Select each cell in this column
            ((DataItem[])(grid.Items[i]))[column].IsSelected = true;
        }
    }

The problem is that if I update the IsSelected property in code, it updates the GUI (kinda, its quirky) but not vice versa. I.e. if I select a cell/row in the GUI, it doesn’t call the property setter in the code. As you can see the binding is TwoWay so I’m not sure the issue.

Another UPDATE: The issue definitely seems to be with virtualization. If i turn off virtualization (VirtualizingStackPanel.IsVirtualizing=”False” ) it works fine.

  • 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-30T22:08:24+00:00Added an answer on May 30, 2026 at 10:08 pm

    A more effective approach would probably be to have IsSelected properties on the DataSource’s class, such that each column has a corresponding “IsSelected” property.

    public class MyData : INotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                Notify("Name");
            }
        }
    
        private bool nameSelected = false;
        public bool NameSelected
        {
            get { return nameSelected; }
            set
            {
                nameSelected = value;
                Notify("NameSelected");
            }
        }
    
      //... etc ...
    }
    

    Next, you can alter the CellStyle for each Column to bind the cells’ IsSelected property to the related IsSelected property on the class.

        <DataGrid ItemsSource="{Binding Users}" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="scratchGrid" CanUserAddRows="False"
                  VerticalScrollBarVisibility="Auto" SelectionUnit="Cell">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="User Name" Width="200">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="IsSelected" Value="{Binding NameSelected}" />
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Age}" Header="User Age" Width="80">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="IsSelected" Value="{Binding AgeSelected}" />
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    Finally, implement your select-all code like so (this does select-all on Age, you may want to make a more generic/elegant implementation 😉 ) :

            foreach (MyData user in Users)
            {
                user.AgeSelected = true;
            }
    

    You’ll have to take care to make sure all your NotifyPropertyChanged behavior is lined up, since you’re expecting the grid to recognize that properties inside its bound collection are being updated.

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

Sidebar

Related Questions

Im trying to submit a specific form programatically, but I allways get the initial
I am trying to provide my own labelFunction for a CategoryAxis programatically but am
I'm trying to rig up a ComboBox with a Datagrid dropdown in WPF. I
I'm trying to programatically select/change the tab of the UITabViewController. I tried doing it
i want to programatically select a radio button in code. The radio button lives
Trying to fill the listbox programatically I have written the following code: protected override
I'm trying to select the first item in a ListView programmatically, but it doesn't
Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.
I am trying to programatically unzip a zipped file. I have tried using the
I'm trying to programatically get a list of installed fonts in C or Python.

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.