ASP.NET GridView.Databind为空

时间:2013-11-15 05:10:34

标签: c# asp.net gridview data-binding

我在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>

1 个答案:

答案 0 :(得分:1)

您需要在标记中设置文字文本以绑定到数据源中的列名称,如下所示:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Eagle">
            <ItemTemplate>
                <asp:Literal runat="server" ID="litEagle" 
                             Text="<%# Eval('Team1') %>" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Bull">
            <ItemTemplate>
                <asp:Literal runat="server" ID="litBull" 
                             Text="<%# Eval('Team2') %>" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Snake">
            <ItemTemplate>
                <asp:Literal runat="server" ID="litSnake" 
                             Text="<%# Eval('Result') %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>