Highlight Last entered Gridview Row(vb)

What's the best way to highlight the last grid view row that has just been inserted into a DB.
EdAsked:
Who is Participating?
 
Lokesh B RDeveloperCommented:
Hi,

Sample code here to highlight the last added row.

Protected Sub Page_Load(sender As Object, e As EventArgs)
	If Not IsPostBack Then
		Dim dt As New DataTable()
		dt.Columns.Add("Name", GetType(String))
		dt.Rows.Add("Scott")

		GridView1.DataSource = dt
		GridView1.DataBind()

		ViewState("data") = dt
	End If
End Sub

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
	If e.Row.RowType = DataControlRowType.DataRow Then
		If e.Row.Cells(0).Text = TextBox1.Text Then
			e.Row.BackColor = System.Drawing.Color.Green
		End If
	End If
End Sub

Protected Sub btnAdd_Click(sender As Object, e As EventArgs)
	Dim dt As DataTable

	If ViewState("data") Is Nothing Then
		dt = New DataTable()
		dt.Columns.Add("Name", GetType(String))
	Else
		dt = DirectCast(ViewState("data"), DataTable)
	End If

	dt.Rows.Add(TextBox1.Text)
	GridView1.DataSource = dt
	GridView1.DataBind()
End Sub

Open in new window


ASPX

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
       
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add Row" />

Open in new window

0
 
EdAuthor Commented:
perfect
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.