如何将 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");
}
}
}