Sunita Salunkhe

Sunita Salunkhe

  • NA
  • 30
  • 16.6k

dropdownlist selected index is getting incorrect in gridview

Mar 24 2015 5:13 AM
I have gridview control on my form, in that grid i took one dropdownlist, whenever i select index above to 2, it is not selecting proper index.
 
code is bellow, please help ..
 
 <asp:GridView ID="grdMould" runat="server" AutoGenerateColumns="False" SkinID="gridviewSkin1"
OnRowDataBound="grdMould_OnRowDataBound" Width="80%">
<Columns>
<asp:TemplateField HeaderText="Sr. No.">
<ItemTemplate>
<asp:TextBox ID="txtMouldSrNo" CssClass="textAlign" runat="server" ReadOnly="true"
Text=" <%#Container.DataItemIndex+1 %>" BorderWidth="0" BackColor="LightGray"> </asp:TextBox>
</ItemTemplate>
<ControlStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Mould No">
<ItemTemplate>
<asp:DropDownList ID="ddlMouldNo" runat="server" AutoPostBack="true" BackColor="Transparent"
OnSelectedIndexChanged="ddlMouldNo_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="133px" />
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Volume" Visible="true">
<ItemTemplate>
<asp:TextBox ID="txtVolume" runat="server" CssClass="textAlign" BorderWidth="0"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="120px" />
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Height" Visible="true">
<ItemTemplate>
<asp:TextBox ID="txtHeight" runat="server" CssClass="textAlign" BorderWidth="0"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="120px" />
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
 
 
protected void grdMould_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMouldNo = (e.Row.FindControl("ddlMouldNo") as DropDownList);
var soset = dc.SoilSetting_View("CBR Modi Mould Volume").ToList();
ddlMouldNo.DataSource = soset;
ddlMouldNo.DataTextField = "SOSET_F1_var";
ddlMouldNo.DataValueField = "F2plusF3";
ddlMouldNo.DataBind();
if (ddlMouldNo.Items.Count > 0)
ddlMouldNo.Items.Insert(0, new ListItem("Select", "0"));
}
}

protected void ddlMouldNo_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;
if (gvr != null)
{
TextBox txtVolume = (TextBox)gvr.FindControl("txtVolume");
TextBox txtHeight = (TextBox)gvr.FindControl("txtHeight");
DropDownList ddlMouldNo = (DropDownList)gvr.FindControl("ddlMouldNo");
if (ddlMouldNo.SelectedIndex > 0)
{
string[] arryValues= ddlMouldNo.SelectedItem.Value.Split('|');
txtVolume.Text = arryValues[0];
txtHeight.Text = arryValues[1];
}
}

Answers (1)