void Main_UpdateMapList() { Level[] loaded = LevelInfo.Loaded.Items; string selected = GetSelected(main_Maps); // Always new data source, avoids "-1 does not have a value" when clicking a row LevelCollection lc = new LevelCollection(); foreach (Level lvl in loaded) { lc.Add(lvl); } main_Maps.DataSource = lc; Reselect(main_Maps, selected); main_Maps.Refresh(); }
public void UpdateMapList() { if (InvokeRequired) { Invoke(new VoidDelegate(UpdateMapList)); return; } if (main_Maps.DataSource == null) { main_Maps.DataSource = lc; } // Try to keep the same selection on update List <string> selected = null; if (lc.Count > 0 && main_Maps.SelectedRows.Count > 0) { selected = new List <string>(); foreach (DataGridViewRow row in main_Maps.SelectedRows) { string lvlName = (string)row.Cells[0].Value; selected.Add(lvlName); } } // Update the data source and control //dgvPlayers.SuspendLayout(); lc.Clear(); string selectedLvl = null; if (map_lbLoaded.SelectedItem != null) { selectedLvl = map_lbLoaded.SelectedItem.ToString(); } map_lbLoaded.Items.Clear(); //lc = new LevelCollection(new LevelListView()); Level[] loaded = LevelInfo.Loaded.Items; foreach (Level lvl in loaded) { lc.Add(lvl); map_lbLoaded.Items.Add(lvl.name); } if (selectedLvl != null) { int index = map_lbLoaded.Items.IndexOf(selectedLvl); map_lbLoaded.SelectedIndex = index; } else { map_lbLoaded.SelectedIndex = -1; } UpdateSelectedMap(null, null); //dgvPlayers.Invalidate(); main_Maps.DataSource = null; main_Maps.DataSource = lc; // Reselect map if (selected != null) { foreach (DataGridViewRow row in main_Maps.Rows) { string lvlName = (string)row.Cells[0].Value; if (selected.Contains(lvlName)) { row.Selected = true; } } } main_Maps.Refresh(); //dgvPlayers.ResumeLayout(); // Update the data source and control //dgvPlayers.SuspendLayout(); }