I am currently developing an DataGridView to store PDF-Files via Drag and Drop.
The user is able to d'n'd the file into the DataGridView and the PDF is stored to an container.
Then I wanted to realize a ContextMenu which allows the user to open or delete the PDF by right clicking on it and selecting the targeted option.
The option "Open" is working fine by using the HitTest(x, y) with the CursorPosition.
My problem is, that as you see the "Delete" button is placed in the cell bellow and the HitTest(x, y) will deliver me the cell right below, which is not my goal.
What I have tried
I've tried it by catching the CellContentClick and Click events, but those are not triggered by right clicking the cell. Also the option by saving the last entered cell by catching the CellMouseEnter event is not working propably
Is there any posibility to get to know which cell was right clicked?
In Your dataGridView enable the Single Cell Selection property and then on click of your context menu you can get the selected cell
You can get the selected cell by
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
}
}
Related
I have a datagridview, a list with the same content as the datagridview and a textbox, when I change the row, the content of the textbox changes according to the row that I have selected (they are not linked through databind, but I change the content manually). Up to here everything works fine.
Now, I need that, if I modify the content of the textbox, and I change the row selected in the datagridview, before changing the row selected, check if the content of the textbox has been modified, and if it has, ask if I want to save the changes in the list.
For this task, I thought of using the RowEnter event of the datagridview, comparing the content of the textbox with the content of the list, but I ran into a difficulty, when I put a MessageBox inside the rowenter event, to ask if I want to save or not, no matter if I answered yes or no, the row of the datagridview does not change, and I want it to change the row right after asking, whether or not I saved the textbox change, or if there is another way to check if I change the content of the textbox with respect to the list, after changing the row in the datagridview
I leave part of the code that I am using.
private void dgvVisitas_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (numeroFilaVisitaActual == e.RowIndex)
{
if (txtNotasVisita.Text != listaVisitasPaciente[e.RowIndex].Notas)
{
MessageBox.Show("No es igual"); // If this Message is show, the row don't change
}
}
numeroFilaVisitaActual = e.RowIndex;
}
I have a dataGridView and I need to get the first cell's value, by clicking on any cell of it's row
for example:
in row num.2, I might click on cell[0]/ cell[1]/ cell[2] or any other cell
what I need is to get cell[0]'s value of the row, no matter which one of it's cells I click on
the peace of code I'm using, only allows me to get the value, only by clicking on the 0th cell itself, not when another cell of it's row is clicked on:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
id = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
}
any help would be appreciatedā„
First of all please be aware that the CellClick event might not be what you want.
From MS documentation:
To determine when the cell contents are clicked, handle the CellContentClick event.
Have a closer look here: Microsoft DataGridView documentation
Nevertheless, it's hard to guess without having the code where you bind the event. But I would say the problem is somewhere in the code where you attach the event listener.
My WinForm project in c#, I have DataGridview that i want to disable cell selection with mouse clicking in editing.
To be more specific; when the user keypress"=" en event fired and disable to select cell by clicking.I want to do something different instead of cell selection. Only by keypressing"ESC" or "Enter" allow to leave cell editing.
I added picture for understanding.Please help.
I'm a beginner in C# and I'm trying to style a DataGridView button in Windows Forms.
I don't know how to, as example, remove the border from a button or change the hover color.
Many configurations that are on a normal button are missing in the DataGridView settings.
How can I achieve a full editable button inside DataGridView?
Datagridview works a little different than normal buttons,
but you can still edit several things in it if you search for the subproperties inside the properties. Let's go in a bit of detail:
DefaultCellStyle
Once you've selected your dataGridView, go to properties > RowTemplate. In there, you find something called DefaultCellStyle. if you press on the '...' at the right. then it'll open a popup that allows you to change some of the standard design of the Cells.
The same can also be applied to ColumnHeadersDefaultCellStyle, which is almost the same as DefaultCellStyle.
Columns
You can also go to Columns and add a new Column. After you've added a new column, you're also able to set several properties unique to that Column as well. You can even set all the cells in a Column to act as buttons!
Datagridview has certainely the access to customise, but most of them are divided in the cellstyles and column collections.
Changing the backcolor on hover
This in't as easily done as on a normal button, I did a search and came with this solution:
In properties, at the lightning button, you can see the events, there you can doubleclick on 'CellMouseMove' and add this. (Change the datagridview1 name eventually to be equall to yours)
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
}
Then doubleclick the 'CellMouseLeave' event so it can revert the color.
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
}
I hope this has helped you further.
I have a DataGridView inside of the winform with delete event handled from toolstrip menu which delete the selected row (along with the object associated with that row in the DataGridView).
Now when i try to edit the value of any cell (to rename the object), and in the edit mode if i select some characters and press delete, it delete the row instead of deleting those selected characters.
What should i do to make the cell handle the event rather than the Winform.
snippets below:
this.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;
this.deleteToolStripMenuItem.Click += new System.EventHandler(this.OnDelete);
private void OnDelete(object sender, EventArgs e)
{
}
try to check the properties of of your DataGridView SelectionMode, must not FullRowSelect
Found a solution :)
when i enter into edit mode i remove the delete key association
this.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.None;
and once i am done editing the cell in the OnAfterEdit i set it back to
this.deleteToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;
Hope it helps someone.