DatagridView Checkbox Checked? - c#

i have a check box in a datagridview windows form and have a event handler cell_Click
on cell click i check the datagridview column for a check box it shows true if the cell is selected too(that is the check box is unchecked and only the the datagrid view cell is selected) and the check box is not selected .i tried for the column gettype and found out the type it shows DatagridViewCheckBox but wrong checked values .???

If I understand you correctly you are saying the checkbox value does not align with the underlying data?
This may well be because the data has been updated and is 'dirty', e.g. it hasn't been committed to the datasource yet. If you add an event handler like this:
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Then that should update the datasource and you'll have the correct checkbox state when you query the cell.

Several things here:
the cell click event just means that the user clicked with the mouse button on the data grid view, what you're looking for is probably the CellValueChanged
this event will give you the coordinates of the cell that changed. You should check to see if it's in your check box column, then get a reference to the cell and you can check the cell.Value to see if it's true or false. You're not going to find any values on the DataGridViewCheckBoxColumn -- it's going to be at the cell level, and you'll always find the value stored in cell.Value, no matter what type of column it is.

Related

Comparing DataGridView's changed cell to the DGV's original value in that cell

In a C# WinForms project I'm trying to highlight a cell that the user changes a value in (change the BackColor) for later review to allow the user to adjust any changes they make and then finally to save those changed cells to the database on a button click.
I've been using the DGV's CellValueChanged event to call the function that will check for changes and highlight the changed cell. The CellValueChanged event is just this:
private void dgvCategories_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
CompareDgvToDataTable(dtCategories, e.RowIndex, e.ColumnIndex);
}
CompareDgvToDataTable() looks like this at the moment:
private void CompareDgvToDataTable(DataTable dt, int intRowIndex, int intColumnIndex)
{
if (dt.Rows[intRowIndex][intColumnIndex].ToString() != dgvCategories.Rows[intRowIndex].Cells[intColumnIndex].Value.ToString())
{
// Change the BackColor
}
}
dtCategories is a variable available to the whole form and is (re)populated whenever the DGV gets rebuilt. As you can see I'm not really doing anything yet. I'm just trying to make sure I'm getting the correct cell and can properly compare the original and user-edited values for now.
However my comparison isn't behaving the way that I expected it to.
It was my assumption that the DGV's DataTable doesn't change until the DGV's datasource is reloaded, but that's apparently not the case. The above if () check always evaluates to false (both sides of the comparison are the same value), as the DataTable's cell and the corresponding DGV cell are the same at that point.
How do I access the original value that the DGV was loaded with to compare that to what the user changes it to - including if the user changes the value back to the original DataTable's 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();
}

DataGridView row is being cleared at validation

I have a DataGridView which I need to run a CellValidating event to ensure that only valid values are selected from a ComboBox. This is needed as the ComboBox contains dummy rows used to display the category, with the fields the user can select listed underneath each category.
Whilst I have the validation code working fine, there is an unwelcome side-effect that all values are being wiped from the row being validated. I have stripped the code in the Event handler down to this, and the issue still occurs:
private void dgvInformation_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
If I remove the
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
line then the issue does not occur.
The DGV is unbound which I believe is causing the issue. As a test I have made a simple form and populated the DGV values unbound, and each time the CellValidating event fires that row is wiped out, but when I create a List<> and use that as the DataSource the values are not wiped out. Could this be a bug with unbound DGVs?
Many thanks
I have problems with the sentense: "to ensure that only valid values are selected from a ComboBox." Using a combobox should actually prevent wrong values beeing tipped in controls like textbox, why dont you just show valid values in the combobox, or validate after all sellections have been made if you need to validate a combination of comboboxes sellection, therefor you may need somthing like a Submit button to run the validation routine.
If you still think you have to validate the combobox after each sellection then you ahve to run the validation somehow on a SelectionChanged event of the combobox.

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?
Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.
Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.
You've got a few questions here.
(1) How can I make a specific column uneditable in DataGridView?
Set the ReadOnly flag on the particular column you want to make uneditable.
dataGridView.Columns["YourColumnName"].ReadOnly = true;
(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?
If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).
(3) How can I make the header title align in the center?
Set the HeaderCell.Style.Alignment
dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

Categories