2012-06-11 151 views
4

我有以下的GridView有几个DropDownLists和TextBoxes。我如何向它添加新行,同时坚持现有的GridView。我想用LinkBut​​ton添加新行。我没有使用DataSource控件,并且当前通过DataTable填充了GridView。这里是GridView控件:将行添加到ASP.NET 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> 

回答

1

通常我尝试做一个例子,但是这个人是相当深入,我不“想”的网址是去任何地方。全面的例子请参考this link

下面是重要的代码。

电网

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

我不能得到这个工作,所以我决定去别的,b我有一个问题。 – Xaisoft

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

它是如何知道要放什么东西在下拉(选择... ),我没有指定两个DropDown,但它仍然放第二个DropDown。

+0

我建议你发布你的工作解决方案作为答案,然后提出一个新问题。里面列出的问题和答案是不满意的。 –

0
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中添加了四个头文件,您可以根据您的要求更改它们..一个按钮单击它将首先添加新行,然后数据被填充在该行中。 希望这可以适用于您..

0

认沽Label控件或文本框,只要你喜欢里面的ItemTemplate ,如果你把它放在最后,它会在最后显示出来,例如

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

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