DataTable添加行和列数据?
源码:不能显示任何数据?是哪里出错了?<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx....
源码:不能显示任何数据?
是哪里出错了?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat=server>
protected void Page_Load()
{
DataTable newdtb = new DataTable();
newdtb.Columns.Add("Id", typeof(int));
newdtb.Columns.Add("ProName", typeof(string));
newdtb.Columns.Add("ProPrice", typeof(decimal));
newdtb.Columns["Id"].AutoIncrement = true;
for (int i = 1; i < 11; i++)
{
DataRow newRow = newdtb.NewRow();
newRow["ProName"] = "pro" + i.ToString();
newRow["ProPrice"] = 12.3m;
newdtb.Rows.Add(newRow);
}
GridView1.DataSource = newdtb;
GridView1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html> 展开
是哪里出错了?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat=server>
protected void Page_Load()
{
DataTable newdtb = new DataTable();
newdtb.Columns.Add("Id", typeof(int));
newdtb.Columns.Add("ProName", typeof(string));
newdtb.Columns.Add("ProPrice", typeof(decimal));
newdtb.Columns["Id"].AutoIncrement = true;
for (int i = 1; i < 11; i++)
{
DataRow newRow = newdtb.NewRow();
newRow["ProName"] = "pro" + i.ToString();
newRow["ProPrice"] = 12.3m;
newdtb.Rows.Add(newRow);
}
GridView1.DataSource = newdtb;
GridView1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html> 展开
2个回答
展开全部
protected void Page_Load()
{
DataTable newdtb = new DataTable();
newdtb.Columns.Add("Id", typeof(int));
newdtb.Columns.Add("ProName", typeof(string));
newdtb.Columns.Add("ProPrice", typeof(decimal));
newdtb.Columns["Id"].AutoIncrement = true;
for (int i = 1; i < 11; i++)
{
DataRow newRow = newdtb.NewRow();
newRow["ProName"] = "pro" + i.ToString();
newRow["ProPrice"] = 12.3m;
newdtb.Rows.Add(newRow);
}
GridView1.DataSource = newdtb;
GridView1.DataBind();
}
这部分代码没有错误。一般都用代码分离格式来做,你怎么把后台代码和前端代码都放在一个文件里啊。我用代码分离格式做的,GridView显示数据正常。
{
DataTable newdtb = new DataTable();
newdtb.Columns.Add("Id", typeof(int));
newdtb.Columns.Add("ProName", typeof(string));
newdtb.Columns.Add("ProPrice", typeof(decimal));
newdtb.Columns["Id"].AutoIncrement = true;
for (int i = 1; i < 11; i++)
{
DataRow newRow = newdtb.NewRow();
newRow["ProName"] = "pro" + i.ToString();
newRow["ProPrice"] = 12.3m;
newdtb.Rows.Add(newRow);
}
GridView1.DataSource = newdtb;
GridView1.DataBind();
}
这部分代码没有错误。一般都用代码分离格式来做,你怎么把后台代码和前端代码都放在一个文件里啊。我用代码分离格式做的,GridView显示数据正常。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询