在ASP.NET GridView中如何添加行?

4

我有一个包含几个DropDownLists和TextBoxes的GridView。在保留现有GridView的同时,如何添加新行?我想使用LinkButton添加新行。我没有使用数据源控件,该GridView是通过DataTable填充的。以下是GridView:

<asp:LinkButton ID="btnAdd" runat="server" Text="Add Room" 
        onclick="btnAdd_Click"></asp:LinkButton>
    <asp:GridView ID="gvRP" runat="server" AutoGenerateColumns="false" 
        onrowdatabound="gvRP_RowDataBound" 
        onrowediting="gvRP_RowEditing">
    <Columns>
    <asp:TemplateField HeaderText="Room" ItemStyle-Width="100%">
    <ItemTemplate>
    <asp:Label runat="server" Text="Room"></asp:Label>
    <asp:DropDownList ID="ddlRoom" runat="server" AutoPostBack="True" DataTextField="Name"
        DataValueField="Id" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlRoom_SelectedIndexChanged">
        <asp:ListItem Value="-1">Select...</asp:ListItem>
    </asp:DropDownList>
    <asp:Label runat="server" AssociatedControlID="ddlRate" Text="Rate" ID="lblRate"></asp:Label><asp:DropDownList
        ID="ddlRate" runat="server" AppendDataBoundItems="true" DataTextField="Name"
        DataValueField="Id">
        <asp:ListItem Value="-1">Select...</asp:ListItem>
    </asp:DropDownList>

    <asp:Label  runat="server" Text="Adults"></asp:Label>
    <asp:TextBox ID="txtAdults" Text='<%#Bind("Adults") %>' runat="server" Width="25px"></asp:TextBox>
    <asp:Label  runat="server" Text="Children"></asp:Label>
    <asp:TextBox ID="txtChildren" Text='<%#Bind("Children") %>' runat="server"  Width="25px"></asp:TextBox>
    <asp:Label runat="server" Text="Check In"></asp:Label>
    <asp:TextBox ID="txtCheckIn" Text='<%#Bind("CheckIn") %>' runat="server" Width="75px"></asp:TextBox>
    <asp:Label  runat="server" Text="Check Out"></asp:Label>
    <asp:TextBox ID="txtCheckOut" Text='<%#Bind("CheckOut") %>' runat="server"  Width="75px"></asp:TextBox>

    <h3>Rates</h3>
    <asp:GridView ID="gvR" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="Name" HeaderText="Rate" />
    <asp:BoundField DataField="Effective" HeaderText="Effective" />
    <asp:BoundField DataField="Expire" HeaderText="Expire" />
    <asp:BoundField DataField="Amount" HeaderText="Amount" />
    <asp:BoundField DataField="Code" HeaderText="Currency" />
    </Columns>
    </asp:GridView>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
4个回答

1
通常我会尝试提供一个示例,但这个示例已经非常详细了,而且我不认为这个网址会失效。请参考this link以获取全面的示例。
以下是重要的代码。 grid
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
    <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>

代码后台

protected void ButtonAdd_Click(object sender, EventArgs e)
{
      AddNewRowToGrid()
}

private void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //Set Previous Data on Postbacks
    SetPreviousData();
}

我无法让它正常工作,所以我决定使用其他东西,但我有一个问题。 - Xaisoft

0

ItemTemplate中放置标签控件或文本框,如果您将其放在最后,则会显示在最后,例如:

<ItemTemplate>
    ....
    <asp:Label Text="foo" runat="server" />
</ItemTemplate>

或者

 <ItemTemplate>
    <asp:Label Text="foo" runat="server" />
     ....
 </ItemTemplate>

0

我决定采用这个解决方案:

DataTable dt = new DataTable();

DataColumn dcRoom = new DataColumn("Room", typeof(DropDownList));
DataColumn dcAdults = new DataColumn("Adults", typeof(string));
DataColumn dcChildren = new DataColumn("Children", typeof(string));
DataColumn dcCheckIn = new DataColumn("CheckIn", typeof(string));
DataColumn dcCheckOut = new DataColumn("CheckOut", typeof(string));

dt.Columns.AddRange(new DataColumn[] { dcRoom, dcAdults, dcChildren, dcCheckIn, dcCheckOut });

dt.Rows.Add(new object[] { new DropDownList(), "", "", "", "" });
gvRP.DataSource = dt;
gvRP.DataBind();

它是如何知道将什么放入下拉菜单(Select...)的,我并没有指定两个下拉菜单,但它仍然放置了第二个下拉菜单。


我建议您将您的解决方案作为答案发布,然后提出一个新问题。在答案中列出问题是不被赞同的。 - Chase Florell

-1
namespace gridview_row_add
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataGridViewTextBoxColumn columntype = new DataGridViewTextBoxColumn();
            columntype.HeaderText = "Type";
            columntype.Width = 80;
            dataGridView1.Columns.Add(columntype);

            DataGridViewTextBoxColumn columnparameters = new DataGridViewTextBoxColumn();
            columnparameters.HeaderText = "Parameters";
            columnparameters.Width = 320;
            dataGridView1.Columns.Add(columnparameters);

            DataGridViewTextBoxColumn columndisplay = new DataGridViewTextBoxColumn();
            columndisplay.HeaderText = "Display";
            columndisplay.Width = 150;
            dataGridView1.Columns.Add(columndisplay);

            DataGridViewTextBoxColumn enumuration = new DataGridViewTextBoxColumn();
            enumuration.HeaderText = "Format";
            enumuration.Width = 90;
            dataGridView1.Columns.Add(enumuration);

            dataGridView1.AllowUserToAddRows = false;//please add this if u don't want to add exta rows or else make it true.          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add();//here on each click the new row will be added.

            int rowcount = dataGridView1.Rows.Count;

            dataGridView1.Rows[rowcount - 1].Cells[0].Value = "data" + rowcount.ToString();
            dataGridView1.Rows[rowcount-1].Cells[1].Value = "field";
            dataGridView1.Rows[rowcount-1].Cells[2].Value = "xyzzz";
            dataGridView1.Rows[rowcount-1].Cells[3].Value = "hts";

            rowcount++;                     
        }
    }
}

这段代码对我来说运作良好。在此代码中,我在girdview中添加了四个标头,您可以根据需要更改它们。点击一个按钮,它将首先添加新行,然后在该行中填充数据...希望这对您有用。


OP正在使用ASP.NET,而你的代码是针对WinForms的。在ASP中没有DataGridView。 - The Berga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接