Hi makumbi,
Use FindByValue and check for not null before set Selected to True.
Please refer below sample.
HTML
<asp:GridView ID="gvStockBooks" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvStockBooks_RowDataBound">
<Columns>
<asp:BoundField DataField="Account" HeaderText="Account" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" />
<asp:BoundField DataField="StdType" HeaderText="StdType" />
<asp:BoundField DataField="Stream" HeaderText="Stream" />
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:Label ID="lblSex" runat="server" Text='<%# Eval("Sex") %>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlsex" runat="server">
<asp:ListItem>MALE</asp:ListItem>
<asp:ListItem>FEMALE</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Colour" HeaderText="Colour" />
<asp:BoundField DataField="Acnos" HeaderText="ACNOS" />
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports Systems.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GridBind();
}
}
private void GridBind()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[8]{new DataColumn("Account")
,new DataColumn("Name")
,new DataColumn("Class")
,new DataColumn("StdType")
,new DataColumn("Stream")
,new DataColumn("Sex")
,new DataColumn("Colour")
,new DataColumn("Acnos") });
dt.Rows.Add("19-07485", "AAMILAH NYANZI GAJU", "P2", "Old", "W", "FEMALE", "Red", "01-18-144");
dt.Rows.Add("15-05111", "AAYAN MOHAMMED", "P5", "Old", "G", "MALE", "Yellow", "01-15-270");
dt.Rows.Add("19-07710", "ABAASA KINGSTON TUMWEBAZE", "P3", "Old", "L", "MALE", "Yellow", "02-17-203");
dt.Rows.Add("16-05636", "ABAASA MARY", "P5", "Old", "H", "FEMALE", "Yellow", "01-15-244");
dt.Rows.Add("14-04640", "ABAHO MELANIE", "P6", "Old", "D", "FEMALE", "Red", "01-14-041");
dt.Rows.Add("20-02071", "ABAHO TROY MUSINGUZI", "P4", "New", "", "", "", "");
dt.Rows.Add("16-05545", "ABAYO FRANCIS MELVIN ", "P5", "Old", "L", "MALE", "Red", "01-14-055");
dt.Rows.Add("17-06484", "ABDOU HAKIM SECK", "P3", "Old", "C", "MALE", "Blue", "02-17-130");
dt.Rows.Add("15-04806", "AGABA JEREMY", "P6", "Old", "D", "", "Green", "01-14-249");
gvStockBooks.DataSource = dt;
gvStockBooks.DataBind();
}
protected void gvStockBooks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlSex = (e.Row.FindControl("ddlsex") as DropDownList);
ddlSex.DataTextField = "Sex";
ddlSex.DataValueField= "Sex";
ddlSex.DataBind();
ddlSex.Items.Insert(0, new ListItem("please Select..", "0"));
string sex = (e.Row.FindControl("lblSex") as Label).Text;
if (ddlSex.Items.FindByValue(sex) != null)
{
ddlSex.Items.FindByValue(sex).Selected = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GridBind()
End If
End Sub
Private Sub GridBind()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(7) {New DataColumn("Account"), New DataColumn("Name"), New DataColumn("Class"), New DataColumn("StdType"), New DataColumn("Stream"), New DataColumn("Sex"), New DataColumn("Colour"), New DataColumn("Acnos")})
dt.Rows.Add("19-07485", "AAMILAH NYANZI GAJU", "P2", "Old", "W", "FEMALE", "Red", "01-18-144")
dt.Rows.Add("15-05111", "AAYAN MOHAMMED", "P5", "Old", "G", "MALE", "Yellow", "01-15-270")
dt.Rows.Add("19-07710", "ABAASA KINGSTON TUMWEBAZE", "P3", "Old", "L", "MALE", "Yellow", "02-17-203")
dt.Rows.Add("16-05636", "ABAASA MARY", "P5", "Old", "H", "FEMALE", "Yellow", "01-15-244")
dt.Rows.Add("14-04640", "ABAHO MELANIE", "P6", "Old", "D", "FEMALE", "Red", "01-14-041")
dt.Rows.Add("20-02071", "ABAHO TROY MUSINGUZI", "P4", "New", "", "", "", "")
dt.Rows.Add("16-05545", "ABAYO FRANCIS MELVIN ", "P5", "Old", "L", "MALE", "Red", "01-14-055")
dt.Rows.Add("17-06484", "ABDOU HAKIM SECK", "P3", "Old", "C", "MALE", "Blue", "02-17-130")
dt.Rows.Add("15-04806", "AGABA JEREMY", "P6", "Old", "D", "", "Green", "01-14-249")
gvStockBooks.DataSource = dt
gvStockBooks.DataBind()
End Sub
Protected Sub gvStockBooks_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlSex As DropDownList = (TryCast(e.Row.FindControl("ddlsex"), DropDownList))
ddlSex.DataTextField = "Sex"
ddlSex.DataValueField = "Sex"
ddlSex.DataBind()
ddlSex.Items.Insert(0, New ListItem("please Select..", "0"))
Dim sex As String = (TryCast(e.Row.FindControl("lblSex"), Label)).Text
If ddlSex.Items.FindByValue(sex) IsNot Nothing Then
ddlSex.Items.FindByValue(sex).Selected = True
End If
End If
End Sub
Screenshot