Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please tell me how to highlight the last inserted record in the gridview? Data in gird view is showing from store procedure with sql server connectivity.

I'm not getting the exact code for this.

please help me out.
Posted
Updated 20-Jul-17 0:16am
v2
Comments
senguptaamlan 14-Dec-10 4:04am    
please let us know what you have tried out
Dalek Dave 14-Dec-10 4:27am    
Edited to remove Smilies (Yuck).
goelhima 14-Dec-10 4:43am    
protected void gridviewNewClaimType_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

// con = new SqlConnection("Data Source=NSEZ-DD4-028;Initial Catalog=Insurance;Integrated Security= SSPI");
con = new SqlConnection(connStr);
cmd = new SqlCommand("Create_Claim_Type_All", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Connection = con;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
gridviewNewClaimType.DataSource = ds;
gridviewNewClaimType.DataBind();
gridviewNewClaimType.PageIndex = e.NewPageIndex;
gridviewNewClaimType.Rows[gridviewNewClaimType.SelectedIndex].BackColor = System.Drawing.Color.Red;
gridviewNewClaimType.DataBind();
con.Close();
}

use the following code inside the Page_Load event handler
C#
int lastRowIndex = 0;
if (!IsPostBack)
{
    dgTest.DataSource = GetDataTable(); //the GetDataTable function returns a DataTable
    dgTest.DataBind();
}
foreach (GridViewRow grv in dgTest.Rows)
{
    if (grv.RowType == DataControlRowType.DataRow)
    {
        if (grv.RowIndex > lastRowIndex)
        {
            //getting the last data row index
            lastRowIndex = grv.RowIndex;
        }
    }
}
//dgTest.Rows[lastRowIndex].CssClass = "lastRowClass";
dgTest.Rows[lastRowIndex].BackColor = System.Drawing.Color.Red;

The lastRowClass is the name of the CSS class that can be used to highlight the last row. Check this code to see whether the last row gets the red color or not.
 
Share this answer
 
v4
Comments
Hiren solanki 14-Dec-10 4:58am    
Good answer.
goelhima 14-Dec-10 5:08am    
its not working :(
senguptaamlan 14-Dec-10 5:17am    
check the updated code and let me know...
senguptaamlan 14-Dec-10 8:45am    
if you have solved the problem let us also know...may be I'm missing something....I'm also eager to know whether the problem has been solved or not...
Ravimcts 15-Oct-12 5:54am    
gettable method is where?
please check my code...
and its still not working...when i m running this code then on the first page last row its showing red but when i fill all text boxes and save than its not working .
public partial class Create_New_Customer : System.Web.UI.Page
    {
  int lastRowIndex = 0;
 protected void Page_Load(object sender, EventArgs e)
        {
if (!IsPostBack)
            {
                // con = new SqlConnection("Data Source=NSEZ-DD4-028;Initial Catalog=Insurance;Integrated Security= SSPI");
                con = new SqlConnection(connStr);

                cmd = new SqlCommand("Create_Customer_All", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                cmd.Connection = con;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
                gridviewCustomer.DataSource = ds;
                gridviewCustomer.DataBind();
                con.Close();
//            }

            foreach (GridViewRow grv in gridviewCustomer.Rows)
            {
                if (grv.RowType == DataControlRowType.DataRow)
                {
                    if (grv.RowIndex > lastRowIndex)
                    {
                        lastRowIndex = grv.RowIndex;
                    }
                }
            }
       //     gridviewCustomer.Rows[lastRowIndex].CssClass = "lastRowClass";
            gridviewCustomer.Rows[lastRowIndex].BackColor = System.Drawing.Color.Red;
            //    con.Close();
        }
 
Share this answer
 
v2
Comments
senguptaamlan 14-Dec-10 6:25am    
I didn't got this point "but when i fill all text boxes and save than its not working " please explain a bit more
i got the solution
thanks for helping me out :)

on public class
int lastRowIndex = 0;


on save button
C#
lastRowIndex = gridviewNewClaimType.Rows.Count - 1;
           gridviewNewClaimType.Rows[lastRowIndex].BackColor = System.Drawing.Color.LightBlue;
       }
 
Share this answer
 
C#
gridview.Rows(gridview.Rows.Count - 1)
 
Share this answer
 
v2
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

if (GridView1.PageIndex == 0 && e.Row.RowIndex == 0) // This condition is used to find First record of First page
{
// Write code for First rows of First Page
}

}
if (e.Row.RowType == DataControlRowType.Footer)
{
int PageCount = gdFutureVideoPlan.PageCount-1;
int CurrentPageNo = gdFutureVideoPlan.PageIndex;
if (PageCount == CurrentPageNo) // This condition is used to find last record of last page
{
GridViewRow row= GridView1.Rows[GridView1.Rows.Count - 1];
// In this ROW you will get last row of last page

}
}
}
 
Share this answer
 
Comments
CHill60 20-Jul-17 15:19pm    
Nothing new added to a six+ year old post!
If you need to highlight the last INSERTED record in gridview,

1. You need to maintain a 'CreatedDate' column in your table.
2. On Rowbound(), you can use if..else condition as,

If(createdDate is latest date)
{
//do the highlight operation
gridviewCustomer.Rows[lastRowIndex].BackColor = System.Drawing.Color.Red;

}
 
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