0

I have a table in 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   —

This is not doubling question . This is just division of task and some derivative quesion. https://stackoverflow.com/posts/19944634/edit In this question I do not wanna sorted data like in 19944634 post.

I just wanna GridView.Databind. When I perfom BindData function I receive a table output in browser with right number of columns butbut it is empty. Why?

Code-behind:

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(); 
                }
            }
        }    

Markup:

<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 Answer 1

1

You need to set the literal text in the markup to bind to the column name in your data source, like this:

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Karl Anderson. I would test it later.
When I do <asp:Literal runat="server" ID="litSnake" Text="<%#Eval('Team1').ToString()%>"/> there is Error 1 Too many characters in character literal error. I add ToString. Because there would be another error
And Warning 1 Validation (ASP.Net): If this attribute value is enclosed in quotation marks, the quotation marks must match.
I understood "too many character" and I try bind not dt_unique with 5 rows but dt_teams with 25. This also not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.