带拖放功能的DataGridView——基于用户活动更新列表
本文关键字:用户 活动 列表 更新 于用户 功能 拖放 DataGridView | 更新日期: 2023-09-27 18:03:43
一个业余爱好者试图完成我的第一个真正的应用程序,所以提前感谢你,如果这是一个新手的问题道歉。
我有一个启用拖放功能的datagridview,其中填充了来自Team Class的数据和一个Dictionary,该Dictionary根据索引将值分配给List中的项。我还没有实现绑定,因为我还没有让它与拖放行重新排序一起工作。
teamList = new List<Team>(){
new Team("Redskins","WAS", "Washington"),
new Team("Cowboys", "DAL", "Dallas"),
new Team("Eagles", "PHI", "Philadelphia"),
new Team("Giants", "NYG", "New York"),
new Team("Packers", "GB", "Green Bay")
};
class Team
{
public string Mascot { get; private set; }
public string Key { get; private set; }
public string City { get; private set; }
}
Dictionary<int, int> powerValues = new Dictionary<int, int>(){
{1, 113},
{2, 110},
{3, 109},
{4, 108},
{5, 107}
};
private void printValues()
{ // add Team to DGV wiht PowerRank as index+1 and PowerValue based on Dictionary
int i = 0;
foreach (Team team in TeamList)
{
// add all data to a datagrid view
dataGridView1.Rows.Add(i+1, team.Mascot,powerValues[teamList.IndexOf(team) + 1]); //set the rank
i++;
}
}
一旦我到达这一点,我将尝试做两件事:
1) Update List<Team> based on drag and drop change
2) Re-assign PowerRank and PowerValue based on new index of Team
我尝试了几件事,但都无济于事——有什么正确的想法或方向吗?
我已经通过一种新方法绕过了#2,但它不是我所希望的优雅解决方案,因此有待改进。还在做#2
private void reEstablishRanking()
{
for (int i = 0; i < defaultPR.TeamList.Count; i++)
{
dataGridView1.Rows[i].Cells["PowerRank"].Value = (i + 1);
dataGridView1.Rows[i].Cells["PowerValue"].Value = defaultPR.PRScores[i + 1];
}
}