DataGridView make Cells unselectable - c#

Hi I have c# datagridview and everytime when I hit TAB i got focused cell in my datagridview. Does anyone know how to deny focusing cells in datagridview? I need to mark only row not cells.
Thank you

I see the problem. You're taking about focus rectangle. You can prevent it by subscribing RowPrePaint event and remove the focus PaintPart.
private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts &= ~DataGridViewPaintParts.Focus;
}
Original answer

Related

How can I get dataGridView user input?

How to detect and get user input from dataGridView cell?
For example, I want to put an integer into a cell and after I submit, it would make the calculations using it.
Solution:
I chose CellValidated event
private void dataGridView1_CellValidated(object send, DataGridViewCellEventArgs e)
{
label1.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
Simply, it will give you the value of the last edited cell and show it in the label.
Thanks for giving references.
Implement CellEndEdit event and use the cell property FormattedValue/EditedFormattedValue to get the edited value.

Datagridview checkbox column has a dead area

I have a datagridview (winforms) with a checkbox column as well as other text-based columns. I've successfully worked through most of the common issues around checkbox columns which are very well documented on this site.
However, I have 1 remaining problem. I am able to click "directly" on a checkbox and it does respond the way I want. However, if I carefully move the mouse pointer between the cell boundary and the checkbox control, and mouse click, I am able to select the cell but the state of the checkbox does not toggle. This problem is much more evident when the row height is bigger for a given row.
Thanks for any help
NOTE: this is not, I repeat NOT, the issue that occurs when focus moves off a given checkbox cell after it is checked. I have that one solved.
This is not an issue. This is simply how it is supposed to work. For a grid column you can have cellclick events and cellcontentclick events. Since I want the checkbox to check when I click anywhere inside the cell, I should use cellclick. Among other events you will need to listen for, I added the following to my code:
private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == 1) && e.RowIndex != -1)
{
this.MyGrid[1, e.RowIndex].Value = !(bool)this.MyGrid[1, e.RowIndex].Value;
this.MyGrid.EndEdit();
}
}

Checkbox in DataGridView does not change its value after checking it

I have checkboxes in one of my columns in DataGridView.
And now I have a problem: When I click once on Checkbox it changes but only visualy, in code its value is still set to false. But if I click on Checkbox and then anywhere else on my datagridview (or change its value manually in code on true) it changes its value on true.
How can I force my checkbox to change value after one click? (it's annoying that checking checkbox actually does not check it).
Thanks for any help.
Changes to underlying data source are applied when the controls lose the focus.
You can handle it explicitly in CellContentClick event.
Please read the linked documentation thoroughly, as it describes similar scenarios, and discusses different type of grid cells.
Found also this. Exactly the same problem.
Lets assume like you have a form with name Form1
A DataGridView in it name dgv
A CheckBoxColumn in it with ColumnIndex = 5
public Form1()
{
InitializeComponent();
dgv.CellContentClick += dgv_CellContentClick;
}
Commit the change for datagrid within the event for that specific column
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5) dgv.EndEdit();
}

Refresh Table Adapter affects cell focus

I'm using the following code to commit cell changes to the database and calculate the value of another cell in the database. While I realize it isn't the norm to store a calculated value, I haven't been able to find a way to calculate the cell at the database level.
private void dgvPlayers_CellValidated(object sender, DataGridViewCellEventArgs e)
{
this.Validate();
.
.
.
playerListBindingSource.EndEdit();
playerListTableAdapter.UpdatePlayerAvg(Convert.ToDecimal(average), Convert.ToDecimal(dgvPlayers.Rows[e.RowIndex].Cells[0].Value));
playerListTableAdapter.Update(dsPlayerTeam.PlayerList);
this.playerListTableAdapter.Fill(this.dsPlayerTeam.PlayerList);
}
}
The problem I've run into is that TableAdapter.Fill method causes unexpected changes to the cell focus. If I'm tabbing through the fields, everything works as expected, however if I use the cursor keys to change rows, then the focus will shift to a different column, usually (but not always) the first column.
I thought I could store the row and column indexes of the current cell and key pressed in a keydown event then manually set the focus at the end of my CellValidated handler. What I found was that the keydown event I attached to the DataGridView was never called probably because the DataGridView is actually a collection of controls. This was what I tried:
dgvPlayers.KeyDown += new KeyEventHandler(dgvPlayers_KeyDown);
.
.
.
private void dgvPlayers_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
DataGridView theGrid = (DataGridView)sender;
dgvPlayersKeyPress = e.KeyCode;
}
Since I was never able to collect the Key press, I was never able to manually set the focus.
Are there any other options here? Perhaps there's a way to refresh the bound data for a specific cell rather than .Fill for the whole tableadapter?
I still haven't found a suitable solution to this problem. Does anyone have a recommendation? What I've come to understand is that the cell that when it performs the .Fill, it clears the table, then regenerates the rows which explains the strange focus behaviour. I'm starting to think that I should unbind the datagridview, and manually read and write data to the database. I would really prefer to avoid this.
ThanX!

C# DataGridView not updating database on keyboard return

I have a DataGridView with a CellEndEdit function that updates my database. However, it only works if the user clicks off the edited cell onto a different row. If the user presses Return on the keyboard, the change is lost.
I have looked at the variables in debug mode and found that pressing Return does set everything the same as clicking the next row. Any ideas would be great!
Update Method:
private void Grd_RawLedger_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
raw_LedgerTableAdapter.Update(belvan_GL_EEA_M12DataSet.Raw_Ledger);
this.raw_LedgerTableAdapter.Fill(this.belvan_GL_EEA_M12DataSet.Raw_Ledger);
}
Have you tried using the RowValidated Event instead of the CellEndEdit Event?
This should do what you are asking, the only big difference is that instead of saving per cell change it will save per row change. You would also need to make sure that you are only saving things that have changed.

Categories