添加行ASP.NET GridView的? [英] Add Row to ASP.NET GridView?

查看:86
本文介绍了添加行ASP.NET GridView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的GridView其中有一对夫妇DropDownLists和文本框。我如何添加一个新行吧,在坚持现有的GridView。我想用的LinkBut​​ton添加新行。我不使用数据源控件和GridView控件是通过一个DataTable当前填充。这里是GridView的:

 < ASP:LinkBut​​ton的ID =btnAdd=服务器文本=添加室
        的onclick =btnAdd_Click>< / ASP:LinkBut​​ton的>
    < ASP:GridView控件ID =GVRP=服务器的AutoGenerateColumns =假
        onrowdatabound =gvRP_RowDataBound
        onrowediting =gvRP_RowEditing>
    <柱体和GT;
    < ASP:的TemplateField的HeaderText =房间ItemStyle-WIDTH =100%>
    <&ItemTemplate中GT;
    < ASP:标签=服务器文本=房间>< / ASP:标签>
    < ASP:DropDownList的ID =ddlRoom=服务器的AutoPostBack =真DataTextField =姓名
        DataValueField =IDAppendDataBoundItems =真OnSelectedIndexChanged =ddlRoom_SelectedIndexChanged>
        < ASP:ListItem的值= - 1>选择...< / ASP:ListItem的>
    < / ASP:DropDownList的>
    < ASP:标签=服务器AssociatedControlID =ddlRate文本=率ID =lblRate>< / ASP:标签>< ASP:DropDownList的
        ID =ddlRate=服务器AppendDataBoundItems =真DataTextField =姓名
        DataValueField =ID>
        < ASP:ListItem的值= - 1>选择...< / ASP:ListItem的>
    < / ASP:DropDownList的>    < ASP:标签=服务器文本=成人>< / ASP:标签>
    < ASP:文本框ID =txtAdults文本='<%#绑定(成人)%>' =服务器WIDTH =25像素>< / ASP:文本框>
    < ASP:标签=服务器文本=儿童>< / ASP:标签>
    < ASP:文本框ID =txtChildren文本='<%#绑定(孩子)%>' =服务器WIDTH =25像素>< / ASP:文本框>
    < ASP:标签=服务器文本=入住>< / ASP:标签>
    < ASP:文本框ID =txtCheckIn文本='<%#绑定(检入)%>' =服务器WIDTH =960x75像素>< / ASP:文本框>
    < ASP:标签=服务器文本=退房及GT;< / ASP:标签>
    < ASP:文本框ID =txtCheckOut文本​​='<%#绑定(结帐)%GT;' =服务器WIDTH =960x75像素>< / ASP:文本框>    < H3>价格和LT; / H3 GT&;
    < ASP:GridView控件ID =GVR=服务器的AutoGenerateColumns =false的>
    <柱体和GT;
    < ASP:BoundField的数据字段=名称的HeaderText =率/>
    < ASP:BoundField的数据字段=有效的HeaderText =有效/>
    < ASP:BoundField的数据字段=过期的HeaderText =过期/>
    < ASP:BoundField的数据字段=金额的HeaderText =金额/>
    < ASP:BoundField的数据字段=code的HeaderText =货币/>
    < /专栏>
    < / ASP:GridView的>
    < / ItemTemplate中>
    < / ASP:的TemplateField>
    < /专栏>
    < / ASP:GridView的>


解决方案

通常我尝试做一个例子,但是这一次是相当彻底的,我不想的网址是去任何地方。请参考<一个href=\"http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx\"相对=nofollow>的COM prehensive例如此链接。

下面是最重要code。

电网

 &LT; FooterStyle Horizo​​ntalAlign =右/&GT;
&LT; FooterTemplate&GT;
    &LT; ASP:按钮的ID =ButtonAdd=服务器文本=添加新行/&GT;
&LT; / FooterTemplate&GT;

后面的 code

 保护无效ButtonAdd_Click(对象发件人,EventArgs的发送)
{
      AddNewRowToGrid()
}私人无效AddNewRowToGrid()
{
    INT的rowIndex = 0;    如果(的ViewState [CurrentTable]!= NULL)
    {
        数据表dtCurrentTable =(数据表)的ViewState [CurrentTable];
        DataRow的drCurrentRow = NULL;
        如果(dtCurrentTable.Rows.Count大于0)
        {
            的for(int i = 1; I&LT; = dtCurrentTable.Rows.Count;我++)
            {
                //提取文本框值
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            的ViewState [CurrentTable] = dtCurrentTable;            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
    }
    其他
    {
        的Response.Write(ViewState中为空);
    }    上回发//设置previous数据
    设置previousData();
}

I have the following GridView which has a couple DropDownLists and TextBoxes. How can I add a new row to it, while persisting the existing GridView. I would like to Add the New row with the LinkButton. I am not using DataSource Controls and the GridView is currently populated via a DataTable. Here is the 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>

解决方案

Usually I try and do an example, but this one is quite thorough, and I don't "think" the url is going anywhere. Please refer to this link for a comprehensive example.

Here's the important code.

grid

<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
    <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>

code behind

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();
}

这篇关于添加行ASP.NET GridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