我在sql中有一个表
ID Team1 Team2 Result
1 Eagle Eagle —
2 Eagle Bull 1:0
3 Eagle Snake 0:2
4 Bull Eagle 0:1
5 Bull Bull —
6 Bull Snake 3:4
7 Snake Eagle 2:0
8 Snake Bull 4:3
9 Snake Snake —
这不是双重问题。这只是任务分工和一些衍生问题。 https://stackoverflow.com/posts/19944634/edit 在这个问题中,我不想像 19944634 帖子那样对数据进行排序。
我只是想要 GridView.Databind。 当我执行 BindData 函数时,我在浏览器中收到一个包含正确列数的表格输出,但它是空的。为什么?
代码隐藏:
namespace ASP_Web_Datagrid
{
public partial class GridView_FROM_SQL : System.Web.UI.Page
{
DataTable dt_teams = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
System.Data.DataTable dtUnique = dt_teams.DefaultView.ToTable(true, "name1");
GridView1.DataSource = dtUnique;
GridView1.DataBind();
Message.Text = dt_teams.Rows.Count.ToString();
}
}
public void BindData()
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
const string queryTransaction = "SELECT name1 from Team_table";
using (SqlConnection con1 = new SqlConnection(constr))
{
using (SqlCommand cmd1 = new SqlCommand(queryTransaction, con1))
{
con1.Open();
SqlDataAdapter ada;
ada = new SqlDataAdapter(cmd1);
ada.Fill(dt_teams);
cmd1.ExecuteNonQuery();
con1.Close();
}
}
}
标记:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Eagle">
<ItemTemplate>
<asp:Literal runat="server" ID="litEagle" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bull">
<ItemTemplate>
<asp:Literal runat="server" ID="litBull" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Snake">
<ItemTemplate>
<asp:Literal runat="server" ID="litSnake" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>