Help !!!! Probleme avec les colonnes d'un datagrid VB.net

Résolu
piervs Messages postés 26 Date d'inscription vendredi 18 avril 2003 Statut Membre Dernière intervention 4 mai 2005 - 22 avril 2005 à 12:20
piervs Messages postés 26 Date d'inscription vendredi 18 avril 2003 Statut Membre Dernière intervention 4 mai 2005 - 25 avril 2005 à 14:01
Bonour à tous et merci d'avance !

J'ecris car j'ai un problème de taille avec les propriétés des colonnes d'un datagrid en vb.net (je precise que je debute en VB.net ).
Voilà mon problème :
Je rempli une bête datagrid avec un bête dataset, jusque là tout se passe bien, j'ai les champs et colonnes selectionnés par ma requete qui s'affichent sans aucun soucis. Là où ca se complique c'est quand j'essaye de modifier les colonnes s'affichant (modifier la taille, le titre de celles-ci ou en masquer certaines), là je n'y arrive pas du tout.
Je sais, je sais, vous allez me dire faut utiliser les tablestyle et tout ca. Mais j'ai deja essayé et cet enfo*** de logiciel me mets une exception disant "index outofrange" comme si j'essayais de travailler sur une colonne n'existant pas (genre la colonne 500 ou je sais pas quoi) alors que j'essaye de travailler sur la colonne 1 !!!!!
Voilà si quelqu'un se sent l'ame charitable et peut m'aider, je serais bein heureux.
Merci encore

3 réponses

cs_labout Messages postés 1356 Date d'inscription samedi 8 décembre 2001 Statut Membre Dernière intervention 23 octobre 2006 8
22 avril 2005 à 22:28
Voici un exemple de code avec une
base nommée Immo
table Immo
Avce présentation soignée

L'ecran présente un datagrid 6 colonnes, 2 boutons
Button1 base Access
Button2 Sql Server
Ignore les autres pour le moment
Tout est dans le code, rien dans les prop du datagrid
**************

Imports System.Data.OleDb


Public
Class Form1



Inherits System.Windows.Forms.Form



Private GridTableStyle
As DataGridTableStyle



Private oConnect
As OleDbConnection =
New OleDbConnection


#
Region " Code généré par le Concepteur Windows Form "



Public
Sub
New()



MyBase.New()



'Cet appel est requis par le Concepteur Windows Form.


InitializeComponent()



'Ajoutez une initialisation quelconque après l'appel InitializeComponent()



End
Sub



'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.



Protected
Overloads
Overrides
Sub Dispose(
ByVal disposing
As
Boolean)



If disposing
Then



If
Not (components
Is
Nothing)
Then


components.Dispose()



End
If



End
If



MyBase.Dispose(disposing)



End
Sub



'Requis par le Concepteur Windows Form



Private components
As System.ComponentModel.IContainer



'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form



'Elle peut être modifiée en utilisant le Concepteur Windows Form.



'Ne la modifiez pas en utilisant l'éditeur de code.



Friend
WithEvents DataGrid1
As System.Windows.Forms.DataGrid



Friend
WithEvents Button1
As System.Windows.Forms.Button



Friend
WithEvents Button2
As System.Windows.Forms.Button



Friend
WithEvents TextBox1
As System.Windows.Forms.TextBox



Friend
WithEvents Label1
As System.Windows.Forms.Label



Friend
WithEvents Button3
As System.Windows.Forms.Button



Friend
WithEvents Button4
As System.Windows.Forms.Button


<System.Diagnostics.DebuggerStepThrough()>
Private
Sub InitializeComponent()



Me.DataGrid1 =
New System.Windows.Forms.DataGrid



Me.Button1 =
New System.Windows.Forms.Button



Me.Button2 =
New System.Windows.Forms.Button



Me.TextBox1 =
New System.Windows.Forms.TextBox



Me.Label1 =
New System.Windows.Forms.Label



Me.Button3 =
New System.Windows.Forms.Button



Me.Button4 =
New System.Windows.Forms.Button



