-
April 23rd, 2010, 03:27 AM
#1
[RESOLVED] Get selected row?
On clicking a certain cell or row in my table panel layout, the background of that particular row should be highlighted. How do I determine which row has currently been clicked?
Code:
protected void CreateTblPnl(object sender, EventArgs e)
{
(...)
tblPnl.Click += new EventHandler(tblPnl_Click);
}
protected void tblPnl_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblPnl.ColumnCount; i++)
{
for (int j = 0; j < tblPnl.RowCount; j++)
{
Control control = tblPnl.GetControlFromPosition(i, j);
if (control.Focus) // Doesnt work!!
{
tblPnl.BackColor = ColorTranslator.FromHtml("#3A9DCB"); // Ensure only selected row is coloured!!
}
}
}
// OR:
//TableLayoutPanelCellPosition pos = this.tblPnl.GetCellPosition(e);
//Point loc = this.tblPnl.PointToClient(new Point(e.X, e.Y));
//int row = pos.Row;
//int col = pos.Column;
}
-
April 24th, 2010, 08:43 AM
#2
Re: Get selected row?
Add an onclick event handler to each cell pointing to a single handler method. In that method, extract the name of the firing cell and change its color:
I'm not certain what type of control your cells are, so in this example, I'm using a TextBox. Just change the type of control to suit your purpose.
private void genericOnClickMethod(object sender, EventArgs e){
TextBox t = (TextBox)sender;
...(act on "t" to change its style)
t.BackgroundColor, etc....
}
-
April 26th, 2010, 03:52 AM
#3
Re: Get selected row?
That would however only change the colour of the current cell clicked. I would however like the whole row (consisting of 4 cells) to be selected so that I can perform further functions such as highlighting that whole row or deleting it.
-
May 4th, 2010, 08:36 AM
#4
Re: Get selected row?
I resolved it using a for loop to go through every cell in the table and then using a variable "rowIndex" and another "colIndex" to determine the index of the cell that had been selected. I then changed the background color of that cell as well as the rest of the cells on the row (by determining in which column the cell was located). Not the best way but it more or less works....
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|