OnRowDataBound="grdViewCInfo_RowDataBound"
>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Concert Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150px" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Bind("Concert_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Bind("Concert_Name") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150px" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="lblAddr" runat="server" Text='<%#Bind("Address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddr" runat="server" Text='<%#Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#Bind("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%#Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pincode" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblPincode" runat="server" Text='<%#Bind("Pincode") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPincode" runat="server" Text=' <%#Bind("Pincode") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblcountry" runat="server" Text='<%#Bind("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Concert Date and Time" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblDateTime" runat="server" Text='<%#Bind("Concert_Date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDateTime" runat="server" Text='<%#Bind("Concert_Date") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CausesValidation="False">
<ItemStyle Width="50px" />
</asp:CommandField>
<asp:CommandField ShowDeleteButton="true">
<ItemStyle Width="50px" />
</asp:CommandField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
答案 0 :(得分:1)
(DropDownList)grdViewCInfo.Rows[e.NewEditIndex].Cells[4].FindControl("ddlCountry");
返回什么,ddl
设置了什么?
我估计不是。也许在那个细胞中没有“ddlCountry”控制。
这是一个奇怪的巧合线,
//grdViewCInfo.Rows[e.NewEditIndex].Cells[4].Controls.Add(ddl);
在下面说明。
答案 1 :(得分:1)
其中一部分也应该在RowDataBound上完成。
以下是一些示例代码
private static DataSet ds;
private const string query = "select * from tblcountrynames";
protected void grdViewCInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
grdViewCInfo.EditIndex = e.NewEditIndex;
//guessing that this is your databind event
dbLoad();
}
protected void grdViewCInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
//your ddl
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCountry");
PopulateCountries(ddl, query);
//grdViewCInfo.Rows[e.NewEditIndex].Cells[4].Controls.Add(ddl);
}
}
private void PopulateCountries(DropDownList ddl, string query)
{
if(ds!= null && ds.Tables[0].Rows.Count >0)
MySqlConnection objMycon1 = new MySqlConnection(strProvider);
//commenting open; as adapter doesn't need connection to be open
//objMycon1.Open();
MySqlCommand cmd1 = new MySqlCommand(query, objMycon1);
MySqlDataAdapter da = new MySqlDataAdapter(cmd1);
ds = new DataSet();
da.Fill(ds);
objMycon1.Close();
objMycon1.Dispose();//comment if objMycon1 is not IDisposible
}
if (ds.Tables[0].Rows.Count > 0)
{
ddl.DataSource = ds;
ddl.DataTextField = "Name";
ddl.DataValueField = "ID";
ddl.DataBind();
}
}
这是应该怎么做的。
希望这会有所帮助。
答案 2 :(得分:0)
您的EditItemTemplate中有下拉列表吗?
编辑:我认为您在RowEditing事件中实际上并不处于编辑模式。如果将代码向下移动到RowCommand,
然后检查编辑命令。将代码包装成类似的内容,
if(e.CommandName == "Edit")
{
// do your edit here.
}
虽然请保留grdViewCInfo.EditIndex = e.NewEditIndex;
行,因为您需要它。