C# 将行添加到 ASP.NET GridView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10982744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Row to ASP.NET GridView?
提问by Xaisoft
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:
我有以下 GridView,它有几个 DropDownLists 和 TextBoxes。如何在保留现有 GridView 的同时向其添加新行。我想用 LinkButton 添加新行。我没有使用 DataSource 控件,GridView 目前是通过 DataTable 填充的。这是网格视图:
<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>
回答by Chase Florell
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 linkfor 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();
}
回答by Xaisoft
I decided to go with this solution:
我决定采用这个解决方案:
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();
How does it know What to put in the DropDown (Select...) and I haven't specified two DropDowns, yet it still puts the second DropDown.
它怎么知道在 DropDown (Select...) 中放什么,我还没有指定两个 DropDown,但它仍然放置了第二个 DropDown。
回答by sanjeev
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++;
}
}
}
this code works fine for me. in this code i added four headers in girdview, you can change them as per your requirement..one button click it will add new row first then data got filled in that row.. hope this will work for you..
这段代码对我来说很好用。在这段代码中,我在 girdview 中添加了四个标题,您可以根据您的要求更改它们..一个按钮单击它将首先添加新行然后数据填充该行..希望这对您有用..
回答by Emad Mokhtar
Put Label control or Textbox as you like inside ItemTemplateif you put it at last it'll be displayed at last, for example
将Label控件或Textbox随意放入ItemTemplate中,如果最后放了它最后会显示出来,例如
<ItemTemplate>
....
<asp:Label Text="foo" runat="server" />
</ItemTemplate>
or
或者
<ItemTemplate>
<asp:Label Text="foo" runat="server" />
....
</ItemTemplate>

