我有一个已经与数据库绑定的datagridview,有一个组合框 链接到数据库。我的目标是当我更改组合框的值时,datagridview将根据更改后的组合框的值进行更改。
这是与mssql绑定的C#winform。
我的datagridview与
绑定RegistrationEvent.BibNumber,RegistrationEvent.RaceTime,User.FirstName,User.LastName, Runner.CountryCode.
表用户包含电子邮件,名字,姓氏
表格Runnercontain电子邮件,性别,国家/地区代码
表RegistrationEvent包含BibNumber,RaceTime,EventId
通过事件表链接的表Marathon和RegistrationEvent包含MarathonId,EventName,EventId
与tb Marathon绑定的组合框包含MarathonId,MarathonName,CityName,CountryCode和YearHeld。
组合框显示的会员值MarathonName包含2011年马拉松,2012年马拉松,2013年马拉松,2014年马拉松,2015年马拉松。
我希望当我从这些显示的成员中选择1个时,根据组合框更改的数据网格
这是用于将datagridview与特定的表和列绑定:
private void buttonQueryDb_Click(object sender, EventArgs e)
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT [RegistrationEvent].[BibNumber], [RegistrationEvent].[RaceTime], [User].[FirstName], [User].[LastName], [Runner].[CountryCode] " +"FROM [User], [Runner], [RegistrationEvent] WHERE[User].Email = [Runner].Email AND [RegistrationEvent].RegistrationEventId = [Runner].RunnerId AND [RegistrationEvent].RaceTime IS NOT NULL", sqlCon);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
dgv1.DataSource = dtbl;
dgv1.ReadOnly = true;
dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}
这是用于将组合框与表绑定
using (Model1 db = new Model1())
{
db.Configuration.ProxyCreationEnabled = false;
marathonevent.DataSource = db.Marathons.ToList();
marathonevent.ValueMember = "MarathonName";
marathonevent.DisplayMember = "MarathonName";
}
我用它来用组合框填充datagridview
private void marathonevent_SelectionChangeCommitted(object sender, EventArgs e)
{
Marathon obj = marathonevent.SelectedItem as Marathon;
using (Model1 db = new Model1())
{
db.Configuration.ProxyCreationEnabled = false;
var query = (from ccode in db.Runners
where ccode.CountryCode == obj.CountryCode
select new { Country = ccode.Country });
dgv1.DataSource = query.ToList();
}
它仅显示表Runner中的数据,所以我使用了它:
private void marathonevent_SelectionChangeCommitted(object sender, EventArgs e)
{
Marathon obj = marathonevent.SelectedItem as Marathon;
using (Model1 db = new Model1())
{
db.Configuration.ProxyCreationEnabled = false;
var query = (from ccode in db.Runners
from fn in db.Users
from ln in db.Users
from bib in db.RegistrationEvents
from rctime in db.RegistrationEvents
where ccode.CountryCode == obj.CountryCode
select new { Country = ccode.Country, User = fn.FirstName,ln.LastName, RegistrationEvent = bib.BibNumber, rctime.RaceTime}) ;
dgv1.DataSource = query.ToList();
dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
winform被冻结
我想在MarathonName中选择值,并且datagridview将根据组合框的值而变化,显示数据。这些数据在mssql中全部链接在一起。