data and color flickering inside the datagridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Data;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows.Forms;
<span style="color:blue using System.Threading;
<span style="color:blue using System.IO;

<span style="color:blue namespace WindowsFormsApplication2
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class Form1 : Form
{
DataSet ds = <span style="color:blue new DataSet();

<span style="color:blue public Form1()
{
InitializeComponent();



FileStream stream = <span style="color:blue new FileStream(<span style="color:#a31515 "c:\sites.xml", FileMode.Open);
<span style="color:blue try
{
ds.ReadXml(stream);
}

<span style="color:blue finally
{

stream.Close();

}

dataGridView1.DataSource = ds.Tables[0];

<span style="color:green // timer clock
<span style="color:blue this.timer1.Enabled = <span style="color:blue true;
<span style="color:blue this.timer1.Interval = 5000;
<span style="color:blue this.timer1.Tick += <span style="color:blue new System.EventHandler(<span style="color:blue this.timer1_Tick);

<span style="color:green //timer actualisation column status checkin
<span style="color:blue this.timer2.Enabled = <span style="color:blue true;
<span style="color:green //this.timer2.Interval = 50000;
<span style="color:blue this.timer2.Interval = 5000;
<span style="color:blue this.timer2.Tick += <span style="color:blue new System.EventHandler(<span style="color:blue this.timer2_Tick);

<span style="color:green //column
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Start"].DisplayIndex = 0;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "End"].DisplayIndex = 1;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Course_name"].DisplayIndex = 2;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Instructor"].DisplayIndex = 3;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "date"].DisplayIndex = 4;

<span style="color:green // Alignment property Columns inside datagridview

<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Start"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "End"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Course_name"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Instructor"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "date"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

<span style="color:green // size of each columns

<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Start"].Width = 180;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "End"].Width = 180;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Course_name"].Width = 230;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Instructor"].Width = 200;
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "status"].Width = 280;

<span style="color:green //default -false
<span style="color:blue this.dataGridView1.AllowUserToAddRows = <span style="color:blue false;

<span style="color:green //Hide column row header and column date
<span style="color:blue this.dataGridView1.RowHeadersVisible = <span style="color:blue false;
<span style="color:blue this.dataGridView1.Columns[4].Visible = <span style="color:blue false;

<span style="color:green //filter the column date for today
ds.Tables[0].DefaultView.RowFilter = <span style="color:#a31515 "date = #" + DateTime.Today + <span style="color:#a31515 "#";

<span style="color:green //change name of column 3
<span style="color:blue this.dataGridView1.Columns[<span style="color:#a31515 "Course_Name"].HeaderText = <span style="color:#a31515 "Course";

<span style="color:green //auto resize row.
<span style="color:blue this.dataGridView1.AutoResizeRows();
<span style="color:blue this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

<span style="color:blue this.dataGridView1.GridColor = Color.Black;
<span style="color:blue this.dataGridView1.Font = <span style="color:blue new System.Drawing.Font(<span style="color:#a31515 "Microsoft Sans Serif", 27.0F, System.Drawing.FontStyle.Regular);

<span style="color:green // change header color
<span style="color:blue this.dataGridView1.EnableHeadersVisualStyles = <span style="color:blue false;
<span style="color:blue this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGray;

dataGridView1.Columns[<span style="color:#a31515 "Start"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment
dataGridView1.Columns[<span style="color:#a31515 "End"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment
dataGridView1.Columns[<span style="color:#a31515 "Course_Name"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment
dataGridView1.Columns[<span style="color:#a31515 "Instructor"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment
dataGridView1.Columns[<span style="color:#a31515 "date"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment
dataGridView1.Columns[<span style="color:#a31515 "status"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; <span style="color:gray ///<span style="color:green header alignment

<span style="color:green // when xml need to be modified and update the datagridview
FileSystemWatcher incoming = <span style="color:blue new FileSystemWatcher();
incoming.Path = <span style="color:#a31515 @"c:";
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName;
incoming.Filter = <span style="color:#a31515 "sites.xml";
incoming.Changed += <span style="color:blue new FileSystemEventHandler(OnChanged);
incoming.EnableRaisingEvents = <span style="color:blue true;

}


<span style="color:blue public <span style="color:blue void OnChanged(<span style="color:blue object source, FileSystemEventArgs e)
{
<span style="color:blue this.UpdateDataGrid();
}

<span style="color:blue public <span style="color:blue void UpdateDataGrid()
{
<span style="color:blue if (<span style="color:blue this.InvokeRequired)
{
<span style="color:blue this.Invoke((MethodInvoker)<span style="color:blue delegate
{
UpdateDataGrid();
});
}
<span style="color:blue else
{
DataSet ds = <span style="color:blue new DataSet();

{
Thread.Sleep(5);
FileStream stream1 = <span style="color:blue new FileStream(<span style="color:#a31515 "c:\sites.xml", FileMode.Open);
ds.ReadXml(stream1);
stream1.Dispose();
stream1.Close();
}

<span style="color:green // dataGridView1.DataSource = ds.Tables[0];
<span style="color:blue int count = 0;
<span style="color:blue foreach (DataRow dr <span style="color:blue in ds.Tables[0].Rows)
{
String StartCourse = dr[0].ToString();
<span style="color:blue string EndCourse = dr[1].ToString();
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(StartCourse);
DateTime EndTime = Convert.ToDateTime(EndCourse);

<span style="color:blue if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = <span style="color:#a31515 "ok";

}

<span style="color:blue else
{
ds.Tables[0].Rows[count][5] = <span style="color:#a31515 "nok";


}

count++;
dataGridView1.DataSource = ds.Tables[0];
}
ds.Tables[0].DefaultView.RowFilter = <span style="color:#a31515 "date = #" + DateTime.Today + <span style="color:#a31515 "#";

}

ColorGrid();
}

<span style="color:blue private <span style="color:blue void timer1_Tick(<span style="color:blue object sender, EventArgs e)
{
<span style="color:blue this.UpdateDataGrid();
<span style="color:blue this.label1.Text = DateTime.Now.ToString(<span style="color:#a31515 "dddd, MMMM dd, yyyy hh:mm tt");
}

<span style="color:blue void ColorGrid()
{
<span style="color:blue foreach (DataGridViewRow row <span style="color:blue in <span style="color:blue this.dataGridView1.Rows)
{
Color cellColor = Color.White;

<span style="color:blue if (row.Cells[5].Value.ToString() == <span style="color:#a31515 "ok")
{
cellColor = Color.Green;
<span style="color:green //row.DefaultCellStyle.BackColor = Color.Green;
}
<span style="color:blue else
{
cellColor = Color.Red;
<span style="color:green //row.DefaultCellStyle.BackColor = Color.Red;
}

<span style="color:blue foreach (DataGridViewCell cell <span style="color:blue in row.Cells)
{
cell.Style.BackColor = cellColor;
cell.Style.SelectionBackColor = cellColor;
cell.Style.SelectionForeColor = Color.Black;
}
}
}


<span style="color:blue private <span style="color:blue void timer2_Tick(<span style="color:blue object sender, EventArgs e)
{
<span style="color:blue this.UpdateDataGrid();

<span style="color:blue int count = 0;
<span style="color:blue foreach (DataRow dr <span style="color:blue in ds.Tables[0].Rows)
{

String StartCourse = dr[0].ToString();
<span style="color:blue string EndCourse = dr[1].ToString();
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(StartCourse);
DateTime EndTime = Convert.ToDateTime(EndCourse);

<span style="color:blue if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = <span style="color:#a31515 "ok";

}

<span style="color:blue else
{
ds.Tables[0].Rows[count][5] = <span style="color:#a31515 "nok";

}

count++;
dataGridView1.DataSource = ds.Tables[0];
ColorGrid();
}

}
}
}

[/code]

I have a bug in my code. when I start and stop the programm ---> no problem
I have a problem when I modify the data into the XML file. after the datagriview update the new data into the datagridview and start flickering periodically.( because timer2?)
Its seems periodically the data and the color into my datagridview are flickering because the timer 2 refresh my datagridview.
This application when you modify your data in the XML by the notepad, the program reload the datagriview by the method onchanged. I need the timer 2 because I refresh/compare the data in one column(5). could you take a look? thanks for helping me... I begin
in C# any help or suggestion would be appreciated :)
the XML file:


<pre lang="x-xml <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
<Courses>
<Start>01:00 AM</Start>
<End>01:19 PM</End>
<Course_name>CIS</Course_name>
<Instructor>herbert</Instructor>
<date>01/15/2012</date>
<status>........</status>
</Courses>
</table>[/code]
<br/>
<br/>

<br/>
<br/>

View the full article
 
Back
Top