如何将gridview与来自两个不同表的数据绑定?我有三张桌子:玩家,玩家团队和团队。我的gridview有下一个数据:PlayerID,Name,Surname,...来自表玩家,TeamID,TeamName ......来自另一个表团队。 Table Player团队表格玩家和团队通过ID。我使用tableAdapter来解决这个问题。
当我想编辑gridview中的某些行时,我会在表单中进行更改(在文本框中更改数据)并从下拉列表中选择Team,但我无法保存此更改。
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");
}
}
}
答案 0 :(得分: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>
我希望能回答你的问题!
答案 1 :(得分:0)