我有一个这样的课程:
void AddCustomer(Customer customer)
{
// Add infor of customer to dgvCustomerInfo
int rowIndexCustomerInfo = dgvCustomerInfo.Rows.Add();
dgvCustomerInfo.Rows[rowIndexCustomerInfo].Cells[0].Value = customer.Name;
if (customer.Sex == true)
{
dgvCustomerInfo.Rows[rowIndexCustomerInfo].Cells[1].Value = "Male";
}
else
{
dgvCustomerInfo.Rows[rowIndexCustomerInfo].Cells[1].Value = "Female";
}
// Add favorite foof of customer to dgvFavouriteFoods
int rowIndexFavouriteFood = dgvFavouriteFoods.Rows.Add();
for (int i = 0; i < customer.FavouriteFoods.Count; i++)
{
if (i < dgvFavouriteFoods.ColumnCount)
{
dgvFavouriteFoods.Rows[rowIndexFavouriteFood].Cells[i].Value = customer.FavouriteFoods[i];
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Customer customer1 = new Customer()
{
Name ="Andrew",
Sex = true,
FavouriteFoods = new List<string>(){"Pizza","Hot dog","Noodle","Chicken"
}
};
Customer customer2 = new Customer()
{
Name = "Sandra",
Sex = false,
FavouriteFoods = new List<string>(){"Rice","Pork","Beef","Chicken"
}
};
this.AddCustomer(customer1);
this.AddCustomer(customer2);
}
我有2个datagridviews: dgvCustomerInfo将显示客户的姓名和性别 另外一个,dgvFavouriteFoods将展示客户最喜欢的4种食物。
These are my 2 datagridviews
我想点击dgvFavouriteFoods的任何标题,dgvFavouriteFoods将被排序,dgvCustomerInfo也将按照相应的顺序排序。
例如:
安德鲁喜欢披萨,热狗,面条,鸡肉
桑德喜欢米饭,猪肉,牛肉,鸡肉
当我点击dgvFavouriteFoods的标题时,dgvFavouriteFoods被排序,并且dgvCustomerInfo应该按照相应的顺序排序,让我知道
安德鲁喜欢披萨,热狗,面条,鸡肉
桑德喜欢米饭,猪肉,牛肉,鸡肉。
你能帮我解决一下吗?
这是我将客户添加到这两个datagridviews的代码:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
注意:不要更改Customer类。
答案 0 :(得分:1)
您需要2个班级和1个名单:
private List<Customer> _myCustomers = new List<Customer>();
public class Customer
{
/// <summary>
/// Name of customer
/// </summary>
public string Name { get; set; }
/// <summary>
/// Sex of customer
/// </summary>
public bool Sex { get; set; }
/// <summary>
/// List of favourite foods
/// </summary>
public List<FavouriteFood> FavouriteFoods { get; set; }
}
public class FavouriteFood {
public FavouriteFood() { }
public FavouriteFood(string _food) { this.Food = _food; }
public string Food { get; set; }
}
FormLoad:
private void Form1_Load(object sender, EventArgs e)
{
//dgvCustomerInfo grid Mouse Up Event:
this.dgvCustomerInfo.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgvCustomerInfo_MouseUp);
Customer customer1 = new Customer()
{
Name = "Andrew",
Sex = true,
FavouriteFoods = new List<FavouriteFood>(){
new FavouriteFood("Pizza"),
new FavouriteFood("Hot dog"),
new FavouriteFood("Noodle"),
new FavouriteFood("Chicken")
}
};
Customer customer2 = new Customer()
{
Name = "Sandra",
Sex = false,
FavouriteFoods = new List<FavouriteFood>()
{
new FavouriteFood("Rice"),
new FavouriteFood("Pork"),
new FavouriteFood("Beef"),
new FavouriteFood("Chicken")
}
};
_myCustomers.Add(customer1);
_myCustomers.Add(customer2);
dgvCustomerInfo.DataSource = _myCustomers;
}
上次:鼠标注册事件:
private void dgvCustomerInfo_MouseUp(object sender, MouseEventArgs e)
{
Customer _selected = (Customer)dgvCustomerInfo.CurrentRow.DataBoundItem;
dgvFavouriteFoods.DataSource = _selected.FavouriteFoods;
}