Hi Vincenzo67,
Use GridView OnRowDataBound event to set the DropDownList selected value based on the column value.
Refer below sample. I have used temporary DataTable for binding the GridView. You need to replace the binding code with your MySql client code.
HTML
<asp:GridView ID="gvProducts3" runat="server" DataKeyNames="ID" EnableViewState="true"
CssClass="mGrid" HorizontalAlign="Center" OnRowDataBound="gvProducts3_RowDataBound"
AutoGenerateColumns="false" Width="100%">
<AlternatingRowStyle CssClass="altrows" />
<Columns>
<asp:TemplateField HeaderText="DDL" ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div>
<asp:DropDownList ID="ddl_I" runat="server" AutoPostBack="true"
BackColor="Yellow" CssClass="ddl_Class_new">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
gvProducts3.DataSource = RetrieveProducts_reco3();
gvProducts3.DataBind();
}
private DataTable RetrieveProducts_reco3()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("tMotiv");
dt.Rows.Add(1, "OK");
dt.Rows.Add(1, "KO");
return dt;
}
protected void gvProducts3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
DropDownList ddl_I = e.Row.FindControl("ddl_I") as DropDownList;
if (ddl_I.Items.FindByValue(dr["tMotiv"].ToString()) != null)
{
ddl_I.Items.FindByValue(dr["tMotiv"].ToString()).Selected = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
gvProducts3.DataSource = RetrieveProducts_reco3()
gvProducts3.DataBind()
End Sub
Private Function RetrieveProducts_reco3() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("tMotiv")
dt.Rows.Add(1, "OK")
dt.Rows.Add(1, "KO")
Return dt
End Function
Protected Sub gvProducts3_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim ddl_I As DropDownList = TryCast(e.Row.FindControl("ddl_I"), DropDownList)
If ddl_I.Items.FindByValue(dr("tMotiv").ToString()) IsNot Nothing Then
ddl_I.Items.FindByValue(dr("tMotiv").ToString()).Selected = True
End If
End If
End Sub
Screenshot