2

如何将 gridview 与来自两个不同表的数据绑定?我有三张桌子:Players、PlayerTeam 和 Teams。我的 gridview 有下一个数据:来自表 Players 的 PlayerID、Name、Surname...,以及来自另一个表 Teams 的 TeamID、TeamName...。Table PlayerTeam marge 通过 ID 对 Player 和 Teams 进行表格。我正在使用 tableAdapter 来解决这个问题。

当我想在 gridview 中编辑某些行时,我在表单中进行更改(更改文本框中的数据)并从下拉列表中选择团队,但我无法保存这些更改。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Rukomet
{
    public partial class IgracUI : System.Web.UI.Page
    {
        dsIgrac.tblIgracRow igrac;
        dsKlub.tblKlubRow klub;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            this.UcitajKlub();
            this.dohvatiIgraca();
            this.prikaziIgracevePodatke();

        }

        private void pokupiPodatkeSForme()
        {
            this.igrac.MaticniBroj = this.txtMaticniBroj.Text;
            this.igrac.Ime = this.txtIme.Text;
            this.igrac.Prezime = this.txtPrezime.Text;
            this.igrac.BrojDresa = this.txtBrojDresa.Text;
            this.igrac.Pogoci = Convert.ToInt32(this.txtPogoci.Text);
            this.igrac.Opomene = Convert.ToInt32(this.txtOpomene.Text);
            this.igrac.DvijeMinute = Convert.ToInt32(this.txtDvijeMinute.Text);
            this.igrac.Diskvalifikacija = Convert.ToInt32(this.txtDiskvalifikacija.Text);

            this.igrac.Lijecnicki = Convert.ToDateTime(this.txtLijecnicki.Text);

            this.klub.KlubID = Convert.ToInt32(this.ddlKlubovi.SelectedValue);

        }

        private void prikaziIgracevePodatke()
        {
            if (igrac.IgracID == -1)
                return;

            this.txtMaticniBroj.Text = igrac.MaticniBroj;
            this.txtIme.Text = igrac.Ime;
            this.txtPrezime.Text = igrac.Prezime;
            this.txtBrojDresa.Text = igrac.BrojDresa;
            this.txtPogoci.Text = igrac.Pogoci.ToString();
            this.txtOpomene.Text = igrac.Opomene.ToString();
            this.txtDvijeMinute.Text = igrac.DvijeMinute.ToString();
            this.txtDiskvalifikacija.Text = igrac.Diskvalifikacija.ToString();

            this.txtLijecnicki.Text = igrac.Lijecnicki.ToShortDateString();


            if (this.ViewState["selected"] != null)
            {
                ddlKlubovi.SelectedValue = Convert.ToString(this.ViewState["selected"]);
            }

        }

        private void dohvatiIgraca()
        {
            dsIgrac.tblIgracDataTable tblIgrac = new dsIgrac.tblIgracDataTable();

            if (Request.Params["IgracID"] == null)
            {
                this.igrac = tblIgrac.NewtblIgracRow();
                this.igrac.IgracID = -1;
            }
            else
            {
                dsIgracTableAdapters.tblIgracTableAdapter adapterIgrac = new dsIgracTableAdapters.tblIgracTableAdapter();

                int id = Convert.ToInt32(this.Request.Params["IgracID"]);

                adapterIgrac.FillByIgracID(tblIgrac, id);

                this.igrac = (dsIgrac.tblIgracRow)tblIgrac.Rows[0];
            }
        }


        void UcitajKlub()
        {
            dsIgrac.tblKlubDataTable Klub = new dsIgrac.tblKlubDataTable();

            dsIgracTableAdapters.tblKlubTableAdapter adapterKlub = new dsIgracTableAdapters.tblKlubTableAdapter();

            adapterKlub.Fill(Klub);
            this.ddlKlubovi.DataTextField = "ImeKluba";
            this.ddlKlubovi.DataValueField = "KlubID";

            this.ddlKlubovi.DataSource = Klub;
            this.ddlKlubovi.DataBind();
        }

        protected void btnOdustani_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("GlavnaForma.aspx");
        }


        protected void btnSpremi_Click(object sender, EventArgs e)
        {
            this.dohvatiIgraca();
            this.pokupiPodatkeSForme();

            dsIgracTableAdapters.tblIgracTableAdapter adapterIgrac = new dsIgracTableAdapters.tblIgracTableAdapter();


            if (this.igrac.IgracID == -1)
            {
                adapterIgrac.Insert(
                    this.igrac.MaticniBroj,
                    this.igrac.Ime,
                    this.igrac.Prezime,
                    this.igrac.BrojDresa,
                    this.igrac.Pogoci,
                    this.igrac.Opomene,
                    this.igrac.DvijeMinute,
                    this.igrac.Diskvalifikacija,
                    this.igrac.Lijecnicki);
                    this.klub.KlubID);

            }
            else
            {
                adapterIgrac.Update(this.igrac);
            }
            this.Response.Redirect("GlavnaForma.aspx");
        }

    }
}
4

2 回答 2

0

您可以做的是使用联接或组子句并将结果绑定到所述数据网格视图。

请参考以下链接:

关联

链接2

于 2013-05-08T20:46:30.787 回答
0

您应该发布代码以获得更好的答案,但我想我理解您的要求。为此,我在 HTML 中使用 C# WebGrid 查询三个表,如下所示:

@{
var db=Database.Open("Contracts");
var selectCommand = "SELECT Requests.*, Contractors.Agency+'/'"+
                    "+Contractors.FirstName+' '+Contractors.LastName as Contractor,Programs.Division "+
                    "FROM Requests, Contractors, Programs "+
                    "WHERE ContractNumber is not null "+
                    "and ContractNumber <> '' "+
                    "and ScannedContractDate is not null "+
                    "and AmendmentCount = 0 "+
                    "and Contractors.id = Requests.ContractorId "+
                    "and Requests.programId = Programs.Id "; 
}
<h2>Page Title</h2>
<div>@grid.GetHtml( tableStyle: "grid",
            headerStyle: "head",
            alternatingRowStyle: "alt",
            columns: grid.Columns(
            grid.Column("Division"),
            grid.Column("ContractNumber", header:"Contract Number"),
            grid.Column("Contractor"),
            grid.Column("StartDate", header:"Start Date", canSort: true),
            grid.Column("EndDate", header:"End Date", canSort: true),
            grid.Column("Amount"),
            grid.Column("ScannedContractDate", header:"Contract Date", canSort: true),
            grid.Column("MaxAmend", header:"Amendments")
           ) ) 
        </div> 

我希望这能回答你的问题!

于 2013-05-08T20:22:08.747 回答