Этот код будет переключать флажок при нажатии любого аниматора переключателя, используемого в веб-шаблонах. Замените ".onoffswitch-label" как доступный в вашем коде. checkboxID - это чекбокс, включенный здесь.
$('.onoffswitch-label').click(function () {
if ($('#checkboxID').prop('checked'))
{
$('#checkboxID').prop('checked', false);
}
else
{
$('#checkboxID').prop('checked', true);
}
});
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;
}
}
}
Это решение для 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;
}
}
}
}
подключите событие OnRowDataBound , затем выполните действия
ASPX (Grid):
<asp:.... OnRowDataBound="RowDataBound"..../>
Отложенный код:
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;
}
}
ДЛЯ 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;
}
}
}
}
Этот код мне подходит:
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;
}
}
Кроме преобразования в строку вместо вызова ToString, я не вижу никакой разницы, так что это может быть ошибка чувствительности к регистру. Попробуйте использовать:
dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO"
Возможно ли, что в значении ячейки есть пробелы или какой-либо другой символ? В таком случае попробуйте использовать метод Contains, а не прямое равенство.
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))