how to give fore color and back ground color using switch case in data grid view
output as follows;
Date Session Ck Mk RK VB GS CMK UNNI MS
7/1/12 1 REO TAs PH2
7/1/12 2 REO TAs PH2
7/1/12 3 REO TAs PH2
7/1/12 4 REO TAs PH2
8/1/12 1 REO TAs PH2
8/1/12 2 REO TAs PH2
8/1/12 3 REO TAs PH2
8/1/12 4 REO TAs PH2
using switch case how to give fore and Back ground color to 7/1/12 and 8/1/12 date.
please send the code using c sharp;
Loading
Hemant SrivastavaPosted Jan 15, 2013, 11:34 AM
Thanks,
Hemant
Hemant SrivastavaPosted Jan 14, 2013, 6:08 PM
Hemant SrivastavaPosted Jan 14, 2013, 6:06 PM
public class Session
{
public DateTime DATE { get; set; }
public int SESSION_ID { get; set; }
public string CK { get; set; }
public string MK { get; set; }
public string RK { get; set; }
public string VB { get; set; }
public string GS { get; set; }
public string CMK { get; set; }
public string UNNI { get; set; }
public string MS { get; set; }
public Session(DateTime date, int sessionId, string ck, string mk, string rk,
string vb, string gs, string cmk, string unni, string ms)
{
DATE = date;
SESSION_ID = sessionId;
CK = ck;
MK = mk;
RK = rk;
VB = vb;
GS = gs;
CMK = cmk;
UNNI = unni;
MS = ms;
}
}
and you make a funtion that binds your data into a datagridview. Inside this method, you can set the forecolor and backcolor property while iterating the datagridview element.
private void button1_Click(object sender, EventArgs e) lstProduct = new List
{
List
{
new Session(DateTime.Now.AddDays(5), 1, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(2), 2, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(5), 3, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(5), 4, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(5), 5, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now, 6, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(3), 7, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now.AddDays(2), 8, "REO", "TAs", "PH2", "", "", "", "", ""),
new Session(DateTime.Now, 9, "REO", "TAs", "PH2", "", "", "", "", "")
};
DateTime minDate = DateTime.Now.AddDays(2);
DateTime maxDate = DateTime.Now.AddDays(5);
dataGridView1.DataSource = lstProduct;
foreach (DataGridViewRow dRow in dataGridView1.Rows)
{
if (minDate <= Convert.ToDateTime(dRow.Cells[0].Value) && Convert.ToDateTime(dRow.Cells[0].Value) < maxDate)
{
dRow.DefaultCellStyle.ForeColor = Color.Red;
dRow.DefaultCellStyle.BackColor = Color.Yellow;
}
}
}
It will work, let me know if you have any issue.
Satyapriya NayakPosted Jan 14, 2013, 1:07 AM
What have you tried so far?