Filter Criteria % like % following Code - c#

i use this code for focus row which match text in search text box.now how can i use % like % criteria for this code
foreach (DataGridViewRow row in GdvDetails.Rows)
{
var cellValue = row.Cells["Bunch Component"].Value;
if (cellValue != null && cellValue.ToString() == txtSearch.Text)
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Empty;
}
}

try this
foreach (DataGridViewRow row in GdvDetails.Rows)
{
var cellValue = row.Cells["Bunch Component"].Value;
if (cellValue != null)
{
if ( cellValue.ToString().Contains(txtSearch.Text))
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Empty;
}
}
}

Related

Change row color based on text in column

I'm trying to change the color of the row in a datagridviewer based on the text that is in one of the columns. I'm getting the error: Object reference not set to an instance of an object on the line of the first if statement. I filled in the datagridviewer based on a datasource which the code is also below.
void ChangeDataGridViewColor()
{
foreach (DataGridViewRow Row in datagridviewTreatmentPrep.Rows)
{
if (Row.Cells["Primary Onc"].Value.ToString() == "JMK")
{
Row.DefaultCellStyle.BackColor = Color.Green;
}
if (Row.Cells["Primary Onc"].Value.ToString() == "DBF")
{
Row.DefaultCellStyle.BackColor = Color.Orange;
}
else
{
Row.DefaultCellStyle.BackColor = Color.White;
}
}
}
void FillDataGridViewTreatmentPrep()
{
string constring = "datasource = RadOncViewerDatabase.db";
string TreatPrepQuery = "SELECT * FROM TreatmentPrep";
SQLiteConnection connectionstring = new SQLiteConnection(constring);
connectionstring.Open();
DataTable dsTreatPrep = new DataTable();
SQLiteDataAdapter adapterTreatPrep = new SQLiteDataAdapter(TreatPrepQuery, constring);
adapterTreatPrep.Fill(dsTreatPrep);
datagridviewTreatmentPrep.DataSource = dsTreatPrep;
//datagridviewTreatmentPrep.BindingContext = new BindingContext();
//this.datagridviewTreatmentPrep.DataSource = dsTreatPrep.Tables[0].DefaultView.ToTable(true, "Patient_Name");
}
Use the the RowPrePaint event, and don't use .ToString() on objects that can be null:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex < 0) return;
var row = (sender as DataGridView).Rows[e.RowIndex];
string value = Convert.ToString(row.Cells["Primary Onc"].Value);
//or in VS 2015: string value = row.Cells["Primary Onc"].Value?.ToString();
if (value == "JMK")
row.DefaultCellStyle.BackColor = Color.Green;
else if (value == "DBF")
row.DefaultCellStyle.BackColor = Color.Orange;
else
row.DefaultCellStyle.BackColor = Color.White;
}
Below is the code I have tested and it works as expected. Sorry for the delay.
foreach (DataGridViewRow Row in dataGridView1.Rows) {
DataRowView drv = (DataRowView)Row.DataBoundItem;
if (drv != null) {
if (drv.Row["Primary Onc"].ToString() == "JMK") {
Row.DefaultCellStyle.BackColor = Color.Green;
}
else {
if (drv.Row["Primary Onc"].ToString() == "DBF") {
Row.DefaultCellStyle.BackColor = Color.Orange;
}
else {
Row.DefaultCellStyle.BackColor = Color.White;
}
}
}
}
Hope this helps.
you must use DataGridViewCellStyle class
my solution:
int max = dgv.Rows.Count;
DataGridViewCellStyle style;
for (int i = 0; i < max; i++)
for (int j = 2; j < dgv.Columns.Count; j++)
if (dgv[j, i].Value.ToString() == "")
{
style = dgv[j, i].Style;
style.BackColor = Color.Red;
dgv[j, i].Style = style;
}

How can I color rows in datagridview with condition C#

