C# windows form databinding avec datagrid + combox dans les colonnes.

Description

Voici un petit exemple de DataBinding qui a été développé avec le RAD SharpDevelop (version Fidalgo 1). Le dossier contient également un Export pour visual studio mais je ne sais pas si ça marche !!

Désolé il n'y a pas beaucoup de commentaires... mais si vous avez des questions n'hésitez pas -;)

Le prg met montre comment faire pour insérer une liste de choix (ComboBox) dans une cellule du DataGrid en utilisant deux classes qui ont été récuperées sur le NET.

Voici le code d'ouverture. (mise en Place du DataGridTableStyle... etc)

Source / Exemple :


private void OpenDataBase()
{	

	this.myConnection.ConnectionString = util.connectionPath;

	this.myCommand.CommandText = "SELECT * FROM Livre";
	this.myDataAdapter.SelectCommand = this.myCommand;
	this.myDataAdapter.Fill(this.myDataSet, "Livre");

	this.myCommand.CommandText = "SELECT IDPersonne, Nom & ' ' & Prenom as NP FROM Personne ORDER BY Nom ASC";
	this.myDataAdapter.SelectCommand = this.myCommand;
	this.myDataAdapter.Fill(this.myDataSet, "Personne");

	this.myCommand.CommandText = "SELECT * FROM Bibliotheque";
	this.myDataAdapter.SelectCommand = this.myCommand;
	this.myDataAdapter.Fill(this.myDataSet, "Bibliotheque");

	DataGridTableStyle tableStyle = new DataGridTableStyle();
	DataTable dataTable  = myDataSet.Tables["Bibliotheque"];
	tableStyle.MappingName = "Bibliotheque";
	tableStyle.RowHeadersVisible = true;
	tableStyle.RowHeaderWidth = 400;

	for (int i = 0; i < dataTable.Columns.Count; i++)
	{
		switch (i)
		{
			case 0:
				DataGridTextBoxColumn column_0 = new DataGridTextBoxColumn();
				column_0.MappingName = "IdSortie";
				column_0.HeaderText = "N";
				column_0.Width = 0;
				tableStyle.GridColumnStyles.Add(column_0);
				break;
			case 1:  
				DataGridComboBoxColumn column_1 = new DataGridComboBoxColumn();

				column_1.MappingName = "IdLivre";
				column_1.HeaderText = "Nom du livre";
				column_1.Width = 150;
				column_1.myComboBox.DataSource = myDataSet.Tables["Livre"].DefaultView;
				column_1.myComboBox.DisplayMember = "NomDuLivre";
				column_1.myComboBox.ValueMember = "IdLivre";
				tableStyle.PreferredRowHeight = column_1.myComboBox.Height;
				tableStyle.GridColumnStyles.Add(column_1);
				break;

			case 2:  
				DataGridComboBoxColumn column_2 = new DataGridComboBoxColumn();

				column_2.MappingName = "IdPersonne";
				column_2.HeaderText = "Nom et Prénom";
				column_2.Width = 150;
				column_2.myComboBox.DataSource = myDataSet.Tables["Personne"].DefaultView;
				column_2.myComboBox.DisplayMember = "NP";
				column_2.myComboBox.ValueMember = "IdPersonne";
				tableStyle.PreferredRowHeight = column_2.myComboBox.Height;
				tableStyle.GridColumnStyles.Add(column_2);
				break;
		
			case 3:
				DataGridTextBoxColumn column_3 = new DataGridTextBoxColumn();
				
				column_3.MappingName = "DateSortie";
				column_3.HeaderText = "Date de sortie";
				column_3.Width = 90;
				tableStyle.GridColumnStyles.Add(column_3);
				break;
		}	
	}

	this.myDataGrid.TableStyles.Clear();
	this.myDataGrid.TableStyles.Add(tableStyle);
	this.myDataGrid.DataSource = dataTable;
}

Conclusion :


bonne prog @ tous. VIC

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.