akhter says:
string
selectcontainer = DataBinder.Eval(e.Row.DataItem,
"Contnam"
).ToString();
Problem is in you are using FindByValue method and passing the CN_Name to find which should be CN_ID.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" DataKeyNames="CN_ID"
HorizontalAlign="Center" AutoGenerateColumns="false" CssClass="mygrdContent"
HeaderStyle-BackColor="#66ccff" HeaderStyle-CssClass="header" HeaderStyle-ForeColor="White"
OnRowDataBound="GridView1_RowDataBound"
PagerStyle-CssClass="pager" RowStyle-CssClass="rows" Width="452px" ShowFooter="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="D_CID" runat="server" Text='<%#Bind("CN_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Container">
<ItemTemplate>
<%#Eval("CN_Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Contnam" runat="server" Width="150px">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="QTY" runat="server" Text='<%#Eval("QTY") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal1" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="TRUE" ShowDeleteButton="True" ControlStyle-ForeColor="Blue" />
</Columns>
<HeaderStyle BackColor="#66CCFF" ForeColor="White" />
<SelectedRowStyle BackColor="Yellow" />
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CN_ID"), new DataColumn("CN_Name"), new DataColumn("QTY") });
dt.Rows.Add(1, "Mango", 10);
dt.Rows.Add(2, "Apple", 12);
dt.Rows.Add(3, "Orange", 12);
dt.Rows.Add(4, "Banana", 10);
ViewState["dt"] = dt;
this.BindGrid();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddlContnam = (DropDownList)e.Row.FindControl("Contnam");
DataTable dt = ViewState["dt"] as DataTable;
ddlContnam.DataSource = dt;
ddlContnam.DataTextField = "CN_Name";
ddlContnam.DataValueField = "CN_ID";
ddlContnam.DataBind();
string selectcontainer = DataBinder.Eval(e.Row.DataItem, "CN_ID").ToString();
if (ddlContnam.Items.FindByValue(selectcontainer) != null)
{
ddlContnam.Items.FindByValue(selectcontainer).Selected = true;
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void GridView1_RowUpdating(object sender, EventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void BindGrid()
{
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CN_ID"), New DataColumn("CN_Name"), New DataColumn("QTY")})
dt.Rows.Add(1, "Mango", 10)
dt.Rows.Add(2, "Apple", 12)
dt.Rows.Add(3, "Orange", 12)
dt.Rows.Add(4, "Banana", 10)
ViewState("dt") = dt
Me.BindGrid()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then
Dim ddlContnam As DropDownList = CType(e.Row.FindControl("Contnam"), DropDownList)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
ddlContnam.DataSource = dt
ddlContnam.DataTextField = "CN_Name"
ddlContnam.DataValueField = "CN_ID"
ddlContnam.DataBind()
Dim selectcontainer As String = DataBinder.Eval(e.Row.DataItem, "CN_ID").ToString()
If ddlContnam.Items.FindByValue(selectcontainer) IsNot Nothing Then
ddlContnam.Items.FindByValue(selectcontainer).Selected = True
End If
End If
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As EventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub BindGrid()
GridView1.DataSource = TryCast(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
Screenshot