I want with a condition :
all rows have bool_badge =0 : color with RED
all rows have bool_badge=1 : color with ForestGreen
I have a code Correct BUT just when i click for a cell specific
My code:
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
int row = this.dataGridView1.CurrentCell.RowIndex;
string valeur = dataGridView1[2, row].Value.ToString();
if (valeur == "0")
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
}
else
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
}
}
The Result :
1) `
2)
But I want when i execute my application , the test begin if bool_badge 0 or 1, and i have for all the gridview : color RED or ForestGreen ,
I try this code:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string valeur = dataGridView1[2, i].Value.ToString();
if (valeur == "0")
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
}
else
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
}
}
But i have ERROR!
this is :
How can i fix it?
Very thanks,
You can use Datagridview's Cell_Formatting event.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "bool_badge" && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
// if the column is bool_badge and check null value for the extra row at dgv
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1")
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.ForestGreen;
}
}
}
Result will be,
Hope helps,
You should use .Rows and .Cells properties.
string valeur = dataGridView1.Rows[i].Cells[2].Value.ToString();
For helping you debugging don't do Value.ToString(); just
var value = dataGridView_XXX.Rows[rowNumber].Cells[i].value;
And
if (value == null) display your row/column index
else dataGridView_XXX.Rows[rowNumber].DefaultCellStyle.BackColor = xxx;

null value in cellformatting

I have a datagrid called dgMember_Grade that takes its data from a stored procedure.
One of its columns represent a date called vacationStart.
I want to color the rows based on cell value, but it gives me an error on the foreach line:
Cannot convert type 'char' to 'System.Windows.Forms.DataGridViewCell'
I tried the code:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
foreach (DataGridViewCell cell in Myrow.Cells[26].Value.ToString()) // error on foreach
{
if (cell != null)
{
DateTime date = DateTime.Now;
DateTime expiredate = DateTime.Parse(Myrow.Cells[26].Value.ToString());
if (expiredate < date)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}
}
Here is the code you can use:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
var cellValue = Myrow.Cells[26].Value;
if (cellValue == null || cellValue == DBNull.Value)
continue;
DateTime expiredate = Convert.ToDateTime(cellValue);
if (expiredate < DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
Note:
You don't need 2 loops, You can simply use a loop betwen Rows and apply what you need based on Myrow.Cells[26].Value
You can use events like CellFormatting or CellPainting or RowPrePaint to apply formatting.
If you already know an index of the cell.
May be you are trying to do this:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
if (Myrow.Cells[26].Value == null) {
continue;
}
DateTime expiredate = DateTime.Parse(Myrow.Cells[26].Value.ToString());
if (expiredate < DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}

Display Null if datagridview Column is empty

I have a gridview with few column cells. all i want is to check an empty cells and write null by default. if some of the columns are not filled., i want a default null text to the specific cells that are ommited
I tried to create something like this but it is not working as i don't know how i can take it further
private void Gridview_Output_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
{
string _Car = string.Empty;
string _Dealer = string.Empty;
string _Model = string.Empty;
int gridcount = Gridview.Rows.Count;
for (int i = 0; i < gridcount; i++)
{
_Car = "";
_Dealer = "";
_Model = "";
if (Gridview.Rows[i].Cells[1].Value == DBNull.Value)
{
_Car = "Null";
}
else
{
_Car = Gridview.Rows[i].Cells[1].Value.ToString();
}
if (Gridview.Rows[i].Cells[2].Value == DBNull.Value)
{
_Dealer = "Null";
}
else
{
_Dealer = Gridview.Rows[i].Cells[2].Value.ToString();
}
if (Gridview.Rows[i].Cells[4].Value == DBNull.Value)
{
_Model = "Null";
}
else
{
_Model = Gridview.Rows[i].Cells[4].Value.ToString();
}
}
}
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (!dr.IsNewRow)
{
for (int c = 0; c <= dr.Cells.Count - 1; c++)
{
if (dr.Cells[c].Value == null)
{
dr.Cells[c].Value = "NULL TEXT";
}
}
}
}
You are looking for the DataGridView's CellFormatting event.
Try something like this :
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (string.IsNullOrEmpty(e.Value as string))
{
e.Value = "NULL";
e.FormattingApplied = true;
}
}
If necessary, you can also add a column condition with the e.ColumnIndex property.

Datagridview colour changing

I'm trying to change the colour based on the value in the cell, complete or incomplete but for some reason it's saying that 'Color' doesn't exist in the current context.
Is there a system item I should be using or something?
If anyone has any alternatives to what I'm trying to do that would also be appreciated.
foreach (DataGridViewRow row in dtaFinished.Rows)
{
string RowType = row.Cells[4].Value.ToString();
if (RowType == "Completed")
{
row.DefaultCellStyle.BackColor = Color.Green; //Error on these lines
row.DefaultCellStyle.ForeColor = Color.White; //Error on these lines
}
else if (RowType == "Incomplete")
{
row.DefaultCellStyle.BackColor = Color.Yellow;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
use the below namespace:
using System.Drawing;
hope thiS will help.
Hi You can found your answere Here
I used this in a project a while back :-
private void dgvOutstandingReports_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dgvOutstandingReports.Rows[rowIndex];
if (theRow.Cells[colIndex].Value.ToString() == "Daily Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightYellow;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Monthly Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightGray;
}
else if (theRow.Cells[colIndex].Value.ToString() == "SMP Report")
{
theRow.DefaultCellStyle.BackColor = Color.Snow;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Weekly Report")
{
theRow.DefaultCellStyle.BackColor = Color.Pink;
}
else if (theRow.Cells[colIndex].Value.ToString() == "Hourly Report")
{
theRow.DefaultCellStyle.BackColor = Color.LightSteelBlue;
}
}
}

Categories