C# 如何在DataGridView中添加新的空行?
我有一个DataGridView,我想添加一个包含空单元格的新行,但我不知道DataGridView中的列数(变量)。有一种简单的方法可以做到这一点吗?您可以使用以下代码轻松地做到这一点。下面的标记用于带有gridview的页面,下面的C#代码用于添加空行的按钮单击事件,而不知道列的名称或数据类型 这是经过考验的 C#单击按钮添加新空行的代码C# 如何在DataGridView中添加新的空行?,c#,datagridview,C#,Datagridview,我有一个DataGridView,我想添加一个包含空单元格的新行,但我不知道DataGridView中的列数(变量)。有一种简单的方法可以做到这一点吗?您可以使用以下代码轻松地做到这一点。下面的标记用于带有gridview的页面,下面的C#代码用于添加空行的按钮单击事件,而不知道列的名称或数据类型 这是经过考验的 C#单击按钮添加新空行的代码 protected void btn1_Click(object sender, EventArgs e) { GridViewRow row1
protected void btn1_Click(object sender, EventArgs e)
{
GridViewRow row1 = GridView1.Rows[0];
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
for (int i = 0; i < row1.Cells.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
Table parentTable = row1.Parent as Table;
parentTable.Rows.Add(row);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewSample.aspx.cs" Inherits="GridViewSample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="btn1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [CategoryName] FROM [Alphabetical list of products]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
protectedvoid btn1\u单击(对象发送方,事件参数e)
{
GridViewRow1=GridView1.Rows[0];
GridViewRow行=新的GridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);
对于(int i=0;i
带有gridview的aspx标记
protected void btn1_Click(object sender, EventArgs e)
{
GridViewRow row1 = GridView1.Rows[0];
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
for (int i = 0; i < row1.Cells.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
Table parentTable = row1.Parent as Table;
parentTable.Rows.Add(row);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewSample.aspx.cs" Inherits="GridViewSample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="btn1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [CategoryName] FROM [Alphabetical list of products]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>