添加新行时会覆盖Gridview数据

时间:2014-01-14 21:01:14

标签: asp.net gridview modalpopupextender

在我的设置中,我想要使用 ModalPopupExtender 填充 GridView 。 ModalPopupExtender包含一个面板(ID =“FlowPanel”),其中包含几个控件:标签,下拉列表和两个按钮,取消提交

取消 按钮(ID =“CancelFlowsButton”)是取消ModalPopupExtender的按钮,而 提交 按钮(ID =“AddFlowButton”)使用面板 “FlowPanel” 中的数据在GridView中创建一个新行。

使用我的代码,行可以很好地添加到GridView中。但是,每次添加新行时,先前创建的行中的数据都会覆盖新创建的行的值。

我不确定我做错了什么。由于我是ASP.NET的初学者,请耐心等待,任何帮助将不胜感激。谢谢!

Default.aspx.cs代码(快照)

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            FirstFlowGridViewRow();
        }
    }

    protected void AddFlowButton_Click(object sender, EventArgs e)
    {
        AddNewFlowGridViewRow();          
        FlowModalPopupExtender.Hide();
    }


    protected void FirstFlowGridViewRow()
    {
        DataTable table = new DataTable();
        DataRow dr = null;

        table.Columns.Add(new DataColumn("Col1", typeof(string)));
        table.Columns.Add(new DataColumn("Col2", typeof(string)));
        table.Columns.Add(new DataColumn("Col3", typeof(int)));
        table.Columns.Add(new DataColumn("Col4", typeof(int)));
        table.Columns.Add(new DataColumn("Col5", typeof(string)));
        table.Columns.Add(new DataColumn("Col6", typeof(string)));
        table.Columns.Add(new DataColumn("Col7", typeof(int)));

        dr = table.NewRow();

        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;
        dr["Col3"] = 0;
        dr["Col4"] = 0;
        dr["Col5"] = string.Empty;
        dr["Col6"] = string.Empty;
        dr["Col7"] = 0;
        table.Rows.Add(dr);

        ViewState["currentTable"] = table;
        FlowGridView.DataSource = table;
        FlowGridView.DataBind();
    }

    protected void AddNewFlowGridViewRow()
    {
        int rowIndex = 0;

        if (ViewState["currentTable"] != null)
        {
            DataTable currentDt = (DataTable) ViewState["currentTable"];
            DataRow currentRow = null;
            if(currentDt.Rows.Count > 0)
            {
                for(int i = 1; i <= currentDt.Rows.Count; i++)
                {
                    currentRow = currentDt.NewRow();

                    currentDt.Rows[i - 1]["Col1"] = NameTextBox.Text;
                    currentDt.Rows[i - 1]["Col2"] = DirectionDropDownList.SelectedValue;
                    currentDt.Rows[i - 1]["Col3"] = StartYearTextBox.Text;
                    currentDt.Rows[i - 1]["Col4"] = EndYearTextBox.Text; ;
                    currentDt.Rows[i - 1]["Col5"] = TimingDropDownList.SelectedValue;
                    currentDt.Rows[i - 1]["Col6"] = TypeDropDownList.SelectedValue;
                    currentDt.Rows[i - 1]["Col7"] = AmountTextBox.Text;

                    rowIndex++;
                }

                currentDt.Rows.Add(currentRow);
                ViewState["currentTable"] = currentDt;

                FlowGridView.DataSource = currentDt;
                FlowGridView.DataBind();
            }
        }

        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousFlowData();
    }

    protected void SetPreviousFlowData()
    {
        int rowIndex = 0;

        if (ViewState["currentTable"] != null)
        {
            DataTable currentDt = (DataTable) ViewState["currentTable"];

            if(currentDt.Rows.Count > 0)
            {
                for(int i = 0; i < currentDt.Rows.Count; i++)
                {
                    Label LabelName = (Label)FlowGridView.Rows[rowIndex].Cells[1].FindControl("NameLabel");                      
                    DropDownList DropDownDirection = (DropDownList)FlowGridView.Rows[rowIndex].Cells[2].FindControl("GridDirectionDropDownList");
                    Label LabelStart = (Label)FlowGridView.Rows[rowIndex].Cells[3].FindControl("StartYearLabel");
                    Label LabelEnd = (Label)FlowGridView.Rows[rowIndex].Cells[4].FindControl("EndYearLabel");
                    DropDownList DropDownTiming = (DropDownList)FlowGridView.Rows[rowIndex].Cells[5].FindControl("GridTimingDropDownList");
                    DropDownList DropDownType = (DropDownList)FlowGridView.Rows[rowIndex].Cells[6].FindControl("GridTypeDropDownList");
                    Label LabelAmount = (Label)FlowGridView.Rows[rowIndex].Cells[7].FindControl("AmountLabel");

                    LabelName.Text = currentDt.Rows[i]["Col1"].ToString();
                    DropDownDirection.SelectedValue = currentDt.Rows[i]["Col2"].ToString();
                    LabelStart.Text = currentDt.Rows[i]["Col3"].ToString();
                    LabelEnd.Text = currentDt.Rows[i]["Col4"].ToString();
                    DropDownTiming.SelectedValue = currentDt.Rows[i]["Col5"].ToString();
                    DropDownType.SelectedValue = currentDt.Rows[i]["Col6"].ToString();
                    LabelAmount.Text = currentDt.Rows[i]["Col7"].ToString();
                    rowIndex++;
                }
            }
        }
    }

