Reputation: 959
I am new to c#
and Database
linking that's why not able to get this by searching old posts of stackoverflow
private void issueDetails()
{
string connectionPath = @"Data Source=Data\libraryData.dat;Version=3;New=False;Compress=True";
using (SQLiteConnection connection = new SQLiteConnection(connectionPath))
{
SQLiteCommand command = connection.CreateCommand();
connection.Open();
string query = "SELECT bookno as 'Book No.',studentId as 'Student ID', title as 'Title', author as 'Author', description as 'Description', issuedDate as 'Issued Date', dueDate as 'Due Date' FROM issuedBooks";
command.CommandText = query;
command.ExecuteNonQuery();
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "issuedBooks");
dataGridView1.DataSource = ds.Tables["issuedBooks"];
dataGridView1.Sort(dataGridView1.Columns["Student ID"], ListSortDirection.Ascending);
dataGridView1.ReadOnly = true;
connection.Close();
}
}
i used like above for getting some library book details like issuedDate and dueDate, now i want highlight a particular cell that is over dueDate .
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
Color c = Color.Black;
if (e.ColumnIndex == 6)
{
if (isLate(Convert.ToString(e.Value)))
{
c = Color.Red;
count++;
Console.WriteLine(count);
}
}
e.CellStyle.ForeColor = c;
}
public string toInd(string date)
{
DateTimeFormatInfo fmt = new CultureInfo("fr-fr").DateTimeFormat;
string dateString;
DateTimeOffset offsetDate, dateig;
string ret = DateTime.Now.ToShortDateString();
dateString = date;
bool ws = DateTimeOffset.TryParse(dateString, fmt, DateTimeStyles.None, out dateig);
if (ws)
{
offsetDate = DateTimeOffset.Parse(dateString, fmt);
ret = offsetDate.Date.ToShortDateString();
return ret;
}
return ret;
}
private bool isLate(string nowS)
{
DateTime dueDate = Convert.ToDateTime(toInd(nowS));
DateTime now;
string present = DateTime.Now.ToShortDateString();
now = Convert.ToDateTime(present);
//Console.WriteLine(toInd(nowS));
int a = dueDate.CompareTo(now);
if (a >= 0)
return false;
else return true;
}
but if use if (isLate(Convert.ToString(e.Value)))
{
c = Color.Red;
count++;
Console.WriteLine(count);
}
this for number of books over due, the 'count' value increases 4 time even only two books are over due in my database because this if block access 2 times while data binding and again two time when it sorts how can i get only number over due books value
Upvotes: 3
Views: 247
Reputation: 959
private void UpdateDataGridViewColor()
{
if (calledMethod == 2)
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
int j = 6;
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.ForeColor = Color.Red;
if (isLate(dataGridView1[j, i].Value.ToString()))
{
dataGridView1[j, i].Style = CellStyle;
}
}
}
}
Upvotes: 2
Reputation: 148
Actually dataGridView1_CellFormatting is fired on every single cell event, including if you sort, or even if you you hover the mouse over a cell.. very expensive in terms of performance.
Instead you could use the RowsAdded event on the dgv which will be fired one time only upon addition:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
if (isLate(dataGridView1[6, e.RowIndex].Value.ToString()))
{
dataGridView1[0, e.RowIndex].Style.ForeColor = Color.Red;
count++;
}
}
Upvotes: 2