Here I have created sample, in this I have used DataGridView with Refresh Button. After refreshing it highlights the Updated Row.
Code
DataTable previousData = new DataTable();
private void BindGrid()
{
string strQuery = "select Top 10 CustomerID, ContactName, City, Country from customers";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable updatedDt = new DataTable();
updatedDt = previousData;
if (updatedDt.Rows.Count > 0)
{
updatedDt = GetUpdatedData();
}
else
{
updatedDt = GetData(cmd);
previousData = updatedDt;
}
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = updatedDt;
lblTime.Text = "Last Refreshed : " + DateTime.Now.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
this.UpdateRowColor();
}
private DataTable GetUpdatedData()
{
DataTable dtUpdated = new DataTable();
if (previousData != null)
{
DataTable dt = previousData;
string strQuery = "select Top 20 CustomerID, ContactName, City, Country from customers ORDER BY ContactName";
SqlCommand cmd = new SqlCommand(strQuery);
dtUpdated = GetData(cmd);
previousData = dtUpdated;
DataColumn column = dtUpdated.Columns.Add();
column.ColumnName = "Updated";
foreach (DataRow dr in dtUpdated.Rows)
{
foreach (DataRow dr1 in dt.Rows)
{
if (dr["CustomerID"].ToString() == dr1["CustomerID"].ToString())
{
dr["Updated"] = "NO";
}
}
}
}
return dtUpdated;
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = "Data Source=192.168.0.1\\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
BindGrid();
this.UpdateRowColor();
}
private void UpdateRowColor()
{
if (previousData.Rows.Count > 0)
{
DataTable updatedDt = new DataTable();
updatedDt = previousData;
if (updatedDt != null && updatedDt.Columns.Contains("Updated"))
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string updated = row.Cells[4].Value.ToString();
if (updated != "NO")
{
dataGridView1.Columns["Updated"].Visible = false;
row.DefaultCellStyle.BackColor = Color.Yellow;
}
}
}
}
}
Screenshot