向gridView

时间:2016-11-05 09:17:12

标签: c# asp.net twitter-bootstrap gridview

我想在插入的行之后添加新行。我有一个按钮,显示带有一些DropDownLists和Textboxes的bootstrap模态。点击模式中的按钮应该将模态中的数据插入到表中。问题是,当我尝试添加新行时,它将替换当前行。我做错了什么?这是我的代码示例: 的.cs:

protected void addTrip_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        dostawca = DropDownDostawca.SelectedValue.ToString();
        cel = txtCel.InnerText;
        krajZagr = "";
        kraj = DropDownKraj.SelectedValue.ToString();
        miasto = txtMiasto.Text;
        if(rbKraj.Checked == true)
        {
            krajZagr = "Krajowa";
        }
        if(rbZag.Checked == true)
        {
            krajZagr = "Zagraniczna";
        }

        if(cel=="" || miasto=="" || kraj=="" || dostawca=="" )
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowPopup2();", true);

        }
        else
        {
            if(gridViewTrips.Columns.Count == 0)
            {

                DataRow dr = dt.NewRow();
                DataColumn dc = new DataColumn("DO_KOGO");
                DataColumn dc2 = new DataColumn("CEL");
                DataColumn dc3 = new DataColumn("KRAJOWA_ZAGRANICZNA");
                DataColumn dc4 = new DataColumn("KRAJ");
                DataColumn dc5 = new DataColumn("MIASTO");

                dt.Columns.Add(dc);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
                dt.Columns.Add(dc5);

                dr["DO_KOGO"] = dostawca;
                dr["CEL"] = cel;
                dr["KRAJOWA_ZAGRANICZNA"] = krajZagr;
                dr["KRAJ"] = kraj;
                dr["MIASTO"] = miasto;

                dt.Rows.Add(dr);
                gridViewTrips.DataSource = dt;
                gridViewTrips.DataBind();
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["DO_KOGO"] = dostawca;
                dr["CEL"] = cel;
                dr["KRAJOWA_ZAGRANICZNA"] = krajZagr;
                dr["KRAJ"] = kraj;
                dr["MIASTO"] = miasto;

                dt.Rows.Add(dr);
                gridViewTrips.DataSource = dt;
                gridViewTrips.DataBind();
            }





        }


    }

的.aspx:

<asp:GridView runat="server" id="gridViewTrips">
            <EmptyDataTemplate>
                <asp:Table ID="TableTrips" runat="server" CssClass="table table-bordered">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>Do kogo</asp:TableHeaderCell>
                <asp:TableHeaderCell>Cel</asp:TableHeaderCell>
                <asp:TableHeaderCell>Krajowa/Zagraniczna</asp:TableHeaderCell>
                <asp:TableHeaderCell>Kraj</asp:TableHeaderCell>
                <asp:TableHeaderCell>Miasto</asp:TableHeaderCell>

            </asp:TableHeaderRow>

        </asp:Table>
            </EmptyDataTemplate>

        </asp:GridView>

编辑:现在我看到我调试我的项目时我得到了什么:&#34; if(gridViewTrips.Columns.Count == 0)&#34;插入一行后显示我没有任何列。有什么想法发生了什么?