CType(
Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()



Me.SuspendLayout()



'



'DataGrid1



'



Me.DataGrid1.BackgroundColor = System.Drawing.Color.Lavender



Me.DataGrid1.DataMember = ""



Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText



Me.DataGrid1.Location =
New System.Drawing.Point(2, 4)



Me.DataGrid1.Name = "DataGrid1"



Me.DataGrid1.Size =
New System.Drawing.Size(726, 348)



Me.DataGrid1.TabIndex = 0



'



'Button1



'



Me.Button1.Location =
New System.Drawing.Point(26, 370)



Me.Button1.Name = "Button1"



Me.Button1.Size =
New System.Drawing.Size(100, 34)



Me.Button1.TabIndex = 1



Me.Button1.Text = "Access"



'



'Button2



'



Me.Button2.Location =
New System.Drawing.Point(582, 368)



Me.Button2.Name = "Button2"



Me.Button2.Size =
New System.Drawing.Size(100, 34)



Me.Button2.TabIndex = 2



Me.Button2.Text = "SqlServer"



'



'TextBox1



'



Me.TextBox1.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper



Me.TextBox1.Location =
New System.Drawing.Point(370, 372)



Me.TextBox1.Name = "TextBox1"



Me.TextBox1.Size =
New System.Drawing.Size(140, 22)



Me.TextBox1.TabIndex = 3



Me.TextBox1.Text = ""



'



'Label1



'



Me.Label1.Location =
New System.Drawing.Point(217, 374)



Me.Label1.Name = "Label1"



Me.Label1.Size =
New System.Drawing.Size(148, 26)



Me.Label1.TabIndex = 4



Me.Label1.Text = "Libellé commençant par"



'



'Button3



'



Me.Button3.Location =
New System.Drawing.Point(512, 375)



Me.Button3.Name = "Button3"



Me.Button3.Size =
New System.Drawing.Size(35, 20)



Me.Button3.TabIndex = 5



Me.Button3.Text = "Ok"



'



'Button4



'



Me.Button4.Location =
New System.Drawing.Point(158, 372)



Me.Button4.Name = "Button4"



Me.Button4.Size =
New System.Drawing.Size(30, 24)



Me.Button4.TabIndex = 6



Me.Button4.Text = "Button4"



'



'Form1



'



Me.AutoScaleBaseSize =
New System.Drawing.Size(6, 15)



Me.ClientSize =
New System.Drawing.Size(732, 424)



Me.Controls.Add(
Me.Button4)



Me.Controls.Add(
Me.Button3)



Me.Controls.Add(
Me.Label1)



Me.Controls.Add(
Me.TextBox1)



Me.Controls.Add(
Me.Button2)



Me.Controls.Add(
Me.Button1)



Me.Controls.Add(
Me.DataGrid1)



Me.Name = "Form1"



Me.Text = "Form1"



CType(
Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()



Me.ResumeLayout(
False)



End
Sub


#
End
Region





Private
Sub Button1_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button1.Click


DataGrid1.TableStyles.Clear()


oConnect =
New OleDbConnection


oConnect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _


"Data Source=C:\vb.net\Immo\Immo\Database\immo.mdb"


Alimgrille()



End
Sub



Private
Sub Button2_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button2.Click


DataGrid1.TableStyles.Clear()


oConnect =
New OleDbConnection


oConnect.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Immo;Data Source=JMB76"


Alimgrille()



End
Sub



Private
Sub Alimgrille()



Dim dSet
As DataSet



Dim strSql
As
String



Dim IntAvgCharWidth
As
Integer



Dim IntAvgNumWidth
As
Integer



Dim objImmoCM
As CurrencyManager
'Gère une liste d'objets ici champs de la table Immo



' calcule la largeur moyenne des caractères alphe



With Graphics.FromHwnd(
Me.Handle).MeasureString(Text:="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy", Font:=
Me.Font)


IntAvgCharWidth =
CInt(.Width / 52.0!)



End
With



' calcule la largeur moyenne des caractères numériques



With Graphics.FromHwnd(
Me.Handle).MeasureString(Text:="0123456789", Font:=
Me.Font)


IntAvgNumWidth =
CInt(.Width / 10.0!)



End
With


dSet =
New DataSet



' interception d'une éventuelle erreur



Try



With oConnect


.Open()



End
With



Catch dbError
As OleDbException



Stop



End
Try


oConnect.Close()



' requete SQL sur la table Immo



If TextBox1.Text = ""
Then


strSql = "SELECT * from immo order by noinv "



Else


strSql = "SELECT * from immo where natinv like '" & TextBox1.Text & "%' order by noinv"



End
If



' interception d'une éventuelle erreur



Try



' ajoute la requete à l'oleadapter



With
New OleDbDataAdapter(selectCommand:=
New OleDbCommand(cmdText:=strSql, _


Connection:=oConnect))



' remplit le dataset


.Fill(DataSet:=dSet, srcTable:="immo")



' libère OleAAdapter de la memoire


.Dispose()



End
With



Catch dbError
As OleDbException



Stop



End
Try



' alimente objImmoCM avec le dataset cela n'est pas nécessaire si nous



' n'avions pas de GridTableStyle



With dSet.Tables


objImmoCM =
CType(
Me.BindingContext(.Item(Name:="immo")), CurrencyManager)



End
With



' définition de la table de style


GridTableStyle =
New DataGridTableStyle



'Dim pagerStyle As System.Web.UI.WebControls.DataGridPagerStyle



'pagerStyle.VerticalAlign = Web.UI.WebControls.VerticalAlign.Middle



'Ajout des colonnes au GridTableStyle



With GridTableStyle


.BackColor = System.Drawing.Color.White


.ForeColor = System.Drawing.Color.DarkSlateBlue


.GridLineColor = System.Drawing.Color.MediumSlateBlue


.HeaderBackColor = System.Drawing.Color.Lavender


.HeaderForeColor = System.Drawing.Color.MediumSlateBlue


.AlternatingBackColor = Color.LightGray


.RowHeaderWidth = 10


.MappingName = "immo"



With .GridColumnStyles


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="Noinv")))



