I want to place a static dorpdownlist to EditItemTemplate of the GridView.
<EditItemTemplate>
<%--<asp:TextBox ID="txtSuppstatus" Width="40px" runat="server" Text='<%#Eval("Suppstatus") %>' />--%>
<asp:DropDownList ID="ddlSuppstatus" AutoPostBack="true" runat="server" SelectedValue='<%#Eval("Suppstatus") %>'>
<asp:ListItem Text="YES" Value="Y"></asp:ListItem>
<asp:ListItem Text="NO" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
When page will load YES or NO option will show as a text but when user will edit the row on click on Edit it will show dorpdownlist with YES and NO option.
Dropdownlist is binding on edit but i am loosing all the data at the time of edit.
I have gone through to this link but facing the same problem. rest all the control is getting blank and dropdownlist is binding with YES and No option.
on page load
after edit
Please help me how to solve this.
Below is working code:
Added OnRowDataBound:
protected void grvSupplierStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow && grvSupplierStatus.EditIndex == e.Row.RowIndex)
{
DropDownList ddlSupStatus = (DropDownList)e.Row.FindControl("ddlSupstatus");
Label lblsuppstatus = (Label)e.Row.FindControl("lblsuppStatus");
DataSet ds = new DataSet();
ds = GetYesNoValue("suppStatus");
DataTable dt = new DataTable();
dt = ds.Tables[0];
ddlSupStatus .DataSource = dt;
ddlSupStatus .DataTextField = "suppStatus";
ddlSupStatus .DataValueField = "suppStatus";
ddlSupStatus .DataBind();
ddlSupStatus.Items.FindByValue(lblsuppstatus.Text).Selected = true;
}
}
catch (Exception ex)
{
}
}
Added Generic Method
public DataSet GetYesNoValue(string ColumnName)
{
DataTable dtVal = new DataTable();
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = ColumnName;
dtVal.Columns.Add(column);
DataSet dsVal = new DataSet();
dtVal.Rows.Add("--Select--");
dtVal.Rows.Add("Yes");
dtVal.Rows.Add("No");
dsVal.Tables.Add(dtVal);
return dsVal;
}
Added the label and dropdownlist inside the EditItemTemplate
<asp:Label Text='<%#Eval("Suppstatus") %>' Visible="false" ID="lblsuppStatus" runat="server" />
<asp:DropDownList runat="server" ID="ddlSupStatus"> </asp:DropDownList>