Salut,
Je te fais un copier/coller d'un exemple que j'avais déja donné avec un DataGrid pour le remplir, customiser ses colonnes et filtrer les lignes, ça doit pas être bien différent avec le DataGridView.. ( ?? )
string cnxString =
@"Data Source = localhost\SQLEXPRESS;
Initial Catalog = Northwind;
Integrated Security = true;";
string cmdString = @"SELECT * FROM dbo.Products";
DataSet ds;
using ( SqlConnection cnx = new SqlConnection( cnxString ) )
{
cnx.Open( );
SqlDataAdapter da = new SqlDataAdapter( cmdString, cnx );
ds = new DataSet( "Northwind" );
da.Fill( ds, "Products" ); // Donne un nom à la table créée.
}
// Afficher uniquement 2 colones.
DataGridTextBoxColumn columnProductName = new DataGridTextBoxColumn( );
columnProductName.MappingName = "ProductName"; // Nom de la colonne.
columnProductName.HeaderText = "Nom du Produit";
DataGridTextBoxColumn columnUnitPrice = new DataGridTextBoxColumn( );
columnUnitPrice.MappingName = "UnitPrice"; // Nom de la colonne.
columnUnitPrice.HeaderText = "Prix à l'unité";
DataGridTableStyle style = new DataGridTableStyle( );
style.MappingName = "Products"; // Nom de la table.
style.GridColumnStyles.AddRange
(
new DataGridColumnStyle[ ]
{
columnProductName,
columnUnitPrice
}
);
// Afficher uniquement certaines lignes
DataView dv = new DataView( ds.Tables[ "Products" ] ); // Nom de la table.
dv.RowFilter = "ProductName='Chai'OR ProductName='Ikura'"; // comme un WHERE.
DataGrid dg = new DataGrid( );
dg.DataSource = dv;
dg.TableStyles.Add( style );
dg.Dock = DockStyle.Fill;
this.Controls.Add( dg );