I want to hide all rows in datagrid which do not match the text in driverNo.Text, but when driverNo.Text is null, I'd like all the rows in datagrid to appear. How would I accomplish this?
private void driverNo_KeyUp(object sender, KeyEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[1].Value.ToString() == driverNo.Text)
{
}
else if (row.Cells[1].Value.ToString() == null)
{
}
}
}
This should solve the problem:
CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
manager.SuspendBinding();
bool shouldNotFilter = string.IsNullOrEmpty(driverNo.Text);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (shouldNotFilter)
{
row.Visible = true;
}
else
{
if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase))
{
row.Visible = false;
}
else
{
row.Visible = true;
}
}
}
manager.ResumeBinding();
This approach is straight forward but slow, I'd suggest you take a look at DataView and its RowFilter property. Here is a good example.
This may not be exactly what you need, but something along these lines?
private void driverNo_KeyUp(object sender, KeyEventArgs e)
{
// Set all rows.Visible = false in design
if (driverNo.Text = "")
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Visible = true;
}
}
else
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[1].Value.ToString() == driverNo.Text)
{
row.Visible = true;
}
}
}
}
This will make all rows visible if the driverNo.Text is null and only show the rows with what is contained in driverNo.Text otherwise.
NOTE: The rows need to not be visible to start, or you can make them visible to start and just change the if statement to != driverNo.Text and set to false
Related
I am trying to update the value of a checkbox column in datagridview in c# by code but it is not working. Here it is the code:
public frmShowData()
{
InitializeComponent();
dgvAlumnos.AutoGenerateColumns = false;
dgvAlumnos.ReadOnly = false;
updateAttendance();
}
public void updateAttendace(){
foreach (DataGridViewRow r in dgvAlumnos.Rows)
{
if (attendance[r.Index] == true)
{
r.Cells[2].Value = true;
}
else
{
r.Cells[2].Value = false;
}
}
}
Attendance is the array of booleans where I have the values.
The column number 2 of the datagridview is the checkbox column.
However, the changes are not visible in the datagriview.
I am using this code inside the form construct.
Thanks in advance.
Try using value = true instead.
foreach (DataGridViewRow r in dgvAlumnos.Rows)
{
if (attendance[r.Index] == true)
{
r.Cells[2].Value = true;
}
else
{
r.Cells[2].Value = false;
}
}
Try these two after changing data
dgvAlumnos.RefreshEdit();
dgvAlumnos.Refresh();
private void HideRows(object sender, DataGridViewCellEventArgs e)
{
IndexRows = RowIndex;
if (IndexRows>2)
{
DataGridView.Rows[IndexRows].Visible=false;
}
else
{
DataGridView.Rows[IndexRows].Visible=true;
}
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// Avoid for Row which are currently in Edit mode
if (!row.IsNewRow)
{
row.Visible = row.Index < 2;
}
}
I want to fire an event when any of the datagridviewcheckbox is checked. I tried the foreach but it only trigger when all the datagridviewcheck is checked.
I want to fire an event if any of the datagridviewcheckboxcell is checked.
foreach (DataGridViewRow row in dgvLocal.Rows)
{
if ((Convert.ToBoolean(row.Cells[0].Value) == true))
{
//
}
}
Use the cellcontentclicked event of the datagridview
Also use the CurrentCellDirtystateChanged to make sure the last click is commited
void grd_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (grd.IsCurrentCellDirty)
grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1) //compare to checkBox column index
{
DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)grd[e.ColumnIndex, e.RowIndex];
if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
{
//checkBox is checked - do the code in here!
}
else
{
//if checkBox is NOT checked (unchecked)
}
}
}
Try this out
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[1];
if (check.Value == check.TrueValue)
{
//dosomething
}
else
{
//dosomething
}
}
Trigger CellValueChanged
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
}
}
}
I tried to change the backcolor in a specific column when the cell value changed.
I didn't find the method to do that and I don't know how to do that.
ok ----> backcolor in green.
nok ----> backcolor in red.
thank you very much for your help.
private void timer2_Tick(object sender, EventArgs e)
{
int count = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
String StartCourse = dr[0].ToString();
string EndCourse = dr[1].ToString();
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(StartCourse);
DateTime EndTime = Convert.ToDateTime(EndCourse);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = "ok";
}
else
{
ds.Tables[0].Rows[count][5] = "nok";
}
count++;
dataGridView1.DataSource = ds.Tables[0];
}
}
you can call this Procedure:
UPDATED
void ColorGrid()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[5].Value.ToString() == "ok")
{
row.DefaultCellStyle.BackColor = Color.Green;
}
else
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
}
See the DataGridViewColumn.DefaultCellStyle property. This allows you to set a DataGridViewCellStyle for a column. This class has a BackColor property.
See the following MSDN article for more detail:
Cell Styles in the Windows Forms DataGridView Control
Try this:
foreach (DataGridViewRow row in this.DataGridView1.Rows)
{
if (row.Cells[5].Text == "ok")
{
row.DefaultCellStyle.BackColor = Color.Green;
}
else
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
Regards
I'm doing this on my own project.
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.DefaultCellStyle.BackColor = (row.Cells[5].Text == "ok")? Color.Green:Color.Red;
}
If that still didn't work, try delete that datagridview and add it again.
Be sure to name it again as dataGridView1 and attached its corresponding events.
I have a datagridview made up of multiple rows and columns.
I want to iterate through each row and check the contents of a specific column.
If that column contains the word "NO", I want to change the forecolor of the entire row to Red.
Here is an attempt at some code so far but It's certainly not working, starting to wonder If I need to iterate over every cell?
CODE:
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
}
hook up OnRowDataBound event then do stuff
ASPX (Grid):
<asp:.... OnRowDataBound="RowDataBound"..../>
Code Behind:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
{
return;
}
if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
e.Row.BackColor=Color.Red;
}
}
FOR WinForms:
hook the **DataBindingComplete** event and do stuff in it:
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
if (e.ListChangedType != ListChangedType.ItemDeleted)
{
DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
red.BackColor=Color.Red;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (r.Cells["FollowedUp"].Value.ToString()
.ToUpper().Contains("NO"))
{
r.DefaultCellStyle = red;
}
}
}
}
On your DataGridView, handle the CellFormatting event:
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
Your event handler could then look like this:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(dataGridView1.Columns[e.ColumnIndex].Name == "FollowedUp" && e.Value != null && e.Value.ToString() == "No")
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
}
In this way you aren't 'iterating' over the rows -- simply changing the color with which they are painted/drawn when they become visible (and thus require formatting) in the grid.
public void ColourChange()
{
DataGridViewCellStyle RedCellStyle = null;
RedCellStyle = new DataGridViewCellStyle();
RedCellStyle.ForeColor = Color.Red;
DataGridViewCellStyle GreenCellStyle = null;
GreenCellStyle = new DataGridViewCellStyle();
GreenCellStyle.ForeColor = Color.Green;
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
{
dgvr.DefaultCellStyle = RedCellStyle;
}
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes"))
{
dgvr.DefaultCellStyle = GreenCellStyle;
}
}
}
Is it possible there are spaces or some other character as part of the cell value? If so try using the Contains method rather than straight equality.
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
This is the solution for Winforms:
private void HighlightRows()
{
DataGridViewCellStyle GreenStyle = null;
if (this.dgridv.DataSource != null)
{
RedCellStyle = new DataGridViewCellStyle();
RedCellStyle.BackColor = Color.Red;
for (Int32 i = 0; i < this.dgridv.Rows.Count; i++)
{
if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO")
{
this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle;
continue;
}
}
}
}
This code works fine for me:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME)
{
row.DefaultCellStyle.BackColor = Color.LightSalmon;
row.DefaultCellStyle.SelectionBackColor = Color.Salmon;
}
}
Other than casting as a string rather than calling ToString I dont really see any difference so it could be a case sensitivity bug. Try using:
dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO"
private void Grd_Cust_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
colorCode == 4 ? Color.Yellow : Color.Brown;
if (e.RowIndex < 0 || Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value == DBNull.Value)
return;
string colorCode = Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value.ToString();
e.CellStyle.BackColor = colorCode == "NO" ? Color.Red : Grd_Cust.DefaultCellStyle.BackColor;
}