Bonjour,

J'utilise un datagridview lié à un dataset via un dataview comme défini ci-dessous pour afficher un listing "dynamique" :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
internal DataSet dsList = new DataSet();
internal DataView dvSample = new DataView();
 
dsList.Tables.Add("Listing");
ColIndex.ColumnName = "Index";
dsList.Tables["Listing"].Columns.Add(ColIndex);      
 
dvSample = dsList.Tables["Listing"].DefaultView;
DGVList.DataSource = dvSample;

La colonne "Index" contient l'index du tableau et sert à le trier dans un ordre précis.
J'aimerais pouvoir déplacer une ligne par "insertion" en cliquant sur un bouton, mais j'ai l'impression que le dataset/datagrid se trie automatiquement et mes lignes se mélangent avant la fin de l'algorithme de tri. J'ai donc une colonne "Index" cohérente (0,1,2,...) mais les autres colonnes sont mélangées.

Voici une partie du code utilisé:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
string strTempIndex = string.Empty;
 
if (SaveRow != e.RowIndex)// && SaveRow!=9999)
{
   if (BTColReIns.Text == "-")
   {
       SaveRow = e.RowIndex;
       SaveIndex = DGVList.Rows[e.RowIndex].Cells[CaseNrIndex].Value.ToString();
       BTColReIns.Text = "+";
 
   }
   else
   {
       if (
         MessageBox.Show("Voulez vous insérer l'article " + art[SaveRow].Name + " avant l'article " + art[e.RowIndex].Name + " ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes
         )
       {
 
           if (SaveRow > e.RowIndex)
           {
 
               strTempIndex = dsList.Tables["Listing"].Rows[e.RowIndex]["Index"].ToString();               
               int i = e.RowIndex;          
               for (; i < SaveRow; i++)
               {
                  dsList.Tables["Listing"].Rows[i]["Index"] = dsList.Tables["Listing"].Rows[i + 1]["Index"].ToString();
               }                               
 
               dsList.Tables["Listing"].Rows[i]["Index"] = strTempIndex;                             
               DGVList.Sort(DGVList.Columns["Index"], ListSortDirection.Ascending);
               FnSaveDataFull();
           }
           else
           {
               strTempIndex = DGVList.Rows[e.RowIndex - 1].Cells["Index"].Value.ToString();
               for (int i = e.RowIndex - 1; i > SaveRow ; i--)
               {
                   DGVList.Rows[i].Cells["Index"].Value = DGVList.Rows[i - 1].Cells["Index"].Value;
               }
               DGVList.Rows[SaveRow].Cells["Index"].Value = strTempIndex;
 
               DGVList.Sort(DGVList.Columns["Index"], ListSortDirection.Ascending);
               FnSaveDataFull();
           }
       }
       BTColReIns.Text = "-";
       SaveRow = 9999;
       SaveIndex = "9999";
   }
 
}
else
{
   SaveRow = 9999;
   SaveIndex = "9999";
   BTColReIns.Text = "-";
}
Pourriez-vous me conseiller dans la manière de résoudre ce problème ?

Merci.