我在网格视图中显示数据时遇到问题我是asp.net的新学习者请帮助我。我的代码如下: 问题是:
的.aspx
<asp:GridView ID="GridView1" runat="server" class="international_news" AutoGenerateColumns="False" DataKeyNames="news_id">
<Columns>
<asp:BoundField DataField="news_id" Visible="false"/>
<asp:BoundField DataField="news_title"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ReadMore" CommandArgument='<%#Eval("news_id")%>'>
Read More
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
=============================================== =======================================
.aspx.cs
con.Open();
string sqlString = "SELECT [news_id], [news_title] from upload_news";
cmd = new SqlCommand(sqlString, con);
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
if (!this.IsPostBack)
{
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("News Title", typeof(string)),
new DataColumn("News ID", typeof(string))});
while (dr.Read())
{
if (dr[1].ToString() != "yes")
{
dt.Rows.Add(dr[0].ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
con.Close();
答案 0 :(得分:1)
改变这个:
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("News Title", typeof(string)),
new DataColumn("News ID", typeof(string))});
在
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("news_title", typeof(string)),
new DataColumn("news_id", typeof(string))});