How to highlight search in DataGrid faster in wpf?How does database indexing work?How do I enumerate an enum in C#?How can you speed up Eclipse?Why is the Android emulator so slow? How can we speed up the Android emulator?How to do case insensitive search in VimWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Why does Python code run faster in a function?Is < faster than <=?Why is [] faster than list()?
Accidentally cashed a check twice
Is it possible to kill all life on Earth?
How to detach yourself from a character you're going to kill?
What does the behaviour of water on the skin of an aircraft in flight tell us?
Is there a rule that prohibits us from using 2 possessives in a row?
Modern approach to radio buttons
what's the equivalent of helper in LWC?
How should I push back against my job assigning "homework"?
What caused the tendency for conservatives to not support climate change regulations?
How crucial is a waifu game storyline?
What is a simple, physical situation where complex numbers emerge naturally?
Could a guilty Boris Johnson be used to cancel Brexit?
Can you please explain this joke: "I'm going bananas is what I tell my bananas before I leave the house"?
Can an old DSLR be upgraded to match modern smartphone image quality
Beginner's snake game using PyGame
Orientable with respect to complex cobordism?
How can I grammatically understand "Wir über uns"?
Rotated Position of Integers
What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?
Is American Express widely accepted in France?
Are there mythical creatures in the world of Game of Thrones?
How to write a vulnerable moment without it seeming cliche or mushy?
Coding Challenge Solution - Good Range
How do I get a cleat that's stuck in a pedal, detached from the shoe, out?
How to highlight search in DataGrid faster in wpf?
How does database indexing work?How do I enumerate an enum in C#?How can you speed up Eclipse?Why is the Android emulator so slow? How can we speed up the Android emulator?How to do case insensitive search in VimWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Why does Python code run faster in a function?Is < faster than <=?Why is [] faster than list()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am searching in a datagrid through a textbox and my code highlights the searched text in datagrid cells and filter the rows according to the search.This works fine for small number of rows but as the row increase it gets very slow.If someone can suggest anything to make it more efficient, I would be thankful :)
To highlight search text in datagrid cells
public void HighlightSearch()
if (string.IsNullOrEmpty(SearchText))
if ((dataGrid.ItemsSource as DataView).RowFilter == "")
return;
string searchtext = SearchText;
Regex SearchRegex = new Regex(searchtext, RegexOptions.IgnoreCase);
if (dataGrid.ItemsSource != null)
for (int i = 0; i < dataGrid.Columns.Count; i++)
var checkbox = SearchColumns[i];
if (checkbox.IsChecked == true)
for (int j = 0; j < dataGrid.Items.Count; j++)
DataGridCell cell = GetCell(j, i);
string[] substrings =null;
if (cell.Content is TextBlock itx)
Regex regex = new Regex("(" + SearchText + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
if (SearchText.Length == 0)
string str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
else
substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
if (regex.Match(item).Success)
Run runx = new Run(item);
runx.Background = Brushes.Blue;
tb.Inlines.Add(runx);
else
tb.Inlines.Add(item);
To Filter the rows
private void FilterGrid()
if (dataGrid.ItemsSource != null)
string query = "";
foreach (CheckBox checkbox in SearchColumns)
if (checkbox.IsChecked == true)
// to check if its the first columns and form the appropriate filter query ;)
if (query == "")
query = $"Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
else
query += $" or Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
(dataGrid.ItemsSource as DataView).RowFilter = query;
To get each cell of a datagrid i followed this link https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a1872d9-a50b-4bf3-81fc-db0ac4a26d1c/search-in-a-datagridlistview-and-highlight-text?forum=wpf
public DataGridCell GetCell(int row, int column)
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
return null;
public DataGridRow GetRow(int index)
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
return row;
public static T GetVisualChild<T>(Visual parent) where T : Visual
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild<T>(v);
if (child != null)
break;
return child;
c# wpf performance search datagrid
add a comment |
I am searching in a datagrid through a textbox and my code highlights the searched text in datagrid cells and filter the rows according to the search.This works fine for small number of rows but as the row increase it gets very slow.If someone can suggest anything to make it more efficient, I would be thankful :)
To highlight search text in datagrid cells
public void HighlightSearch()
if (string.IsNullOrEmpty(SearchText))
if ((dataGrid.ItemsSource as DataView).RowFilter == "")
return;
string searchtext = SearchText;
Regex SearchRegex = new Regex(searchtext, RegexOptions.IgnoreCase);
if (dataGrid.ItemsSource != null)
for (int i = 0; i < dataGrid.Columns.Count; i++)
var checkbox = SearchColumns[i];
if (checkbox.IsChecked == true)
for (int j = 0; j < dataGrid.Items.Count; j++)
DataGridCell cell = GetCell(j, i);
string[] substrings =null;
if (cell.Content is TextBlock itx)
Regex regex = new Regex("(" + SearchText + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
if (SearchText.Length == 0)
string str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
else
substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
if (regex.Match(item).Success)
Run runx = new Run(item);
runx.Background = Brushes.Blue;
tb.Inlines.Add(runx);
else
tb.Inlines.Add(item);
To Filter the rows
private void FilterGrid()
if (dataGrid.ItemsSource != null)
string query = "";
foreach (CheckBox checkbox in SearchColumns)
if (checkbox.IsChecked == true)
// to check if its the first columns and form the appropriate filter query ;)
if (query == "")
query = $"Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
else
query += $" or Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
(dataGrid.ItemsSource as DataView).RowFilter = query;
To get each cell of a datagrid i followed this link https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a1872d9-a50b-4bf3-81fc-db0ac4a26d1c/search-in-a-datagridlistview-and-highlight-text?forum=wpf
public DataGridCell GetCell(int row, int column)
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
return null;
public DataGridRow GetRow(int index)
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
return row;
public static T GetVisualChild<T>(Visual parent) where T : Visual
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild<T>(v);
if (child != null)
break;
return child;
c# wpf performance search datagrid
add a comment |
I am searching in a datagrid through a textbox and my code highlights the searched text in datagrid cells and filter the rows according to the search.This works fine for small number of rows but as the row increase it gets very slow.If someone can suggest anything to make it more efficient, I would be thankful :)
To highlight search text in datagrid cells
public void HighlightSearch()
if (string.IsNullOrEmpty(SearchText))
if ((dataGrid.ItemsSource as DataView).RowFilter == "")
return;
string searchtext = SearchText;
Regex SearchRegex = new Regex(searchtext, RegexOptions.IgnoreCase);
if (dataGrid.ItemsSource != null)
for (int i = 0; i < dataGrid.Columns.Count; i++)
var checkbox = SearchColumns[i];
if (checkbox.IsChecked == true)
for (int j = 0; j < dataGrid.Items.Count; j++)
DataGridCell cell = GetCell(j, i);
string[] substrings =null;
if (cell.Content is TextBlock itx)
Regex regex = new Regex("(" + SearchText + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
if (SearchText.Length == 0)
string str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
else
substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
if (regex.Match(item).Success)
Run runx = new Run(item);
runx.Background = Brushes.Blue;
tb.Inlines.Add(runx);
else
tb.Inlines.Add(item);
To Filter the rows
private void FilterGrid()
if (dataGrid.ItemsSource != null)
string query = "";
foreach (CheckBox checkbox in SearchColumns)
if (checkbox.IsChecked == true)
// to check if its the first columns and form the appropriate filter query ;)
if (query == "")
query = $"Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
else
query += $" or Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
(dataGrid.ItemsSource as DataView).RowFilter = query;
To get each cell of a datagrid i followed this link https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a1872d9-a50b-4bf3-81fc-db0ac4a26d1c/search-in-a-datagridlistview-and-highlight-text?forum=wpf
public DataGridCell GetCell(int row, int column)
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
return null;
public DataGridRow GetRow(int index)
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
return row;
public static T GetVisualChild<T>(Visual parent) where T : Visual
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild<T>(v);
if (child != null)
break;
return child;
c# wpf performance search datagrid
I am searching in a datagrid through a textbox and my code highlights the searched text in datagrid cells and filter the rows according to the search.This works fine for small number of rows but as the row increase it gets very slow.If someone can suggest anything to make it more efficient, I would be thankful :)
To highlight search text in datagrid cells
public void HighlightSearch()
if (string.IsNullOrEmpty(SearchText))
if ((dataGrid.ItemsSource as DataView).RowFilter == "")
return;
string searchtext = SearchText;
Regex SearchRegex = new Regex(searchtext, RegexOptions.IgnoreCase);
if (dataGrid.ItemsSource != null)
for (int i = 0; i < dataGrid.Columns.Count; i++)
var checkbox = SearchColumns[i];
if (checkbox.IsChecked == true)
for (int j = 0; j < dataGrid.Items.Count; j++)
DataGridCell cell = GetCell(j, i);
string[] substrings =null;
if (cell.Content is TextBlock itx)
Regex regex = new Regex("(" + SearchText + ")", RegexOptions.IgnoreCase);
TextBlock tb = itx as TextBlock;
if (SearchText.Length == 0)
string str = tb.Text;
tb.Inlines.Clear();
tb.Inlines.Add(str);
else
substrings = regex.Split(tb.Text);
tb.Inlines.Clear();
foreach (var item in substrings)
if (regex.Match(item).Success)
Run runx = new Run(item);
runx.Background = Brushes.Blue;
tb.Inlines.Add(runx);
else
tb.Inlines.Add(item);
To Filter the rows
private void FilterGrid()
if (dataGrid.ItemsSource != null)
string query = "";
foreach (CheckBox checkbox in SearchColumns)
if (checkbox.IsChecked == true)
// to check if its the first columns and form the appropriate filter query ;)
if (query == "")
query = $"Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
else
query += $" or Convert([checkbox.Content], System.String) LIKE '%SearchText%'";
(dataGrid.ItemsSource as DataView).RowFilter = query;
To get each cell of a datagrid i followed this link https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a1872d9-a50b-4bf3-81fc-db0ac4a26d1c/search-in-a-datagridlistview-and-highlight-text?forum=wpf
public DataGridCell GetCell(int row, int column)
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
return null;
public DataGridRow GetRow(int index)
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
return row;
public static T GetVisualChild<T>(Visual parent) where T : Visual
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild<T>(v);
if (child != null)
break;
return child;
c# wpf performance search datagrid
c# wpf performance search datagrid
edited Mar 24 at 11:44
Abdul Rafay
asked Mar 24 at 11:14
Abdul RafayAbdul Rafay
34
34
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323212%2fhow-to-highlight-search-in-datagrid-faster-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323212%2fhow-to-highlight-search-in-datagrid-faster-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown