Voilà j'utilise Visual Studio .NET 2003 et j'ai une erreur "Une exception non gérée du type 'System.InvalidCastException'..." qui se situe au niveaux de "protected void dataGrid1_MouseDown {" les lignes "MycolumnStyle gridCol = (MyColumnStyle)..."
Ca dois être surement un erreur bête, en faite je patoge depuis 2 jours a cause de ça.
J'affiche ma form1.cs compléte !!
Vous étes mon dernier recours !
Merci d'avance.
[CODE]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace test
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();
//
// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
//
}
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Code généré par le Concepteur Windows Form
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(40, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(408, 328);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 373);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void AjoutDataGridTableStyle()
{
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataTable dt = (DataTable) myDataSet.Tables["Clients"];
tableStyle.MappingName = dt.TableName;
for(int j = 0; j < dt.Columns.Count; ++j)
{
DataGridTextBoxColumn cs = new DataGridTextBoxColumn();
cs.MappingName = dt.Columns[j].ColumnName;
cs.HeaderText = dt.Columns[j].ColumnName;
tableStyle.GridColumnStyles.Add(cs);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
}
private void Form1_Load(object sender, System.EventArgs e)
{
CreationDataSet();
AjoutDataGridTableStyle();
dataGrid1.DataSource = myDataSet.Tables["Clients"];
}
private DataSet myDataSet;
private void CreationDataSet()
{
// Création d'un DataSet
myDataSet = new DataSet("myDataSet");
// Création de la DataTable "Clients"
DataTable tClients = new DataTable("Clients");
// Création de 4 colonnes, et ajout de ces colonnes à la table.
tClients.Columns.Add(new DataColumn("Id", Type.GetType("System.Int32")));
tClients.Columns.Add(new DataColumn("Nom", Type.GetType("System.String")));
tClients.Columns.Add(new DataColumn("Prenom", Type.GetType("System.String")));
tClients.Columns.Add(new DataColumn("CodeVille", Type.GetType("System.String")));
// Création de 5 enregistrements dans la table "Clients"
tClients.Rows.Add(new Object[] {0, "Dupond", "Pierre", "PAR"});
tClients.Rows.Add(new Object[] {1, "Smith", "John", "LON"});
tClients.Rows.Add(new Object[] {2, "Gonzales", "Pedro", "MAD"});
tClients.Rows.Add(new Object[] {3, "Durand", "Bernadette", "PAR"});
tClients.Rows.Add(new Object[] {4, "Dickinson", "Paul", "LON"});
// Ajout de la Table "Clients" au DataSet
myDataSet.Tables.Add(tClients);
// Création de la DataTable "Villes"
DataTable tVilles = new DataTable("Villes");
// Création de 2 colonnes, et ajout de ces colonnes à la table.
tVilles.Columns.Add(new DataColumn("CodeVille", Type.GetType("System.String")));
tVilles.Columns.Add(new DataColumn("Libelle", Type.GetType("System.String")));
// Création de 3 enregistrements dans la table "Villes"
tVilles.Rows.Add(new Object[] {"PAR", "Paris"});
tVilles.Rows.Add(new Object[] {"LON", "Londres"});
tVilles.Rows.Add(new Object[] {"MAD", "Madrid"});
// Ajout de la Table "Villes" au DataSet
myDataSet.Tables.Add(tVilles);
}
public class MyColumnStyle:DataGridTextBoxColumn
{
public void EditVal(CurrencyManager cm, int row, Rectangle rec,
bool readOnly, string text)
{
this.Edit(cm, row, rec, readOnly, text);
}
}
protected void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
// Use the HitTest method to get a HitTestInfo object.
System.Windows.Forms.DataGrid.HitTestInfo hi;
DataGrid grid = (DataGrid) sender;
hi=grid.HitTest(e.X, e.Y);
// Test if the clicked area was a cell.
if (hi.Type==DataGrid.HitTestType.Cell)
{
// If it's a cell, get the GridTable and CurrencyManager of the
// clicked table.
DataGridTableStyle dgt = grid.TableStyles[0];
CurrencyManager myCurrencyManager = (CurrencyManager)
this.BindingContext[myDataSet.Tables[dgt.MappingName]];
// Get the Rectangle of the clicked cell.
Rectangle cellRect;
cellRect=grid.GetCellBounds(hi.Row, hi.Column);
// Get the clicked DataGridTextBoxColumn.
MyColumnStyle gridCol =(MyColumnStyle)
dgt.GridColumnStyles[hi.Column];
// Edit the value.
gridCol.EditVal(myCurrencyManager, hi.Row, cellRect, false, "New Text");
}
}
}
}
[/CODE]