473,394 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Highlighting entire row when cell is clicked in datagrid

Hi:

Using the WinForms DataGrid, I would like to have the entire row highlight
rather than just the cell that was last clicked. How do you do this?

Thanks,
Charlie
Nov 16 '05 #1
2 7057
Charlie,

I wanted something like this too. I created a new control called a
DataBrowser -- just like a DataGrid but with a few changes...

public class DataBrowser : System.Windows.Forms.DataGrid
{
}

I wanted it to work like a multi-column listbox: you click on a cell and
the whole row gets highlighted. I also wanted to prevent columns from
being resized.

I used the following function to get that behavior:

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hitTestInfo = this.HitTest(e.X, e.Y);
if (hitTestInfo.Type == HitTestType.ColumnResize
|| hitTestInfo.Type == HitTestType.RowResize) {
return;
}
base.OnMouseDown(e);
if ( hitTestInfo.Type == HitTestType.Cell) {
SelectRow(hitTestInfo.Row);
}
return;
}

That function reacts to the mouse click and calls my other function,
SelectRow, which in turn calls HighlightRow...

public void SelectRow(int index)
{
HighlightRow(index);
this.CurrentRowIndex = index;
return;
}
int highlightedRow = -1; // we use -1 to mean no selection
private void HighlightRow(int index)
{
CurrencyManager currencyManager;
currencyManager = (CurrencyManager) this.BindingContext[this.DataSource];
int rowCount = currencyManager.Count;
if (index < 0 || index > rowCount - 1)
return;
if (highlightedRow > -1 && highlightedRow < rowCount)
this.UnSelect(highlightedRow);
this.Select(index);
highlightedRow = index;
return;
}

Finally, one more function:

protected override void OnCurrentCellChanged(System.EventArgs e)
{
HighlightRow(this.CurrentCell.RowNumber);
base.OnCurrentCellChanged(e);
return;
}

That got it working for me. Put it all in a class and use the new
control in the designer.

I'm sure the code could use refinement. One problem is that if you use
the keyboard to navigate past the top or bottom of the list, you can
make the highlight disappear even though a record is selected. If you
improve it, share back.

-Patrick
Charlie@CBFC wrote:
Hi:

Using the WinForms DataGrid, I would like to have the entire row highlight
rather than just the cell that was last clicked. How do you do this?

Thanks,
Charlie

Nov 16 '05 #2
Hi:
I made the same thing (I think) only by doing this:
private void dataGrid1_Click(object sender, System.EventArgs e)
{
dataGrid1.Select(dataGrid1.CurrentRowIndex);
}
I also associated the CurrentCellChanged Event with the same function.

Best Regards

Emo Markov
"Patrick B" wrote:
Charlie,

I wanted something like this too. I created a new control called a
DataBrowser -- just like a DataGrid but with a few changes...
Charlie@CBFC wrote:
Hi:

Using the WinForms DataGrid, I would like to have the entire row highlight
rather than just the cell that was last clicked. How do you do this?

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
6
by: Kshitij | last post by:
Hi Is it possible to highlight the clicked row in a datagrid? How to do it Thanks in advance.
14
by: lance | last post by:
Hi, I am a JS newbie. Hopefully the answer to my question is not trivial. I have written a simple webpage that presents a table. A JS function allows the viewer to click on a cell and each...
1
by: orekinbck | last post by:
Hi There I have a datagrid whose main purpose in life is to provide a nice way for users to make a single choice from a list. The grid is read only, single row select and has its data source as...
0
by: melanieab | last post by:
Hi, I have my own custom DataGridTableStyle when I load the data in a datagrid. But when I click on a cell, while the mouse is down, the entire cell background turns an off-white. When the mouse...
7
by: JohnSmith90 | last post by:
I want to highlight the entire cell data for all rows of a particular column in a data grid. I do not want to fill the back ground color of the cell. How to do that in a data grid? <span...
2
by: Bunnist-Priest | last post by:
Hello Everyone, My first predicament is that I want to make a colomn of my vb.net DataGrid a ProgressBar colomn. (full of Pr.Bars). Is it that difficult? Is there a code example? using - Dim...
0
by: Robert Smith | last post by:
Hello, I have a problem whereby I wish to programmaticlly change the current row of the datagrid to a specific row. This is so that when the user clicks on a grid row and the datavalidation fails...
2
by: benfly08 | last post by:
Hi, everyone. I got a weird problem for my C# program. In my program, I use a DataGrid to display data. I bind the DataGrid to a DataTable. I made all the cells in DataGrid ReadOnly as True. So...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.