selecting checkbox inside datagridview c# - c#

i have a dowpdownlist, a button and a checkbox inside the datagridview.
i just only manually created a check box column on the datagridview.
(here is the code)
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
DataGrid1.Columns.Add(CheckboxColumn);
here is the procedure.
step 1: the user will choose item on the checkbox.
step 2: the user will choose item on the dropdown.
Step 3: the user will click on the button and it will change the itemname
on the checkbox prior to the item selected on the dropdownlist.
here is my problem
after clicking on the button, nothings happen.
here is my code.
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
if (chk.Selected)
{
// codes here
}
else
{
//code here
}
}
x = x + 1;
}

* EDITED **
I've tested this and it definitely Works. Copy and paste this into a new project and play with it. It should help you get to where you need to be.
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn(true);
checkBox.HeaderText = "T/F";
dataGridView1.Columns.Add(checkBox);
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
if (Convert.ToBoolean(chk.Value) == true)
{
MessageBox.Show("Value Is True");
}
}
}

First thing that I would recommend to call:
DataGrid1.EndEdit();
Since, I have experienced that sometimes input do not appears as expected if this line is missing before retrieving a value of checkbox from the grid column.
So something like this:
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
if (chk.Value)
{
// codes here for checked condition
}
else
{
//code here for UN-checked condition
}
}
x = x + 1;
}

Related

Windows Form->DataGridView->DataGridViewCheckBoxColumn Uncheck All Leaves One Item Checked

We have a very strange problem in a Windows Form that we cannot seem to figure out.
Our Windows Form has a DataGridView with a DataGridViewCheckBoxColumn in the first column.
We've added a the following functionality that allows a user to shift->click to select multiple rows in this grid:
int colHit = gvLibrary.HitTest(e.X, e.Y).ColumnIndex;
int lastRowHit;
//mouse left click
if (e.Button == MouseButtons.Left)
{
if (colHit == 0)
{
if (Control.ModifierKeys == Keys.Shift)
{
lastRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
ShiftClickCheckBoxSetter(this.gvLibrary, int.Parse(txtFirstClickRow.Text), lastRowHit);
}
else
{
int firstRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
txtFirstClickRow.Text = firstRowHit.ToString();
}
}
}
Here's the CheckBoxSetter Code:
private void ShiftClickCheckBoxSetter(DataGridView dataGridView, int p, int lastRowHit)
{
if (p < lastRowHit)
{
for (int i = p; i < lastRowHit; i++)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
else//
{
for (int i = p; i >= lastRowHit; i--)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
}
And this is working as expected.
We've also added a ContextMenuStrip to the control for a right-click event.
else if (e.Button == MouseButtons.Right)
{
if (colHit != 0)
{
ContextMenuStrip m = new ContextMenuStrip();
m.Items.Add("Select All", null, m_LibraryItemClicked);
m.Items.Add("Select None", null, m_LibraryItemClickedNone);
m.Show(gvLibrary, e.Location);
}
}
Delegate Event One:
void m_LibraryItemClicked(object sender, EventArgs e) {
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected) {
dgvr.Selected = false;
}
dgvr.Cells["LSelect"].Value = true;
}
}
Delegate Event Two:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
This allows to the user to select all or select none for the checkboxes.
When the Select All selection is chosen, all check boxes are checked:
However when the Select None option is selected:
All check boxes are de-selected, except for the last one checked in the Shift-Click event:
I would think that iterating through all of the Grid Rows and setting the checkbox to not selected would suffice, IE:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
However there seems to be some kind of state property that is disallowing this checkbox in that row to be changed.
Thanks in advance.
I checked your code and could reproduce this behaviour. The problem seem to be with the current cell (not the cell selected). When you try to change this particular cell, the action doesn't get executed immediately.
To change this behaviour add a dataGridView1.CurrentCell = null; before changing the value of the "LSelect" cell. This should fix your issue.
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
dataGridView1.CurrentCell = null;
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}

How to enable and disable particular row in DataGridView by checkbox?

