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 want to create filrer for 4 fields. But I get too many code.
I need filter when just one field choosen or a few (2,3 or 4) fields at the same time. How to create logic for it?
My ugly code:
void ViewSource_Filter(object sender, FilterEventArgs e)
{
if (e.Item is Event evnt)
{
bool selectedModel = filterEventsControl.ComboBoxModels.SelectedIndex != 0 && filterEventsControl.ComboBoxModels.SelectedItem != null;
bool selectedIp = filterEventsControl.ComboBoxIPs.SelectedIndex != 0 && filterEventsControl.ComboBoxIPs.SelectedItem != null;
bool selectedParameter = filterEventsControl.ComboBoxParameters.SelectedIndex != 0 && filterEventsControl.ComboBoxParameters.SelectedItem != null;
bool selectedStatus = filterEventsControl.ComboBoxStatus.SelectedIndex != 0 && filterEventsControl.ComboBoxStatus.SelectedItem != null;
if (selectedModel && !selectedIp && !selectedParameter && !selectedStatus)
{
var model = filterEventsControl.ComboBoxModels.SelectedItem.ToString();
if (evnt.DeviceName == model)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
else if (selectedModel && selectedIp && !selectedParameter && !selectedStatus)
{
var model = filterEventsControl.ComboBoxModels.SelectedItem.ToString();
var ip = filterEventsControl.ComboBoxIPs.SelectedItem.ToString();
if (evnt.DeviceName == model && evnt.Ip == ip)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
...
else
{
e.Accepted = true;
}
}
}
It can be something like this:
bool FilterByName(Event evnt)
{
var model = filterEventsControl.ComboBoxModels.SelectedItem?.ToString();
return evnt.DeviceName == model;
}
bool FilterByIp(Event evnt)
{
var ip = filterEventsControl.ComboBoxIPs.SelectedItem?.ToString();
return evnt.Ip == ip;
}
void ViewSource_Filter(object sender, FilterEventArgs e)
{
...
bool res = true;
if (selectedModel)
res = res && FilterByName();
if (selectedIp)
res = res && FilterByIp();
...
}
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
}
}
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;
}
}
}
}
}
I have banners on a site. When banner showed then I increment field in DB.
This is code:
public Banner GetBanner(CategoryBanner type)
{
var banners = Database.Banners.Where(b => b.IsPublish.Value &&
b.Category.Value == (int)type &&
b.PeriodShowCountAlready < b.PeriodShowCount || b.IsPublish.Value && b.Category.Value == (int)type &&
b.ShowNext < DateTime.Now);
var count = banners.Count();
if (count != 0)
{
var skip = new Random().Next(banners.Count() - 1);
Banner banner = banners.Skip(skip).FirstOrDefault();
if (banner != null)
{
UpdatePeriodShowCountAlready(banner); // problem is inside this method
if (banner.ShowStart == null)
UpdateShowStartAndEnd(banner);
return banner;
}
}
return null;
}
private void UpdatePeriodShowCountAlready(Banner banner)
{
try
{
if (banner != null)
{
banner.PeriodShowCountAlready++;
if (banner.PeriodShowCountAlready >= banner.PeriodShowCount && banner.ShowNext < DateTime.Now)
{
banner.PeriodShowCountAlready = 0;
banner.ShowStart = null;
banner.ShowNext = null;
}
Database.SubmitChanges();
}
}
catch (Exception ex)
{
ErrorLog.GetDefault(null).Log(new Error(ex));
}
}
And, I get the following error:
System.Data.Linq.ChangeConflictException
Row not found or changed.
This error is simple to reproduce:hold down the F5 is enough for a few seconds.
I understand why this error is occur, but how to rewrite my code properly?
Thanks.