Please run this code in a new page.
I have made is use of IsPostBack condition to preseve the Item's color of DropDownList.
HTML
<div>
<asp:DropDownList ID="ddlColor" runat="server" AutoPostBack="true">
</asp:DropDownList>
</div>
Namespace
Imports System.Data
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "UK")
dt.Rows.Add(2, "India")
dt.Rows.Add(3, "Germany")
dt.Rows.Add(4, "India")
dt.Rows.Add(4, "India")
Me.ddlColor.DataValueField = "Id"
Me.ddlColor.DataTextField = "Country"
Me.ddlColor.DataSource = dt
Me.ddlColor.DataBind()
ViewState("DataTable") = dt
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("Country").ToString().ToUpper() = "INDIA" Then
ddlColor.Items(i).Attributes.CssStyle.Add("color", "DarkTurquoise")
End If
Next
End If
Dim dtable As DataTable = DirectCast(ViewState("DataTable"), DataTable)
For i As Integer = 0 To dtable.Rows.Count - 1
If dtable.Rows(i)("Country").ToString().ToUpper() = "INDIA" Then
ddlColor.Items(i).Attributes.CssStyle.Add("color", "DarkTurquoise")
End If
Next
End Sub
Screenshot
