0

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

我想输出一些到 ASP.NET 控件,比如

     Eagle Bull Snake
Eagle  —   1:0  0:2
Bull  0:1   —   3:4  
Snake 2:9  4:3   — 

我怎样才能做到这一点?我可以大量执行 2 个内部循环以将数据提取到数据网格中吗?

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

        protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvRow = e.Row;
            if (gvRow.RowType == DataControlRowType.DataRow)
            {
               var dr = (DataRow)gvRow.DataItem;
                //var dtSource = GridView1.DataSource();
               var dtSource = sender.DataSource();
               var iRowCount = dtSource.Rows.Count;

                for (var j = 0; j < iRowCount; j++)
                {
                    var sTeam1 = dtSource.Rows(j)("Team1");

                    for (var i = 0; i < iRowCount; i++)
                    {
                        var sTeam2 = dtSource.Rows(i)("Team1");
                        var dv = dt_teams.DefaultView();
                        dv.RowFilter = string.Format("Team1 = '{0}' AND Team2 = '{1}'", sTeam1, sTeam2);
                        var dt = dv.ToTable();
                        if (dt.Rows.Count > 0)
                        {
                            ((Literal)gvRow.FindControl("lit" + sTeam2)).Text = dt.Rows(0)("Result1").ToString();
                        }
                    }
                }
            }
        }

    }
}

我将任务分为两个步骤。在第一步中,我只想测试 BindData (Gridview1>Databind)。我在浏览器中有一个列数正确的表格输出,但它是空的。当然,当 GridView1_RowDataBound 的内容被评论时,这就像那样。

4

1 回答 1

2

我现在没有调试器,所以可能会有一些错误,但我希望你能得到 ff: 代码。

private DataTable _dtQuery = null;

protected void Page_Load(object sender, EventArgs e)
{
    _dtQuery = ResultFromQuery();

    var dtUnique = dt.DefaultView.ToTable(true, "Team1"); // this will hold 3 rows which are eagle, snake, bull
    GridView1.DataSource = dtUnique;
    GridView1.DataBind;
}

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    GridViewRow gvRow = e.Row;
    if (gvRow.RowType == DataControlRowType.DataRow) {
        var dr = (DataRow)gvRow.DataItem;

        var dtSource = sender.DataSource();
        var iRowCount = dtSource.Rows.Count;

        for(var j = 0; j < iRowCount; j++) {
            var sTeam1 = dtSource.Rows(j)("Team1");

            for(var i = 0; i < iRowCount; i++) {
                var sTeam2 = dtSource.Rows(i)("Team1");

                var dv = _dtQuery.DefaultView();
                dv.RowFilter = string.Format("Team1 = '{0}' AND Team2 = '{1}'", sTeam1, sTeam2);
                var dt = dv.ToTable();

                if(dt.Rows.Count > 0) {
                    ((Literal)gvRow.FindControl("lit" + sTeam2)).Text = dt.Rows(0)("Result").ToString();
                }
            }
        }
    }
}




<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>
于 2013-11-13T04:18:14.673 回答