Bonsoir,

j'ai un problème avec mon gridview, paging et sorting.

J'ai écrit un code, mais le problème il ne marche pas parfaitement c'est à dire :
quand je clique sur champ de trie pour la première fois le grid reste le même mais quand je clique une 2 ème fois le grid change selon le trie et puis il fonctionne correctement.
Mon souci est de corriger le fonctionnement au 1 er clique.

Voilà mon code :
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
//page load
if (!IsPostBack)
            {
 
 
                ChargerGrid();
 
            }
 
private DataTable GetData()
    {
 
 
            #region Requête CAML sur la liste des projets
            SPQuery objSPQuery = new SPQuery();
            objSPQuery.ViewFields =
                 @"
               <FieldRef Name='ID' />
               <FieldRef Name='NomProjet' />
               <FieldRef Name='ChefDeProjet'  />
               <FieldRef Name='DateDebut'  />
               <FieldRef Name='DateFinProjet'  />";
 
            objSPQuery.Query =
                @" <OrderBy>
                  <FieldRef Name='DateDebut' Ascending='False' />
                  </OrderBy>";
 
            objSPListItemCollection = objSPList.GetItems(objSPQuery);
            #endregion
 
 
            #region Affecter la valeur de la requête au grid
            DataTable datatable = objSPListItemCollection.GetDataTable();
            #endregion
 
 
            return datatable;
    }
 
    public void ChargerGrid()
    {
        try
        {
 
 
 
            GVProjet.DataSource = GetData();
            GVProjet.DataBind();
 
 
        }
 
        catch (Exception E)
        {
            LblErreur.Text = E.Message;
        }
 
    }
private string GridViewSortDirection
    {
 
        get { return ViewState["SortDirection"] as string ?? "ASC"; }
 
        set { ViewState["SortDirection"] = value; }
 
    }
 
 
 
    private string GridViewSortExpression
    {
 
        get { return ViewState["SortExpression"] as string ?? string.Empty; }
 
        set { ViewState["SortExpression"] = value; }
 
    }
 
 
 
    private string GetSortDirection()
    {
 
        switch (GridViewSortDirection)
        {
 
            case "ASC":
 
                GridViewSortDirection = "DESC";
 
                break;
 
 
 
            case "DESC":
 
                GridViewSortDirection = "ASC";
 
                break;
 
        }
 
 
 
        return GridViewSortDirection;
 
    }
 
 
 
 
    protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
    {
 
        if (dataTable != null)
        {
 
            DataView dataView = new DataView(dataTable);
 
            if (GridViewSortExpression != string.Empty)
            {
 
                if (isPageIndexChanging)
                {
 
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
 
                }
 
                else
                {
 
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
 
                }
 
            }
 
            return dataView;
 
        }
 
        else
        {
 
            return new DataView();
 
        }
 
    }
 
 
 
    protected void GVProjet_Sorting(object sender, GridViewSortEventArgs e)
    {
 
        GridViewSortExpression = e.SortExpression;
 
        int pageIndex = GVProjet.PageIndex;
 
        //DataTable table = (DataTable)Session["datatable"];
 
        GVProjet.DataSource = SortDataTable(GetData() as DataTable, false);
 
        GVProjet.DataBind();
 
        GVProjet.PageIndex = pageIndex;
 
    }

Finalement, j'espère que vous m'aidez à résoudre mon problème.

Merci