C# 如何在ASP.NET中绑定网格?
我不能绑定我的网格。我不知道我做错了什么,当我运行程序时,网格显示为空 这是我的密码:C# 如何在ASP.NET中绑定网格?,c#,asp.net,C#,Asp.net,我不能绑定我的网格。我不知道我做错了什么,当我运行程序时,网格显示为空 这是我的密码: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) this.BindGrid(this.GridView1); } private void BindGrid(GridView grid) { SqlCommand cmd =
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
this.BindGrid(this.GridView1);
}
private void BindGrid(GridView grid)
{
SqlCommand cmd = new SqlCommand("Select * from Person", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
grid.DataSource = dt;
grid.DataBind();
}
<body>
<form id="form1" runat="server">
<div style="margin-left: 240px">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="856px" AutoGenerateColumns = "false"
ShowFooter = "true" ShowHeader="true" BorderStyle="Groove" CaptionAlign="Top"
HorizontalAlign="Center" onrowdatabound="GridView1_RowDataBound" >
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField HeaderText="ID" />
<asp:BoundField HeaderText="First Name" />
<asp:BoundField HeaderText="Last Name" />
<asp:BoundField HeaderText="Home Phone #" />
<asp:BoundField HeaderText="Cell #" />
<asp:BoundField HeaderText="Email Address" />
<asp:BoundField HeaderText="NIC #" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
/>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
</form>
</body>
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
this.BindGrid(this.GridView1);
}
专用void BindGrid(GridView网格)
{
SqlCommand cmd=新的SqlCommand(“选择*来自个人”,cn);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
da.填充(dt);
grid.DataSource=dt;
grid.DataBind();
}
试试这个:
private void BindGrid(GridView grid)
{
using(SqlConnection cn = new SqlConnection("Some Connection string"))
using(SqlCommand cmd = new SqlCommand("Select * from Person", cn))
{
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
grid.DataSource = dt;
grid.DataBind();
}
}
}
试试这个:
private void BindGrid(GridView grid)
{
using(SqlConnection cn = new SqlConnection("Some Connection string"))
using(SqlCommand cmd = new SqlCommand("Select * from Person", cn))
{
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
grid.DataSource = dt;
grid.DataBind();
}
}
}
看起来您没有连接到绑定字段中的数据绑定示例:
<asp:boundfield datafield="ID" headertext="ID"/>
看起来您没有连接到绑定字段中的数据绑定示例:
<asp:boundfield datafield="ID" headertext="ID"/>
或者如果不想使用绑定字段,则删除以下内容
AutoGenerateColumns=“false”或者如果不想使用绑定字段,则删除以下内容
AutoGenerateColumns=“false”问题出在哪里?你有错误吗?屏幕是否空白?您的页面加载事件是否已连接?。。是否执行了任何代码?请提供必要的错误详细信息或您的观察结果,对问题进行更详细的描述。我没有得到任何错误@mellamokb。。网格显示为空。。数据库中有一些数据是的@昆廷。。代码也会执行。。正如我之前说过的。。代码中没有任何错误。。但是这个看起来是空的..有什么问题吗?你有错误吗?屏幕是否空白?您的页面加载事件是否已连接?。。是否执行了任何代码?请提供必要的错误详细信息或您的观察结果,对问题进行更详细的描述。我没有得到任何错误@mellamokb。。网格显示为空。。数据库中有一些数据是的@昆廷。。代码也会执行。。正如我之前说过的。。代码中没有任何错误。。但是这个房间看起来是空的。。。对不起,伙计!这也在做同样的事情。。我在网格中看不到我的任何数据!:)。对不起,伙计!这也在做同样的事情。。我在网格中看不到我的任何数据!:)当我添加datafield=“ID”时,ID仅出现在网格中,而不是数据中当我添加datafield=“ID”时,ID只会出现在网格中,而不会出现在数据中这是一个建议。应该是评论部分的一部分。这是一个建议。应该是评论部分的一部分。