ASP.NET Datagrid从数据库C#中检索选择的项目,并使它们像表一样排序

时间:2022-02-27 11:18:42

I have a table in sql

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

I wanna output in some to ASP.NET control like

我想输出一些像ASP.NET控件一样

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

How can i do that? Can I do 2 inner loops in massive to fetch data into datagrid?

我怎样才能做到这一点?我可以做大量的2个内部循环来获取数据到datagrid吗?

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

    }
}

I divide Task in 2 steps. In first step I wanna just test BindData (Gridview1>Databind). I have a table output in browser with right number of columns but it is empty. This works like that when contetnt of GridView1_RowDataBound is commented of course.

我分两步完成任务。在第一步中,我想测试BindData(Gridview1> Databind)。我在浏览器中有一个表输出,列数正确,但它是空的。当有人对GridView1_RowDataBound的评论进行评论时,就像这样。

1 个解决方案

#1


2  

i dont have a debugger with me right now so there may be some error but i hope you get the ff: code.

我现在没有调试器,所以可能有一些错误,但我希望你得到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>

#1


2  

i dont have a debugger with me right now so there may be some error but i hope you get the ff: code.

我现在没有调试器,所以可能有一些错误,但我希望你得到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>