Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I develop software in c# window application and i am using data grid.
In my data grid i want to highlight last added row.
This is my code for add row:

C#
private void createtable()
        {
            dt = new DataTable();
            dt.Columns.Add("Frame");
            dt.Columns.Add("Time");
            dt.Columns.Add("Data");
        }

// method for add data in data grid.
public void messagedisplay(string msg)
        {
            dt.Rows.Add(frmno, totalsec, msg);
            grid1.DataSource = dt;
            grid1.Columns[0].Width = 150;
            grid1.Columns[1].Width = 150;
        }


Please solve this problem.
Posted
Updated 14-Dec-11 0:58am
v2

Hi Use following code.

C#
public void messagedisplay(string msg)
        {
            dt.Rows.Add(frmno, totalsec, msg);
            grid1.DataSource = dt;
            grid1.Columns[0].Width = 150;
            grid1.Columns[1].Width = 150;
            //Setting Last row color as Yellow
            grid1.Rows[dt.Rows.Count-1].BackColor = System.Drawing.Color.Yellow 
        }
 
Share this answer
 
v2
Comments
jaideepsinh 14-Dec-11 7:14am    
Thank's for your reply but if i use this code then i get error at .BackColor like this:
'System.Windows.Forms.DataGridViewRow' does not contain a definition for 'BackColor' and no extension method 'BackColor' accepting a first argument of type 'System.Windows.Forms.DataGridViewRow' could be found (are you missing a using directive or an assembly reference?)
hi
you can use following code:

C#
public void messagedisplay(string msg)
        {
            dt.Rows.Add(frmno, totalsec, msg);
            grid1.DataSource = dt;
            grid1.Columns[0].Width = 150;
            grid1.Columns[1].Width = 150;
            
            grid1.Rows[dt.Rows.Count-1].Selected=True
        }


last added row being select.
try it.
 
Share this answer
 
v2
public void messagedisplay(string msg)
{
frmno++;
dt.Rows.Add(frmno, totalsec, msg);
grid1.DataSource = dt;
grid1.Columns[0].Width = 150;
grid1.Columns[1].Width = 150;
if (grid1.RowCount > 0)
grid1.Rows[grid1.RowCount-1].Selected = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900