Hi,
I need use ASP.Net DropDownList control in GridView control using C# for Populate DropDownList with Value stored in database.
The GridView it's generated using the general table tb_g where a whole series of CIGM codes are stored.
+----+--------------+
| ID | CIGM |
+----+--------------+
| 1 | XXXX23000022 |
| 2 | XXXX23000027 |
| 3 | XXXX23000039 |
| 4 | XXXX23000043 |
+----+--------------+
Using the DropDownList tCon editing the value OK or KO and storing this value of DropDownList tCon and the value of column CIGM on the tb_t.
+----+--------------+------+
| ID | CIGM | tCon |
+----+--------------+------+
| 1 | XXXX23000022 | OK |
+----+--------------+------+
After insert in table tb_t when return of gridview page I have this error.
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'tCon'.
Can you help me?
My code below
<asp:BoundField DataField="CIGM"
HeaderText="CIGM"
ReadOnly="true" HtmlEncode="false" />
<asp:TemplateField
HeaderText="tCon">
<itemtemplate>
<asp:DropDownList ID="tCon" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="tCon_SelectedIndexChanged">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
</itemtemplate>
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList tCon = (DropDownList)e.Row.FindControl("tCon");
string CIGM = e.Row.Cells[2].Text;
string sql = "SELECT DISTINCT `tCon` " +
"FROM `tb_t` " +
"WHERE CIGM = '" + CIGM.ToUpper() + "'; ";
string conString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con =
new MySqlConnection(conString))
{
using (MySqlDataAdapter sda =
new MySqlDataAdapter(sql, con))
{
using (DataTable dt =
new DataTable())
{
sda.Fill(dt);
tCon.DataSource = dt;
tCon.DataTextField = "tCon";
tCon.DataValueField = "tCon";
tCon.DataBind();
string tCon2 = DataBinder.Eval(e.Row.DataItem, "tCon").ToString();
tCon.Items.FindByValue(tCon2).Selected = true;
}
}
}
}
}
SET @s = CONCAT('SELECT * FROM `tb_g` t LEFT JOIN `tb_t` p
ON p.CIGM = t.CIGM;');
PREPARE `stmt` FROM @`s`;
SET @`s` := NULL;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;