c# datagridview set CurrentCell to Editmode - c#

i am working on a scoring program for Darts. I use a Datagridview to enter the scores.
After pressing enter key the program will check if finished or not.
If not, the program adds another row for scoring, and should set the next Cell to CurrentCell in EditMode.
The row is being added fine, but the CurrentCell is selected and not in edit Mode.
Can anyone help me please? Thanks in advance.
private void DGV_score_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
if (DGV_score.CurrentRow.Index == maxrow)
{
DGV_score.Rows.Add();
maxrow = maxrow + 1;
if (leg % 2 != 0)
{
DGV_score.CurrentCell = DGV_score.Rows[maxrow].Cells[0];
DGV_score.BeginEdit(true);
}
if (leg % 2 == 0)
{
DGV_score.CurrentCell = DGV_score.Rows[maxrow].Cells[2];
}
DGV_score.BeginEdit(true);
}
}
}
I tried calling the BeginEdit in CurrentCellChanged instead, but this also doesn't set the cell in EditMode.
private void DGV_score_CurrentCellChanged(object sender, EventArgs e)
{
DGV_score.BeginEdit(true);
}
I have no idea how to fix this, maybe it collides with the way i handle the enter key (I used a solution suggested at this community?
private void DGV_score_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
tb.KeyDown -= DGV_score_KeyDown;
tb.PreviewKeyDown -= DGV_score_PreviewKeyDown;
tb.KeyDown += DGV_score_KeyDown;
tb.PreviewKeyDown += DGV_score_PreviewKeyDown;
}
}

Related

Move Datagridview text box focus to next and not down on barcode read complete

I am working with barcode scanner. On simple text box I am handling KeyPress event by looking for Keys.Return. This help me to know that barcode reading has been completed.
But when working with datagridview text boxes. Barcode reader not sending return on key press event also by default datagridview moves cursor to down after barcodede reading completion.I want to move focus of datagridview text box to next cell and not to down.
How should I handle this?
My current implementation is as follows:
private void dgvListItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxColumn textBox = (DataGridViewTextBoxColumn)dgvMeterData.Columns["MeterId"];
if (dgvMeterData.CurrentCellAddress.X == textBox.DisplayIndex)
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += tb_KeyPress;
}
}
}}
private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tb = sender as TextBox;
if (e.KeyChar==(char)Keys.Return)
{
tb.Text = tb.Text + e.KeyChar;
var columnIndex = dgvMeterData.CurrentRow.Cells["TestResults"].ColumnIndex;
var rowIndex = dgvMeterData.CurrentRow.Cells["TestResults"].RowIndex;
dgvMeterData.CurrentCell = (DataGridViewCell)dgvMeterData[columnIndex, rowIndex];
}
}
This helped me doing almost the same thing. I hope you get the idea.
private void dgvListItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
tb.PreviewKeyDown -= dgvListItems_PreviewKeyDown;
tb.PreviewKeyDown += dgvListItems_PreviewKeyDown;
}
}
private void dgvListItems_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Return)
{
int columnIndex = dgvListItems.CurrentCell.ColumnIndex; ;
int rowIndex = dgvListItems.CurrentCell.RowIndex;
if (columnIndex == dgvListItems.Columns.Count - 1)
{
dgvListItems.CurrentCell = dgvListItems[0, rowIndex + 1];
}
else
{
dgvListItems.CurrentCell = dgvListItems[columnIndex + 1, rowIndex];
}
}
}

How to show listbox on Spacekey press event in DatagridView?