I am trying to enable and disable specific row in DataGridView by checking and unchecking of checkbox inside gridview. (C# Windows application)
I tried using the CellClick event which did not work as expected.
This is the code which i tried
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && dataGridView1.CurrentCell.Selected == true)
{
dataGridView1.Columns[3].ReadOnly = false;
}
}
please tell me how to do this.
Thanks in advance
I think you missed the CellContentClick event, try this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["Your Column Name"].Index) //To check that we are in the right column
{
dataGridView1.EndEdit(); //Stop editing of cell.
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Your Column Name"].Value)
{
//dataGridView1.Columns[3].ReadOnly = true;// for entire column
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
dataGridView1.Rows[colIndex].Cells[rowIndex].ReadOnly = true;
}
}
}
You can simply use dataGridView "CellContentClick" event. The code goes as bellow.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var _dataGrid = (DataGridView)sender;
//Give your checkbox column Index
int chkBoxColumnIndex = 1;
if (e.ColumnIndex == chkBoxColumnIndex && e.RowIndex >= 0)
{
bool isChecked = _dataGrid[chkBoxColumnIndex, e.RowIndex].Value == null ? false : (bool)_dataGrid[chkBoxColumnIndex, e.RowIndex].Value;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
_dataGrid[i, e.RowIndex].ReadOnly = isChecked;
}
}
}
Well, you're setting the readonly property to false meaning it is NOT read only. Try setting it to true. Also the event that fires this should be the checkbox click event (double click on the checkbox to create the event handler).
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
dataGridView1.Columns[3].ReadOnly = true;
}
Also, take a look at the frozen property.
dataGridView1.Columns[3].Frozen = true;

Cannot check or uncheck DataGridViewCheckBoxColumn cells programmatically on UI

i've a DataGridView on which i've added a DataGridViewCheckBoxColumn.
I've also addedd two buttons one for select all the checkBox and one for de-select all the checkBox.
Cell click and select/deselect all buttons works separately, but doesn't works if, for example, i select a cell with mouse click (check box is selected on UI) and then if i press the deselect-All button the value of checkboxs changed but the checkboxs on UI stay checked.
here is my code
Cell Click
private void dictionaryDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == dictionaryDataGrid.Columns["ColSel"].Index) && (e.RowIndex >= 0))
dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value = !(bool)(dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value == null ? false : dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value);
}
button select all:
private void btn_selAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = true;
}
}
button deselect all:
private void btn_unselAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = false;
}
}
Try this :
private void btn_unselAll_Click(object sender, EventArgs e)
{
dictionaryDataGrid.ClearSelection();
foreach (DataGridViewRow row in dictionaryDataGrid.Rows)
{
((DataGridViewCheckBoxCell)row.Cells["ColSel"]).Value = false;
}
}

Dragging and dropping datagridview row on itself

I was recently trying to find some code to drag and drop a row from one datagridview to another datagridview in a WinForms app. I eventually found code that works, but there's one little problem. When I select a row in dataGridView2 to drag to dataGridView1, if I'm not careful and being sloppy, I accidentally drag the row into another row in dataGridView2. Its like it just disappears into another row in dataGridView2. Is there a way to detect that if the row being dragged isn't in dataGridView1, don't allow it to be dropped?
dataGridView2.MouseMove += new MouseEventHandler(dataGridView2_MouseMove);
dataGridView1.DragEnter += new DragEventHandler(dataGridView1_DragEnter);
dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);
void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
if (row != null)
{
DataGridViewRow newrow = row.Clone() as DataGridViewRow;
for (int i = 0; i < newrow.Cells.Count; i++)
{
newrow.Cells[i].Value = row.Cells[i].Value;
}
this.dataGridView1.Rows.Add(newrow);
}
}
void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
e.Effect = DragDropEffects.Copy;
}
}
void dataGridView2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.dataGridView2.DoDragDrop(this.dataGridView2.CurrentRow, DragDropEffects.All);
this.dataGridView2.Rows.Remove(this.dataGridView2.CurrentRow);
}
}
Set AllowDrop Property of datagridview2 to false.

access element of a datagriview after a double click

I have a DataGridView (c#). I would like to have an access to every member of the selected row after a double click.
Any idea how to do that ?
Thanks
Hook up the CellDoubleClick event of the data grid. The DataGridViewCellEventArgs contains the row and column that was clicked. If the row index is -1 the header was clicked.
private void MyDataGridView_CellDoubleClick(object sender,
DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
// header was clicked
}
else
{
// data row was clicked, can access the row contents like this
var row = MyDataGridView.Rows[e.RowIndex];
var cell = row[0];
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
foreach(var item in dataGridView1.SelectedRows)
{
}
}

Categories

Resources