473,791 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7076
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(Sys tem.Windows.For ms.MouseEventAr gs e)
{
DataGrid.HitTes tInfo hitTestInfo = this.HitTest(e. X, e.Y);
if (hitTestInfo.Ty pe == HitTestType.Col umnResize
|| hitTestInfo.Typ e == HitTestType.Row Resize) {
return;
}
base.OnMouseDow n(e);
if ( hitTestInfo.Typ e == HitTestType.Cel l) {
SelectRow(hitTe stInfo.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(in dex);
this.CurrentRow Index = index;
return;
}
int highlightedRow = -1; // we use -1 to mean no selection
private void HighlightRow(in t index)
{
CurrencyManager currencyManager ;
currencyManager = (CurrencyManage r) this.BindingCon text[this.DataSource];
int rowCount = currencyManager .Count;
if (index < 0 || index > rowCount - 1)
return;
if (highlightedRow > -1 && highlightedRow < rowCount)
this.UnSelect(h ighlightedRow);
this.Select(ind ex);
highlightedRow = index;
return;
}

Finally, one more function:

protected override void OnCurrentCellCh anged(System.Ev entArgs e)
{
HighlightRow(th is.CurrentCell. RowNumber);
base.OnCurrentC ellChanged(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.EventArg s e)
{
dataGrid1.Selec t(dataGrid1.Cur rentRowIndex);
}
I also associated the CurrentCellChan ged 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
7934
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 header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
6
1838
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
3417
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 cell with the same content has the background color changed to lime green. Each cell that was previously highlighted has the background color changed back to white. This works great with Firefox but does not work correctly with IE. In IE, none...
1
1886
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 an ArrayList. I came up against the issue of selecting a cell and getting the whole row to highlight, and the second issue of unhighlighting any previously selected row. To get around these two issues I have put in a two prong approach:
0
1040
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 comes back up, everything becomes the way it should (the selected row backcolor and forecolor turn the colors I tell it to in my DataGridTableStyle). What can I do to prevent this off-white backcolor on Mousedown? I've tried firing the mousedown...
7
1868
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 style='background-color: #FFFF04'>highlighted Text</span>
2
1644
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 ColStyle as New DataGridTextBoxColomn ColStyle.TextBox.Contains(ProgressBar1) didn't show anything inside.
0
1092
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 then the cursor and highlight should move back to the original cell. The code I have so far is as such Dim IntLastRow as integer
2
2088
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 I can only Click on the cell but not change them. Now, after I display the data, I clicked on one cell, then I clicked a button which will *virtually* go to the other form.(The reason why I said *virtually* is because I only use one form for...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.