Bonjour,

Je travaille actuellement sur un gridview en code behind comportant à la fois des colonnes boundfield ET TemplateField.

J'ai un gros soucis lors de l'update. Il me met bien à jour le 1° et le dernier champ de données (1ère et dernière colonne du gridview après la commabdfield), mais aucun de ceux qui se trouvent entre. Je ne vois vraiment pas d'où peut venir le problème, après 1 jour et demi de recherches....

Je bosse sous VS2005, avec le framework 2.0 et il s'agit du code behind d'une page ASPX.

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
 
 
Partial Class pouet
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Call init_GV()
    End Sub
 
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        With SqlDataSource1
            Dim SSoldeModif, SGAR, SRecup, sprov As String
            SGAR = CType((GridView1.Rows(e.RowIndex).Cells(1).Controls(0)), TextBox).Text
            SSoldeModif = CType((GridView1.Rows(e.RowIndex).Cells(2).Controls(0)), TextBox).Text
            SRecup = CType((GridView1.Rows(e.RowIndex).Cells(3).Controls(0)), TextBox).Text
            sprov = CType((GridView1.Rows(e.RowIndex).Cells(4).Controls(0)), TextBox).Text
            .UpdateParameters.Add("GAR_SURF", SGAR)
            .UpdateParameters.Add("SOLDE_MODIF", SSoldeModif)
            .UpdateParameters.Add("RECUP_ESTIMEE", SSoldeModif)
            .UpdateParameters.Add("provision", sprov)
        End With
    End Sub
 
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
 
        ' Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
 
    End Sub
 
 
    Protected Sub init_GV()
 
 
        Dim colmodif As New CommandField
        Dim colSoldeModif, colRecup, colprov As New BoundField
        Dim colGarSurf As New TemplateField
 
        With SqlDataSource1
            .ConnectionString = ConfigurationManager.AppSettings("CnxFormatString") & "database=" & ConfigurationManager.AppSettings("BaseAppli") & ";"
            .SelectCommand = "SELECT GAR_SURF, NUCOI, COETB, NUMDEMANDE, SOLDE_MODIF, RECUP_ESTIMEE, provision FROM T_COMPTES where numdemande='20090002' and coetb='009'"
            .UpdateCommand = "UPDATE T_COMPTES SET GAR_SURF=@GAR_SURF, SOLDE_MODIF=@SOLDE_MODIF, RECUP_ESTIMEE=@RECUP_ESTIMEE, provision=@provision WHERE NUCOI=@NUCOI AND COETB=@COETB and numdemande='20090002'"
        End With
 
        GridView1.Columns.Clear()
 
        With colmodif
            .ShowEditButton = True
        End With
 
        With colGarSurf
            .ItemTemplate = New DynamicLblTemplate
            .EditItemTemplate = New DynamicTBTemplate
        End With
 
        With colSoldeModif
            .DataField = "SOLDE_MODIF"
        End With
 
        With colRecup
            .DataField = "RECUP_ESTIMEE"
        End With
 
        With colprov
            .DataField = "provision"
        End With
 
        With GridView1
            .BackColor = Color.FromName("White")
            .BorderColor = Color.FromArgb(153, 153, 153)
            .BorderStyle = 0
            .BorderWidth = New Unit(1, UnitType.Pixel)
            .CellPadding = 3
            .Font.Name = "Arial"
            .Font.Size = New FontUnit(10, UnitType.Pixel)
            .ShowFooter = True
            .FooterStyle.Font.Bold = True
            .FooterStyle.Font.Size = New FontUnit(12, UnitType.Pixel)
            .GridLines = 2
            'Comportement et lien aux données
            .AllowSorting = True
            .AllowPaging = True
            .PageSize = 100
            .PagerSettings.PageButtonCount = 15
            .PagerSettings.Position = PagerPosition.TopAndBottom
            'style des parties annexes (entête, pied,...)
            .FooterStyle.BackColor = Color.FromArgb(204, 204, 204)
            .FooterStyle.ForeColor = Color.FromName("Black")
            .RowStyle.BackColor = Color.FromArgb(238, 238, 238)
            .RowStyle.ForeColor = Color.FromName("Black")
            .PagerStyle.BackColor = Color.FromArgb(135, 206, 235)
            .PagerStyle.ForeColor = Color.FromName("Black")
            .PagerStyle.HorizontalAlign = HorizontalAlign.Center
            .SelectedRowStyle.BackColor = Color.FromArgb(0, 138, 140)
            .SelectedRowStyle.Font.Bold = "True"
            .SelectedRowStyle.ForeColor = Color.FromName("White")
            .HeaderStyle.BackColor = Color.FromArgb(135, 206, 235)
            .HeaderStyle.Font.Bold = "True"
            .HeaderStyle.ForeColor = Color.FromName("White")
            .AlternatingRowStyle.BackColor = Color.FromName("Gainsboro")
            .PagerSettings.Mode = 3
            .DataSourceID = "SqlDataSource1"
            .DataKeyNames = New String() {"NUCOI", "COETB", "numdemande"}
            .AutoGenerateColumns = "false"
            .Columns.Add(colmodif)
            .Columns.Add(colGarSurf)
            .Columns.Add(colSoldeModif)
            .Columns.Add(colprov)
            .Columns.Add(colRecup)
            .DataBind()
        End With
    End Sub