In my Winform application there is a DataGrid view(dataGridView1) and 2 list boxes , listBox1 and listBox2 . In dataGridView1 , there is three columns, Column1 (id) , Column2 (Category) and Column3 (Items) . I want to display listBox1 , containing Categries when user pressing space bar and should focus on Items column after pressing enter button.
I found some solutions, but not working up to my requirements. I want something like,
If (spacebar is pressed && dataGridView1.CurrentCell.ColumnIndex== 2)
{
listbox1.Visible = true;
listbox1.Focus();
listbox1.SelectedIndex = 0;
}
I can't show you my form image because of low reputation.
Thanks to you all !
It's a long time since I've used a datagridview, but if you check its events I think you have a CellEnter and a CellLeave event that you can use to check which column is selected and modify the visible property of your list views.
The event handler of CellEnter has a column index parameter to help you.
at datagridview's keydown event
if (e.KeyCode == Keys.Space)//check if space is pressed
{
if(dataGridView1.CurrentCell.ColumnIndex== 2)
{
listbox1.Visible = true;
listbox1.Focus();
}
}
For checking enter key use this at keydown event of datagrid
if (e.KeyCode == Keys.Enter) //if enter key is pressed change selected index
{
listbox1.SelectedIndex = 3;
}
In column 0 (id), not editing, press enter and move next cell:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter && dataGridView1.CurrentCell.ColumnIndex == 0)
{
dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.CurrentCell.ColumnIndex];
}
}
In column 0 (id), editing, press enter and move next cell (answer from here):
private int currentRow;
private bool resetRow = false;
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (resetRow)
{
resetRow = false;
dataGridView1.CurrentCell = dataGridView1.Rows[currentRow].Cells[1];
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
resetRow = true;
currentRow = e.RowIndex;
}
*Notes: you may need to bind SelectionChanged event after completed binding data to your datagridview instead of binding in design time.
In column 1 (cats) and 2 (items), press spacebar to show list cats and list items:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
((TextBox)e.Control).KeyPress += new KeyPressEventHandler(col1_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 2)
((TextBox)e.Control).KeyPress += new KeyPressEventHandler(col2_KeyPress);
}
void col1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 32)
{
listBox1.Visible = true;
listBox1.Focus();
listBox1.SelectedIndex = 0;
}
}
void col2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 32)
{
listBox2.Visible = true;
listBox2.Focus();
listBox2.SelectedIndex = 0;
}
}

Datagridview checkbox checked when clicking the cell

I handle my checkbox click event with the CurrentCellDirtyStateChanged. What I want to be able to do is handle the same event when I click the cell that contains the checkbox too, i.e. when I click the cell, check the checkbox and call the DirtyStateChanged. Using the following code does not help much, it does not even call the CurrentCellDirtyStateChanged. I've run out of ideas.
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
{
//option 1
(dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell).Value = true;
//option 2
DataGridViewCheckBoxCell cbc = (dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell);
cbc.Value = true;
//option 3
dataGridView.CurrentCell.Value = true;
}
}
As Bioukh points out, you must call NotifyCurrentCellDirty(true) to trigger your event handler. However, adding that line will no longer update your checked state. To finalize your checked state change on click we'll add a call to RefreshEdit. This will work to toggle your cell checked state when the cell is clicked, but it will also make the first click of the actual checkbox a bit buggy. So we add the CellContentClick event handler as shown below and you should be good to go.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
if (cell != null && !cell.ReadOnly)
{
cell.Value = cell.Value == null || !((bool)cell.Value);
this.dataGridView1.RefreshEdit();
this.dataGridView1.NotifyCurrentCellDirty(true);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.RefreshEdit();
}
This should do what you want :
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
{
dataGridView.CurrentCell.Value = true;
dataGridView.NotifyCurrentCellDirty(true);
}
}
private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView3.Columns["Select"].Index)//checking for select click
{
dataGridView3.CurrentCell.Value = dataGridView3.CurrentCell.FormattedValue.ToString() == "True" ? false : true;
dataGridView3.RefreshEdit();
}
}
These Changes had Worked for me!
A more generic approach to OhBeWise solution with a static function in HelperClass
public static void DataGrid_CheckBoxCellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dataGrid = sender as DataGridView;
DataGridViewCheckBoxCell cell = dataGrid?.CurrentCell as DataGridViewCheckBoxCell;
if(cell != null && !cell.ReadOnly)
{
cell.Value = cell.Value == null || !(bool)cell.Value;
dataGrid.RefreshEdit();
dataGrid.NotifyCurrentCellDirty(true);
}
}
In your code behind
dataGridView1.CellClick += HelperClass.DataGrid_CheckBoxCellClick;
Code bellow is not necessary :-)
if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
You can write it simplier:
if(!dataGridView.Columns[e.ColumnIndex].ReadOnly)

