DropdownList没有限制在Gridview的最后一行

时间:2017-03-20 08:55:22

标签: c# asp.net

我将列表项绑定到下拉列表,而gridview_OnRowDateBound。 它的工作性很好,除了最后一行Grid。

我的代码是,

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" AllowSorting="True"
            CellPadding="4" ForeColor="Black" AllowPaging="True" PageSize="20" Width="100%" OnRowDataBound="OnRowDataBound"
            OnPageIndexChanging="Grid_PageIndexChanging" Font-Size="9pt" BackColor="White"
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" GridLines="Vertical" ShowFooter="true">

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (e.Row.FindControl("ddl") as DropDownList);
            for (int i = 0; i < (grid.Rows.Count); i++)
            {
                DropDownList ddlEmployee = (DropDownList)grid.Rows[i].FindControl("ddl");
                Gender = grid.Rows[i].Cells[3].Text.ToString();
                ddlEmployee.Items.Clear();
                ddlEmployee.Items.AddRange(GetLeaveCategorybyGender(Gender).Cast<ListItem>().ToArray());
                try
                {
                    ddlEmployee.DataBind();
                }catch(Exception ex){}
}


private ListItemCollection GetLeaveCategorybyGender(string Gender)
    {

        var listItemCollection = new ListItemCollection();
        if (Gender.ToLower().Trim() == "Male".ToLower().Trim())
        {
            listItemCollection.Add(new ListItem("Father", "Father"));
            listItemCollection.Add(new ListItem("Son", "Son"));
        }
        if (Gender.ToLower().Trim() == "Female".ToLower().Trim())
        {
            listItemCollection.Add(new ListItem("Mother", "Mother"));
            listItemCollection.Add(new ListItem("Daughter", "Daughter"));
        }
        return listItemCollection;

    }

可以解释有什么问题吗?

0 个答案:

没有答案