By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

how do highlight a specific cell in a winforms .net datagrid?

P: n/a
The datagrid has a tablestyle applied to it, and there
are DataGridTextBoxColumn and each DataGridTextBoxColumn
has an array of DataGridTextBox objects.

Private Sub highlight(ByVal str As String)
Dim myTextBoxcolumn As DataGridTextBoxColumn
Dim myTextBox As DataGridTextBox

'loop through the cells of the datagrid, searching
for the string

For c As Integer = 0 To
DataGrid1.VisibleColumnCount - 1
For r As Integer = 0 To
DataGrid1.VisibleRowCount - 1

'if the content of the sell matches the
str, we want to highlight it.
If CType(DataGrid1.Item(r, c), String) =
str Then

myTextBoxcolumn = _
(0).GridColumnStyles(c), DataGridTextBoxColumn)

myTextBox = CType
(myTextBoxcolumn.TextBox, DataGridTextBox)
myTextBox.BackColor = Color.Red
myTextBox.ForeColor = Color.Blue
End If
End Sub

This routine highlights the entire column, not just a
single cell, moreover, you can only see the highlighting
when the cell is selected (has the focus). Is there
anyway to have the cell highlighted, even when it is not
selected? It seems that the datagrid should be capable of
this, but I cannot find any objects which expose the
necessary properties...

I can highlight an entire row, or a column, but setting
the background color on a single cell, or multiple cells
on different rows and different columns still eludes me.

If any gurus would help, I would be very appreciative.

Kind Regards,
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply

P: 1

i am able to highlight the cell only but the same problem problem as yours i am also getting. i.e. when the user clicks the other cell of the datagrid, then the previously highlighted color of the cell is not preserved. i want to make highlighting that cell despite of user clciking on the other cell.

have u done anything on this. i am sending u the code that can highlight the cell only.

for (int row = 0; row < totalRows - 1; row++)

for (int col = 0; col < totalCols; col++)

string result= ,,,,,,,,// u can call the function of word searching
if (result != null)
int index = ((string)<datagridname>[col, row].Value).IndexOf(result);
int length = result.Length;

dgsource[col, row].Selected = true;

dgsource[col, row].Style.SelectionBackColor = color name;


Jun 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.