How to know a specific checkbox inside datagridview is checked or not?

i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .
As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.
can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
dataGridView1_CurrentCellDirtyStateChanged(sender, e);
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
these below links helped me to understand the concept of cellvalue_changed and cell_content_click..
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
and by the help of these links i finally got the solution to my problem
it is as simple as this
//replace the row number and column name with your own
if ((bool)dataGridView1.Rows[0].Cells["Column1"].Value)
{
//do your work
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
label1.Text = dataGridView1.Rows[e.RowIndex].Cells["Col1"].Value.ToString();
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == *someIndex*)
{
DataGridViewCheckBoxCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (cell != null)
{
if (cell.EditingCellValueChanged)
{
//CheckBox has been clicked
}
//here how to get the checkBoxCell value
var cellChecked = cell.EditingCellFormattedValue;
}
}
}

How to detect DataGridView CheckBox event change?

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either
Triggers as soon as the CheckBox is clicked but before its checked state changes, or
Triggers only once the CheckBox looses its focus
I can't seem to find event that triggers immediately after the checked state changes.
Edit:
What I am trying to achieve is that when the checked state of a CheckBox in one DataGridView changes, the data in two other DataGridViews changes. Yet all the events I have used, the data in the other grids only changes after the CheckBox in the first DataGridView looses focus.
To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...
private void dataGridViewSites_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
/// <summary>
/// Works with the above.
/// </summary>
private void dataGridViewSites_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
UpdateDataGridViewSite();
}
P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx
I found #Killercam's solution to work but was a bit dodgy if the user double clicked too fast. Not sure if other's found that the case either. I found a another solution here.
It uses the datagrid's CellValueChanged and CellMouseUp. Changhong explains that
"The reason for that is OnCellvalueChanged event won’t fire until the DataGridView thinks you have completed editing. This makes senses for a TextBox Column, as OnCellvalueChanged wouldn’t [bother] to fire for each key strike, but it doesn’t [make sense] for a CheckBox."
Here it is in action from his example:
private void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
// Handle checkbox state change here
}
}
And the code to tell the checkbox it is done editing when it is clicked, instead of waiting till the user leaves the field:
private void myDataGrid_OnCellMouseUp(object sender,DataGridViewCellMouseEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
myDataGrid.EndEdit();
}
}
Edit: A DoubleClick event is treated separate from a MouseUp event. If a DoubleClick event is detected, the application will ignore the first MouseUp event entirely. This logic needs to be added to the CellDoubleClick event in addition to the MouseUp event:
private void myDataGrid_OnCellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
{
myDataGrid.EndEdit();
}
}
jsturtevants's solution worked great. However, I opted to do the processing in the EndEdit event. I prefer this approach (in my application) because, unlike the CellValueChanged event, the EndEdit event does not fire while you are populating the grid.
Here is my code (part of which is stolen from jsturtevant:
private void gridCategories_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
{
//do some stuff
}
}
private void gridCategories_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
{
gridCategories.EndEdit();
}
}
Here is some code:
private void dgvStandingOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvStandingOrder.Columns[e.ColumnIndex].Name == "IsSelected" && dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dgvStandingOrder[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked == false)
{
dgvStandingOrder.Rows[e.RowIndex].Cells["Status"].Value = "";
}
dgvStandingOrder.EndEdit();
}
}
private void dgvStandingOrder_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
following Killercam'answer, My code
private void dgvProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dgvProducts.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
and :
private void dgvProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dgvProducts.DataSource != null)
{
if (dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
{
//do something
}
else
{
//do something
}
}
}
This also handles the keyboard activation.
private void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dgvApps.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
{
if (dgvApps.CurrentCell.IsInEditMode)
{
if (dgvApps.IsCurrentCellDirty)
{
dgvApps.EndEdit();
}
}
}
}
private void dgvApps_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// handle value changed.....
}
Ben Voigt found the best solution in a comment-reply above:
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
Seriously, that's ALL you need.
What worked for me was CurrentCellDirtyStateChanged in combination with datagridView1.EndEdit()
private void dataGridView1_CurrentCellDirtyStateChanged( object sender, EventArgs e ) {
if ( dataGridView1.CurrentCell is DataGridViewCheckBoxCell ) {
DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
if ( (byte)cb.Value == 1 ) {
dataGridView1.CurrentRow.Cells["time_loadedCol"].Value = DateTime.Now.ToString();
}
}
dataGridView1.EndEdit();
}
It's all about editing the cell, the problem that is the cell didn't edited actually, so you need to save The changes of the cell or the row to get the event when you click the check box so you can use this function:
datagridview.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
with this you can use it even with a different event.
I have found a simpler answer to this problem. I simply use reverse logic. The code is in VB but it is not much different than C#.
Private Sub DataGridView1_CellContentClick(sender As Object, e As
DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim _ColumnIndex As Integer = e.ColumnIndex
Dim _RowIndex As Integer = e.RowIndex
'Uses reverse logic for current cell because checkbox checked occures
'after click
'If you know current state is False then logic dictates that a click
'event will set it true
'With these 2 check boxes only one can be true while both can be off
If DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False And
DataGridView1.Rows(_RowIndex).Cells("Column3").Value = True Then
DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False
End If
If DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False And
DataGridView1.Rows(_RowIndex).Cells("Column2").Value = True Then
DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False
End If
End Sub
One of the best things about this is no need for multiple events.
I've tried some answers from here, but I've always had some kind of problem (like double clicking or using the keyboard). So, I combined some of them and got a consistent behavior (it's not perfect, but works properly).
void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if(gridView.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell))
return;
if(!gridView.CurrentCell.IsInEditMode)
return;
if(!gridView.IsCurrentCellDirty)
return;
gridView.EndEdit();
}
void gridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
if(e.ColumnIndex == gridView.Columns["cFlag"].Index && e.RowIndex >= 0)
gridView.EndEdit();
}
void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if(e.ColumnIndex != gridView.Columns["cFlag"].Index || e.RowIndex < 0)
return;
// Do your stuff here.
}
The Code will loop in DataGridView and Will check if CheckBox Column is Checked
private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit);
var i = 0;
foreach (DataGridViewRow row in dgv1.Rows)
{
if (Convert.ToBoolean(row.Cells[0].Value))
{
i++;
}
}
//Enable Button1 if Checkbox is Checked
if (i > 0)
{
Button1.Enabled = true;
}
else
{
Button1.Enabled = false;
}
}
}
In the event CellContentClick you can use this strategy:
private void myDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)//set your checkbox column index instead of 2
{ //When you check
if (Convert.ToBoolean(myDataGrid.Rows[e.RowIndex].Cells[2].EditedFormattedValue) == true)
{
//EXAMPLE OF OTHER CODE
myDataGrid.Rows[e.RowIndex].Cells[5].Value = DateTime.Now.ToShortDateString();
//SET BY CODE THE CHECK BOX
myDataGrid.Rows[e.RowIndex].Cells[2].Value = 1;
}
else //When you decheck
{
myDataGrid.Rows[e.RowIndex].Cells[5].Value = String.Empty;
//SET BY CODE THE CHECK BOX
myDataGrid.Rows[e.RowIndex].Cells[2].Value = 0;
}
}
}
The best way that I found (which also doesn't use multiple events) is by handling the CurrentCellDirtyStateChanged event.
private void dataGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridMatten.CurrentCell.OwningColumn == dataGridMatten.Columns["checkBoxColumn"] && dataGridMatten.IsCurrentCellDirty)
{
dataGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
//your code goes here
}
}
To do this when using the devexpress xtragrid, it is necessary to handle the EditValueChanged event of a corresponding repository item as described here. It is also important to call the gridView1.PostEditor() method to ensure the changed value has been posted. Here is an implementation:
private void RepositoryItemCheckEdit1_EditValueChanged(object sender, System.EventArgs e)
{
gridView3.PostEditor();
var isNoneOfTheAboveChecked = false;
for (int i = 0; i < gridView3.DataRowCount; i++)
{
if ((bool) (gridView3.GetRowCellValue(i, "NoneOfTheAbove")) && (bool) (gridView3.GetRowCellValue(i, "Answer")))
{
isNoneOfTheAboveChecked = true;
break;
}
}
if (isNoneOfTheAboveChecked)
{
for (int i = 0; i < gridView3.DataRowCount; i++)
{
if (!((bool)(gridView3.GetRowCellValue(i, "NoneOfTheAbove"))))
{
gridView3.SetRowCellValue(i, "Answer", false);
}
}
}
}
Note that because the xtragrid doesnt provide an enumerator it is necessary to use a for loop to iterate over rows.
Removing the focus after the cell value changes allow the values to update in the DataGridView. Remove the focus by setting the CurrentCell to null.
private void DataGridView1OnCellValueChanged(object sender, DataGridViewCellEventArgs dataGridViewCellEventArgs)
{
// Remove focus
dataGridView1.CurrentCell = null;
// Put in updates
Update();
}
private void DataGridView1OnCurrentCellDirtyStateChanged(object sender, EventArgs eventArgs)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
You can force the cell to commit the value as soon as you click the checkbox and then catch the CellValueChanged event. The CurrentCellDirtyStateChanged fires as soon as you click the checkbox.
The following code works for me:
private void grid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
SendKeys.Send("{tab}");
}
You can then insert your code in the CellValueChanged event.
I use DataGridView with VirtualMode=true and only this option worked for me
(when both the mouse and the space bar are working, including repeated space clicks):
private void doublesGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
var data_grid = (DataGridView)sender;
if (data_grid.CurrentCell.IsInEditMode && data_grid.IsCurrentCellDirty) {
data_grid.EndEdit();
}
}
private void doublesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == CHECKED_COLUMN_NUM && e.RowIndex >= 0 && e.RowIndex < view_objects.Count) { // view_objects - pseudocode
view_objects[e.RowIndex].marked = !view_objects[e.RowIndex].marked; // Invert the state of the displayed object
}
}
this worked for me
private void employeeDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == employeeDataGridView.Columns["employeeStatusColumn"].Index)
{
bool isChecked = (bool)employeeDataGridView.CurrentCell.Value;
if (isChecked)
{
MessageBox.Show("Checked " + isChecked); //out true;
}
else
{
MessageBox.Show("unChecked " + isChecked);
}
}
}
private void employeeDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (employeeDataGridView.DataSource != null)
{
if (e.ColumnIndex == employeeDataGridView.Columns["employeeStatusColumn"].Index && e.RowIndex != -1)
{
employeeDataGridView.EndEdit();
}
}
}
private void dataGridViewPendingBill_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
bool isChecked = (bool) dataGridViewPendingBill[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked)
{
totalAmount += int.Parse(dataGridViewPendingBill.Rows[e.RowIndex].Cells["Amount"].Value.ToString());
textBoxAmount.Text = totalAmount.ToString();
}
else
{
totalAmount -= int.Parse(dataGridViewPendingBill.Rows[e.RowIndex].Cells["Amount"].Value.ToString());
textBoxAmount.Text = totalAmount.ToString();
}
dataGridViewPendingBill.EndEdit();
}

Categories