With .Item(index:=0)


.MappingName = "Noinv"


.HeaderText = "N° Inv "


.Alignment = HorizontalAlignment.Right


.Width = IntAvgNumWidth * 10
' 10 la taille du champ


.ReadOnly =
True



End
With


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="Compte")))



With .Item(index:=1)


.MappingName = "Compte"


.HeaderText = "N° Compte"


.Alignment = HorizontalAlignment.Right


.Width = IntAvgNumWidth * 10
' 10 la taille du champ


.ReadOnly =
True



End
With


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="DateSaisie")))



With .Item(index:=2)


.MappingName = "DateSaisie"


.HeaderText = "Date Saisie"


.Alignment = HorizontalAlignment.Center


.Width = IntAvgNumWidth * 10


.ReadOnly =
True



End
With


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="Natinv")))



With .Item(index:=3)


.MappingName = "Natinv"


.HeaderText = "Libelle"


.Alignment = HorizontalAlignment.Left


.Width = IntAvgCharWidth * 30
' 10 la taille du champ


.ReadOnly =
False



End
With


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="Fourn")))



With .Item(index:=4)


.MappingName = "Fourn"


.HeaderText = "Fournisseur"


.Alignment = HorizontalAlignment.Left


.Width = IntAvgCharWidth * 15


.ReadOnly =
True



End
With


.Add(column:=
New DataGridTextBoxColumn(objImmoCM. _


GetItemProperties.Item(Name:="Montant"), "f"))
' c si euro



With .Item(index:=5)


.MappingName = "Montant"


.HeaderText = "Montant"


.Alignment = HorizontalAlignment.Right


.Width = IntAvgNumWidth * 12


.ReadOnly =
True



End
With



End
With



End
With



' alimentation de la grille avec DataSource


DataGrid1.DataSource = dSet



' nécessaire pour ne pas avoir d'arborescence dans la visu de la grille puique l'on a qu'une table



' Obtient ou définit la liste spécifique dans DataSource pour laquelle



' le contrôle System.Windows.Forms.DataGrid affiche une grille.


DataGrid1.DataMember = "Immo"



' ajout de la table de style


DataGrid1.TableStyles.Add(table:=GridTableStyle)



End
Sub



Private
Sub DataGrid1_DoubleClick(
ByVal sender
As
Object,
ByVal e
As System.EventArgs)
Handles DataGrid1.DoubleClick



' connaitre la valeur de la cellule en cours



' MsgBox(DataGrid1.Item(x,y))


MsgBox(DataGrid1.Item(DataGrid1.CurrentCell))



End
Sub



Private
Sub Button3_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button3.Click



' indispensable<


DataGrid1.TableStyles.Clear()


Alimgrille()



End
Sub



Private
Sub Form1_Load(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles
MyBase.Load



End
Sub



Private
Sub Button4_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button4.Click


' dvProducts.RowFilter "noinv Max(noinv)"



End
Sub


End
Class

****************
@+
labout
3
piervs Messages postés 26 Date d'inscription vendredi 18 avril 2003 Statut Membre Dernière intervention 4 mai 2005
25 avril 2005 à 10:09
Merci de ton aide !!
Comme ton exemple est relativement touffu, je vais prendre le temps de bien l'analyser et je te dirais si ca a marché.... ou pas.

a+
Piervs
0
piervs Messages postés 26 Date d'inscription vendredi 18 avril 2003 Statut Membre Dernière intervention 4 mai 2005
25 avril 2005 à 14:01
Super je te remercie ca marche parfaitement !!!!!
0
Rejoignez-nous