I tried to get value of checked checkbox in DataGridView, so I check if value is true or false:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if ((bool)dataGridView1.Rows[i].Cells["check"].Value == true)
{
dataGridView1.Rows[i].Cells["check"].Value = false;
}
else
{
dataGridView1.Rows[i].Cells["check"].Value = true;
}
button2.Enabled = (counter > 0);
}
}
}
}
It involks an error in line:
if ((bool)dataGridView1.Rows[i].Cells["check"].Value == true)
Second solution:
if (dataGridView1.Rows[i].Cells["check"].Value == null || (bool)dataGridView1.Rows[i].Cells["check"].Value == false)
{
dataGridView1.Rows[i].Cells["check"].Value = true;
counter++;
}
else
{
dataGridView1.Rows[i].Cells["check"].Value = false;
counter--;
}
The code below works, but sometimes checkbox is not checked
I am doing something really similar in a project of mine,
I am only using OnCellValueChanged instead of CellContentClick.
Here's my working line of code
bool completed = Convert.ToBoolean(dgv.Rows[e.RowIndex].Cells[1].Value.ToString());
What is exactly your error? Did you try to see what .Value was in the debugger ?
Related
I know it's already been written about.
Everything looks very good. But when I move to the right to see the rest of the columns, the rows in DataDridView start blinking very much. I can't solve this.
private void registersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow rowDataGridView = null;
string dataPropertyName;
dataPropertyName = this.registersDataGridView.Columns[e.ColumnIndex].DataPropertyName;
int isApprovedColumnIndex = this.registersDataGridView.Columns[isApprovedColumnName].Index;
int isCancelledColumnIndex = this.registersDataGridView.Columns[isCancelledColumnName].Index;
bool theColorHasBeenSet = false;
if (e.RowIndex >= 0 && e.RowIndex != btregisterDgvRowIndex)
{
rowDataGridView = this.registersDataGridView.Rows[e.RowIndex];
if (this.registersDataGridView.Columns[isCancelledColumnIndex].DataPropertyName == "IsCancelled")
{
if (rowDataGridView.Cells[isCancelledColumnIndex].Value != null && rowDataGridView.Cells[isCancelledColumnIndex].Value.ToString() == "Tak")
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.PaleVioletRed)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.PaleVioletRed;
}
theColorHasBeenSet = true;
}
else
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
}
}
btregisterDgvRowIndex = e.RowIndex;
}
if (this.registersDataGridView.Columns[isApprovedColumnIndex].DataPropertyName == "IsApproved")
{
if (!theColorHasBeenSet)
{
if (rowDataGridView.Cells[isApprovedColumnName].Value != null && rowDataGridView.Cells[isApprovedColumnName].Value.ToString() == "-")
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.LightGray)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.LightGray;
}
theColorHasBeenSet = true;
}
else if (rowDataGridView.Cells[isApprovedColumnName].Value != null && rowDataGridView.Cells[isApprovedColumnName].Value.ToString() == "Nie")
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.PaleVioletRed)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.PaleVioletRed;
}
theColorHasBeenSet = true;
}
else
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
}
}
btregisterDgvRowIndex = e.RowIndex;
}
}
}
}
else
{
if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
{
rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
}
}
You're not setting the theColorHasBeenSet here which might cause it to change between Ivory and the next color on your list.
Your code seems to verbose to me, try the following
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex < 0)
return;
var rowDataGridView = this.registersDataGridView.Rows[e.RowIndex];
Color GetBackgroundColor()
{
var isApprovedColumnIndex = this.registersDataGridView.Columns[isApprovedColumnName].Index;
var isCancelledColumnIndex = this.registersDataGridView.Columns[isCancelledColumnName].Index;
if (this.registersDataGridView.Columns[isCancelledColumnIndex].DataPropertyName == "IsCancelled")
{
var strValue = Convert.ToString(rowDataGridView.Cells[isCancelledColumnIndex].Value);
if (strValue == "Tak")
return Color.PaleVioletRed;
}
if (this.registersDataGridView.Columns[isApprovedColumnIndex].DataPropertyName == "IsApproved")
{
var strValue = Convert.ToString(rowDataGridView.Cells[isApprovedColumnIndex].Value);
if (strValue == "-")
return Color.LightGray;
if (strValue == "Nie")
return Color.PaleVioletRed;
}
return Color.Ivory;
}
rowDataGridView.DefaultCellStyle.BackColor = GetBackgroundColor();
}
I am creating a scheduler where data I entered below on two textboxes have to appear according to the Day or week and Time values. I've tried with the following code, but didn't work.
private void btnEnterData_click(object sender, System.EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
else if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb7.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
return;
//and codes all the way down
}
private void btnSavePatient_Click(object sender, EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Show();
}
if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb2.Show();
}
// codes all the way down
}
P.S. I'm assuming that I have to use If/Else statement, but I can't get the logic fully :(
Image of the scheduler:
I'm creating a datagridview in WinForms. Each cell in the datagridview is either a textboxcell or datagridview image cell. I'm firing a cellMouseDownEent( object sender, DataGridViewCellMouseEventArgs e). If the sected cell is a image cell I perform task1 and if it is textboxcell I perform task2. I'm not getting how to find out whether the current cell is image cell or text box cell. I tried setting tag property of image cell to 0 and textboxcell cell to 1 to identify which is being clicked, but no luck. Any advice is aapreciated.
Thanks,
I'm adding my code here:
Ignore if a column or row header is clicked
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
// Here you can do whatever you want with the cell
this.dgvAddFilters.CurrentCell = clickedCell; // Select the clicked cell, for instance
// Get mouse position relative to the vehicles grid
var relativeMousePosition = dgvAddFilters.PointToClient(Cursor.Position);
if (clickedCell.Tag.ToString()==null)
{
return;
}
else if (imageCell == null) return;
else if (e.ColumnIndex == 0 && e.RowIndex == 0)
{
if ((dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null))
// (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null))
{
dgvAddFilters.ContextMenuStrip = contMenuOr;
this.contMenuOr.Show(dgvAddFilters, relativeMousePosition);
}
else return;
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuFilterMenu;
this.contMenuFilterMenu.Show(dgvAddFilters, relativeMousePosition);
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuOrEditDelete;
this.contMenuOrEditDelete.Show(dgvAddFilters, relativeMousePosition);
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
}
else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
&& (dgvAddFilters[(e.ColumnIndex + 2), e.RowIndex].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contmenuDeletEdit;
this.contmenuDeletEdit.Show(dgvAddFilters, relativeMousePosition);
}
else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
}
else
{
return;
}
To know the type of cell that is clicked, You can try below way of doing.... See if it is helpful.
Get the clicked cell and check for its type.
Below is an example to check for checkbox type cell.
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
Type type = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType();
if (type.Name == "DataGridViewCheckBoxCell")
{
string value = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
}
Does this help you?
using System.ComponentModel;
using System.Windows.Forms;
namespace DGVCellTypes_47599159
{
public partial class Form1 : Form
{
DataGridView dgv = new DataGridView();
BindingList<dgventry> dgventries = new BindingList<dgventry>();
public Form1()
{
InitializeComponent();
InitOurStuff();
}
private void InitOurStuff()
{
this.Controls.Add(dgv);
dgv.Dock = DockStyle.Top;
dgv.DataSource = dgventries;
dgv.CellMouseDown += Dgv_CellMouseDown;
for (int i = 0; i < 10; i++)
{
dgventries.Add(new dgventry { col1 = $"col1_{i}", col2 = $"col2_{i}", col3 = (i % 2) > 0 });
}
}
private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
{
//do something
}
else
{
//do something
}
}
}
public class dgventry
{
public string col1 { get; set; }
public string col2 { get; set; }
public bool col3 { get; set; }
}
}
I'm not getting how to find out whether the current cell is image cell or text box cell
private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
{
//do something
}
else
{
//do something
}
}
I have a weird behavior in my combobox. I have two combobox, one is cboSede an the other is cboGroup. CboSede enable cboGroup. I have already done this in other forms but here I get this message: ArgumentOutOfRangeException was unhandled by user code. The idea is if the user does not choose any value in cboSede then cboGroup is not enabled and in the other hand, if the user choose a valid option in cboSede, cboGroup is enable.
This is my code:
The SelectedIndexChanged of cboSede
private void cboSede_SelectedIndexChanged(object sender, EventArgs e)
{
if (Util.Security.ConexionBD)
{
if (Convert.ToInt32(cboSede.SelectedIndex) == 0 || Convert.ToInt32(cboSede.SelectedIndex) == -1)
{
cboGroup.Enabled = false;
cboGroup.SelectedIndex = 0;
}
else
{
this.FillGroupCombo();
cboGroup.Enabled = true;
}
}
else
MessageBox.Show("Error", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
the FillGroupCombo function
private void FillGroupCombo()
{
try
{
Entity.Group objGroup = new Entidad.Group ();
objGroup .IdSede = Convert.ToInt32(cboSede.SelectedValue);
objGroup = Control.Group.ListBySede(objGroup );
if (objGroup != null && objGroup.ListGroup.Count > 0)
{
Entity.Group objMsje = new Entity.Group();
objMsje.IdGroup = -1;
objMsje.Name= "--- Select group ---";
objGroup.ListGroup.Insert(0, objMsje);
}
else
{
Entity.Group objMsje = new Entity.Group();
objMsje.IdGroup = 0;
objMsje.Name= "-- No groups found --";
objGroup.ListGroup.Insert(0, objMsje);
}
Util.Utilitario.FillCombo(objGroup.ListGroup, this.cboGroup, "IdGrupo", "Name");
}
catch (Exception ex)
{
Util.Security.Insert(ex);
Util.Security.SaveLog(ex.Message);
}
}
Any idea about why this happens?
This one
if (Convert.ToInt32(cboSede.SelectedIndex) == 0 || Convert.ToInt32(cboSede.SelectedIndex) == -1)
{
cboGroup.Enabled = false;
cboGroup.SelectedIndex = 0;
}
Will kill the code when SelectedIndex == -1 and you actually have no item in your comboBox (when index = 0, it is OutOfRange)
you can give an if condition if you want
if (cboGroup.Items.Count > 0)
cboGroup.SelectedIndex = 0;
This way, it first check of the comboBox really have anything. And if it doesn't then it won't produce OutOfRange error
when i try to update the content of the datagridview combo box it throws
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function
at line ,node.Cells[(int)Parameters.eColumn.valueBySelectionColumn] = cboCell;
How can i solve this problem??
THX
private void treeGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
if (e.Control.GetType() == new DataGridViewComboBoxEditingControl().GetType())
{
if ((cboEditor != null) && (cboEditor_EventHandler != null))
{
cboEditor.SelectedIndexChanged -= cboEditor_EventHandler;
}
cboEditor = (DataGridViewComboBoxEditingControl)e.Control;
cboEditor.SelectedIndexChanged += cboEditor_EventHandler;
if (this.treeGridView1.SelectedCells.Count > 0 &&
this.treeGridView1.SelectedCells[0].ColumnIndex == (int)Parameters.eColumn.valueBySelectionColumn)
{
TreeGridNode node = GetCurrentNode();
object cellValue = node.Parent.Cells[(int)Parameters.eColumn.sectionTypeColumn].Value;
Parameters.eSection section = (Parameters.eSection)dicSection[cellValue.ToString()];
this.treeGridView1.Focus();
switch (section)
{
case Parameters.eSection.UNIX_Script:
DataGridViewComboBoxCell cboCell = Parameters.ValidateChoice(Parameters.eSection.UNIX_Script,
node.Cells[(int)Parameters.eColumn.parameterTypeColumn].Value,
ref cboEditor);
if (cboCell != null)
{
***node.Cells[(int)Parameters.eColumn.valueBySelectionColumn] = cboCell;***
node.Cells[(int)Parameters.eColumn.valueBySelectionColumn].Style.BackColor =
node.Cells[(int)Parameters.eColumn.sequenceColumn].Style.BackColor;
}
break;
}
}
}
}
}