嗨朋友们。至于我之前关于没有数据源,数据绑定,数据集或数据表的gridview插入的帖子,我使用了这个编码仍然是一个错误:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Grid.aspx.cs" Inherits="Grid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 97px;
}
</style>
</head>
<body>
<form id="form1" runat="server" autocomplete="on">
<div>
<table style="width: 100%;">
<tr>
<td class="style1">
<asp:Label ID="empname" runat="server" Text="Emp Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblDesignation" runat="server" Text="Designation"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" Style="height: 26px" />
</td>
<td>
</td>
</tr>
</table>
<table style="width: 100%;">
<tr>
<td>
</td>
<td>
<div>
<asp:GridView ID="grvname" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Emp Name"></asp:TemplateField>
<asp:TemplateField HeaderText="Designation"></asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Grid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnadd_Click(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
//Add the two Columns
row.Cells.AddRange(CreateCells());
//get a reference to the table that holds this row
//Table tbl = (e.Row.Parent as Table);
Table tbl = (grvname.Rows[grvname.Rows.Count + 1].Parent as Table);------->getting error here
//Add the row at the end of the list, but before the footer.
tbl.Rows.AddAt(grvname.Rows.Count + 1, row);
//Add the row at the end of the list, but before the footer.
//tbl.Rows.AddAt(grvname.Rows.Count + 1, row);
}
private TableCell[] CreateCells()
{
TableCell[] cells = new TableCell[2];
TableCell cell;
Label lbl;
//The order item column
cell = new TableCell();
lbl = new Label();
lbl.Text = txtname.Text;
cell.Controls.Add(lbl);
cells[0] = cell;
//The price column
cell = new TableCell();
lbl = new Label();
lbl.Font.Bold = true;
lbl.Text = txtDesignation.Text;
cell.HorizontalAlign = HorizontalAlign.Right;
cell.Controls.Add(lbl);
cells[1] = cell;
return cells;
}
}
答案 0 :(得分:0)
我已经使用了数据表。首先将数据插入表中。并将其分配给gridview。
http://www.aspnettutorials.com/tutorials/controls/data-table-csharp.aspx