Default.aspx代码(快照)

                   <table style="height: 180px; width: 755px;">
                        <tr>
                            <td class="style7" valign="top">
                                <div style="overflow: auto; height: 200px; width: 669px">
                                    <asp:GridView ID="FlowGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
                                        Width="577px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
                                        AutoGenerateEditButton="True" OnRowEditing="FlowGridView_RowEditing" OnRowUpdating="FlowGridView_RowUpdating"
                                        OnRowCancelingEdit="FlowGridView_RowCancelingEdit">
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
                                        <Columns>
                                            <asp:TemplateField HeaderText="Name">
                                                <ItemStyle Font-Size="13px" Width="30%" />
                                                <ItemTemplate>
                                                    <asp:Label ID="NameLabel" runat="server" Text="Label"></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Direction">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridDirectionDropDownList" runat="server">
                                                        <asp:ListItem Value="W">Withdrawal</asp:ListItem>
                                                        <asp:ListItem Value="C">Contribution</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Start Year">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:Label ID="StartYearLabel" runat="server" Text="Label"></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="End Year">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:Label ID="EndYearLabel" runat="server" Text="Label"></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Timing">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridTimingDropDownList" runat="server">
                                                        <asp:ListItem Value="E">End of Year</asp:ListItem>
                                                        <asp:ListItem Value="B">Beginning of Year</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Inflation Adj.">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridTypeDropDownList" runat="server">
                                                        <asp:ListItem Value="Y">Yes</asp:ListItem>
                                                        <asp:ListItem Value="N">No</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Amount">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:Label ID="AmountLabel" runat="server" Text="Label"></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <EditRowStyle BackColor="#999999" />
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
                                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                    </asp:GridView>
                                </div>
                            </td>
                            <td valign="top">
                                <asp:Button ID="AddCashFlowButton" runat="server" Text="Add Cash Flow..." 
                                    Width="158px" />
                                <asp:ModalPopupExtender ID="FlowModalPopupExtender" runat="server" DynamicServicePath=""
                                    Enabled="True" TargetControlID="AddCashFlowButton" PopupControlID="FlowPanel"
                                    CancelControlID="CancelFlowsButton" BackgroundCssClass="modalBackground">
                                </asp:ModalPopupExtender>
                            </td>
                        </tr>
                    </table>

1 个答案:

答案 0 :(得分:1)

您正在覆盖数据。将AddNewFlowGridViewRow()方法更改为:

protected void AddNewFlowGridViewRow()
{

    if (ViewState["currentTable"] != null)
    {
        DataTable currentDt = (DataTable)ViewState["currentTable"];
        DataRow currentRow = null;
        currentRow = currentDt.NewRow();

        currentRow["Col1"] = NameTextBox.Text;
        currentRow["Col2"] = DirectionDropDownList.SelectedValue;
        currentRow["Col3"] = StartYearTextBox.Text;
        currentRow["Col4"] = EndYearTextBox.Text; ;
        currentRow["Col5"] = TimingDropDownList.SelectedValue;
        currentRow["Col6"] = TypeDropDownList.SelectedValue;
        currentRow["Col7"] = AmountTextBox.Text;                            

        currentDt.Rows.Add(currentRow);
        ViewState["currentTable"] = currentDt;

        FlowGridView.DataSource = currentDt;
        FlowGridView.DataBind();
    }

    else
    {
        Response.Write("ViewState is null");
    }
    SetPreviousFlowData();
}

编辑: 如果要在插入新行时删除默认的空行,可以这样做:

protected void AddNewFlowGridViewRow()
{
    int rowIndex = 0;

    if (ViewState["currentTable"] != null)
    {
        DataTable currentDt = (DataTable)ViewState["currentTable"];
        DataRow currentRow = currentDt.NewRow();

        currentRow["Col1"] = NameTextBox.Text;
        currentRow["Col2"] = DirectionDropDownList.SelectedValue;
        currentRow["Col3"] = StartYearTextBox.Text;
        currentRow["Col4"] = EndYearTextBox.Text; ;
        currentRow["Col5"] = TimingDropDownList.SelectedValue;
        currentRow["Col6"] = TypeDropDownList.SelectedValue;
        currentRow["Col7"] = AmountTextBox.Text;

        currentDt.Rows.Add(currentRow);

        if (currentDt.Rows.Count > 1)
        {
            DataRow firstRow = currentDt.Rows[0];

            if ((String.IsNullOrEmpty(firstRow["Col1"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col2"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col5"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col6"].ToString())) &&
                (firstRow["Col3"].ToString() == "0" &&
                    firstRow["Col4"].ToString() == "0" &&
                    firstRow["Col7"].ToString() == "0"))
            {
                currentDt.Rows.RemoveAt(0);
            }
        }

        ViewState["currentTable"] = currentDt;

        FlowGridView.DataSource = currentDt;
        FlowGridView.DataBind();
    }

    else
    {
        Response.Write("ViewState is null");
    }
    SetPreviousFlowData();
}