End Class
Voici le template label :

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
 
Imports Microsoft.VisualBasic
 
 
Public Class DynamicLblTemplate
 
    Implements ITemplate
 
    Public Overridable Overloads Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        'container: dataControlFieldCell, cellule en cours
        'Création d'un nouveau label
        Dim oLabel As Label = New Label()
 
        'Surcharge de la methode databinding du label, pointe sur BindLabel de la classe courante
        AddHandler oLabel.DataBinding, AddressOf BindLabel
        'Ajout du label à la collection de controles de la cellule
        container.Controls.Add(oLabel)
    End Sub
 
 
    Public Sub BindLabel(ByVal sender As Object, ByVal e As EventArgs)
        'Fonction appellée à la suite d'InstantiateIn, sender:oLabel
        'Redefinition de olabel, recuperation de celui de la fonction appelante
        Dim oLabel As Label = CType(sender, Label)
        'Definition de la gridViewRow contenant oLabel (le namingContainer d'un objet dans une cellule est la row correspondante)
        Dim container As GridViewRow = CType(oLabel.NamingContainer, GridViewRow)
        If Not IsDBNull(container.DataItem("GAR_SURF")) Then
            oLabel.Text = container.DataItem("GAR_SURF")
        End If
        'Definition du comportement du label
    End Sub
End Class
et le template textbox pour l'édition :

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
Imports Microsoft.VisualBasic
 
 
Public Class DynamicTBTemplate
 
    Implements ITemplate
 
    Public Overridable Overloads Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        'container: dataControlFieldCell, cellule en cours
        'Création d'un nouveau label
        Dim oTextBox As TextBox = New TextBox()
 
        'Surcharge de la methode databinding du label, pointe sur BindLabel de la classe courante
        AddHandler oTextBox.DataBinding, AddressOf BindLabel
        'Ajout du label à la collection de controles de la cellule
        container.Controls.Add(oTextBox)
    End Sub
 
 
    Public Sub BindLabel(ByVal sender As Object, ByVal e As EventArgs)
        'Fonction appellée à la suite d'InstantiateIn, sender:oLabel
        'Redefinition de olabel, recuperation de celui de la fonction appelante
        Dim oTextBox As TextBox = CType(sender, TextBox)
        'Definition de la gridViewRow contenant oLabel (le namingContainer d'un objet dans une cellule est la row correspondante)
        Dim container As GridViewRow = CType(oTextBox.NamingContainer, GridViewRow)
        'Definition du comportement du label
        If Not IsDBNull(container.DataItem("GAR_SURF")) Then
            oTextBox.Text = container.DataItem("GAR_SURF")
        End If
        oTextBox.TextMode = TextBoxMode.MultiLine
        oTextBox.ID = "test"
    End Sub
End Class
Merci par avance. Je galère vraiment et ne sais plus quoi tenter...