Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1450 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  passage de variable a la volée vers une form

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

passage de variable a la volée vers une form

n°2185557
angelz7
Posté le 12-04-2013 à 11:13:57  profilanswer
 

bonjour a tous,  
 
je viens vers vous pour un petit souci qui a mon avis est simple mais que je n'arrive pas a comprendre  
 
Au passage de suis débutant en c# (en fait je bidouille plus qu'autre chose :))  
 
voila le topo:  
 
1 je fais une requête sql suite a ça je crée des ligne a la volé avec 1 ligne par résultat sql avec a la fin de la ligne un bouton modifier  
 
voici le code (surement pas le meilleur d'ailleurs toute remarque sera intéressante :)  
 

Code :
  1. private void affichage_list_article()
  2.         {
  3.             using (SqlCeConnection myConnection = new SqlCeConnection(@"Data Source=d:\utilisateurs\angelz\documents\visual studio 2012\Projects\BigRedOne-Gestion\BigRedOne-Gestion\data.sdf;Password=pass;" ))
  4.             using (SqlCeCommand myCmd = myConnection.CreateCommand())
  5.             {
  6.                 myCmd.CommandType = CommandType.Text;
  7.                 myCmd.CommandText = "SELECT * FROM gestion_stock ORDER BY id";
  8.                 myConnection.Open();
  9.                 SqlCeDataReader rdr = myCmd.ExecuteReader();
  10.                 int pos_vertical = 65;
  11.                 while (rdr.Read())
  12.                 {
  13.                     string nom = "";
  14.                     string type = "";
  15.                     string prix_achat_htva = "";
  16.                     string prix_vente_htva = "";
  17.                     string benefice_htva = "";
  18.                     string prix_achat_ttc = "";
  19.                     string prix_vente_ttc = "";
  20.                     string benefice_ttc = "";
  21.                     string fournisseur = "";
  22.                     string tel_fournisseur = "";
  23.                     string mail_fournisseur = "";
  24.                     string quantite_stock = "";
  25.                     string taux_tva = "";
  26.                     Int64 id = rdr.GetInt64(0);
  27.                     if (rdr.GetString(1) != null)
  28.                     {
  29.                         nom = rdr.GetString(1);
  30.                     }
  31.                     else
  32.                     {
  33.                         nom = "";
  34.                     }
  35.                     if (rdr.GetString(2) != null)
  36.                     {
  37.                         type = rdr.GetString(2);
  38.                     }
  39.                     else
  40.                     {
  41.                         type = "";
  42.                     }
  43.                     if (rdr.GetString(3) != null)
  44.                     {
  45.                         prix_achat_htva = rdr.GetString(3);
  46.                     }
  47.                     else
  48.                     {
  49.                         prix_achat_htva = "";
  50.                     }
  51.                     if (rdr.GetString(4) != null)
  52.                     {
  53.                         prix_vente_htva = rdr.GetString(4);
  54.                     }
  55.                     else
  56.                     {
  57.                         prix_vente_htva = "";
  58.                     }
  59.                     if (rdr.GetString(5) != null)
  60.                     {
  61.                         benefice_htva = rdr.GetString(5);
  62.                     }
  63.                     else
  64.                     {
  65.                         benefice_htva = "";
  66.                     }
  67.                     if (rdr.GetString(6) != null)
  68.                     {
  69.                         prix_achat_ttc = rdr.GetString(6);
  70.                     }
  71.                     else
  72.                     {
  73.                         prix_achat_ttc = "";
  74.                     }
  75.                     if (rdr.GetString(7) != null)
  76.                     {
  77.                         prix_vente_ttc = rdr.GetString(7);
  78.                     }
  79.                     else
  80.                     {
  81.                         prix_vente_ttc = "";
  82.                     }
  83.                     if (rdr.GetString(8) != null)
  84.                     {
  85.                         benefice_ttc = rdr.GetString(8);
  86.                     }
  87.                     else
  88.                     {
  89.                         benefice_ttc = "";
  90.                     }
  91.                     if (rdr.GetString(9) != null)
  92.                     {
  93.                         fournisseur = rdr.GetString(9);
  94.                     }
  95.                     else
  96.                     {
  97.                         fournisseur = "";
  98.                     }
  99.                     if (rdr.GetString(10) != null)
  100.                     {
  101.                         tel_fournisseur = rdr.GetString(10);
  102.                     }
  103.                     else
  104.                     {
  105.                         tel_fournisseur = "";
  106.                     }
  107.                     if (rdr.GetString(11) != null)
  108.                     {
  109.                         mail_fournisseur = rdr.GetString(11);
  110.                     }
  111.                     else
  112.                     {
  113.                         mail_fournisseur = "";
  114.                     }
  115.                     if (rdr.GetString(12) != null)
  116.                     {
  117.                         quantite_stock = rdr.GetString(12);
  118.                     }
  119.                     else
  120.                     {
  121.                         quantite_stock = "";
  122.                     }
  123.                     Int16 achat = rdr.GetInt16(13);
  124.                     Int16 vente = rdr.GetInt16(14);
  125.                    
  126.                     if (rdr.GetString(15) != null)
  127.                     {
  128.                         taux_tva = rdr.GetString(15);
  129.                     }
  130.                     else
  131.                     {
  132.                         taux_tva = "";
  133.                     }
  134.                    
  135.                     string id_string = Convert.ToString(id);
  136.                     Label monlabelid = new Label();
  137.                     monlabelid.Text = id_string;
  138.                     monlabelid.Width = 30;
  139.                     monlabelid.Location = new Point(16, pos_vertical);
  140.                     affichage_stock.Controls.Add(monlabelid);
  141.                     Label monlabelnom = new Label();
  142.                     monlabelnom.Text = nom;
  143.                     monlabelid.Width = 30;
  144.                     monlabelnom.Location = new Point(70, pos_vertical);
  145.                     affichage_stock.Controls.Add(monlabelnom);
  146.                     Label monlabeltype = new Label();
  147.                     monlabeltype.Text = type;
  148.                     monlabeltype.Width = 90;
  149.                     monlabeltype.Location = new Point(180, pos_vertical);
  150.                     affichage_stock.Controls.Add(monlabeltype);
  151.                     Label monlabelachat = new Label();
  152.                     monlabelachat.Text = prix_achat_ttc + " €";
  153.                     monlabelachat.Width = 90;
  154.                     monlabelachat.Location = new Point(320, pos_vertical);
  155.                     affichage_stock.Controls.Add(monlabelachat);
  156.                     Label monlabelvente = new Label();
  157.                     monlabelvente.Text = prix_vente_ttc + " €";
  158.                     monlabelvente.Width = 90;
  159.                     monlabelvente.Location = new Point(420, pos_vertical);
  160.                     affichage_stock.Controls.Add(monlabelvente);
  161.                     Label monlabelbenef = new Label();
  162.                     monlabelbenef.Text = benefice_ttc + " €";
  163.                     monlabelbenef.Width = 90;
  164.                     monlabelbenef.Location = new Point(520, pos_vertical);
  165.                     affichage_stock.Controls.Add(monlabelbenef);
  166.                     Label monlabeltva = new Label();
  167.                     monlabeltva.Text = taux_tva + " %";
  168.                     monlabeltva.Width = 50;
  169.                     monlabeltva.Location = new Point(640, pos_vertical);
  170.                     affichage_stock.Controls.Add(monlabeltva);
  171.                     Label monlabelfournisseur = new Label();
  172.                     monlabelfournisseur.Text = fournisseur;
  173.                     monlabelfournisseur.Width = 120;
  174.                     monlabelfournisseur.Location = new Point(710, pos_vertical);
  175.                     affichage_stock.Controls.Add(monlabelfournisseur);
  176.                     if (achat == 1 && vente == 0)
  177.                     {
  178.                         Label monlabelachat_depot = new Label();
  179.                         monlabelachat_depot.Text = "Achat";
  180.                         monlabelachat_depot.Width = 60;
  181.                         monlabelachat_depot.Location = new Point(880, pos_vertical);
  182.                         affichage_stock.Controls.Add(monlabelachat_depot);
  183.                     }
  184.                     if (achat == 0 && vente == 1)
  185.                     {
  186.                         Label monlabelachat_depot = new Label();
  187.                         monlabelachat_depot.Text = "Dépot";
  188.                         monlabelachat_depot.Width = 60;
  189.                         monlabelachat_depot.Location = new Point(880, pos_vertical);
  190.                         affichage_stock.Controls.Add(monlabelachat_depot);
  191.                     }
  192.                     if (achat == 0 && vente == 0)
  193.                     {
  194.                         Label monlabelachat_depot = new Label();
  195.                         monlabelachat_depot.Text = "?";
  196.                         monlabelachat_depot.Width = 60;
  197.                         monlabelachat_depot.Location = new Point(880, pos_vertical);
  198.                         affichage_stock.Controls.Add(monlabelachat_depot);
  199.                     }
  200.                     Label monlabelquantite = new Label();
  201.                     monlabelquantite.Text = quantite_stock;
  202.                     monlabelquantite.Width = 30;
  203.                     monlabelquantite.Location = new Point(990, pos_vertical);
  204.                     affichage_stock.Controls.Add(monlabelquantite);
  205.                     Button buttonModif = new Button();
  206.                     buttonModif.Text = "Modifier";
  207.                     buttonModif.Tag = id_string;
  208.                     buttonModif.Click += buttonModif_Click;
  209.                     buttonModif.Location = new Point(1050, pos_vertical);
  210.                     affichage_stock.Controls.Add(buttonModif);
  211.                    
  212.                     pos_vertical = pos_vertical + 20;
  213.                 }
  214.                
  215.                
  216.                
  217.                 myConnection.Close();
  218.             }
  219.         }


 
pour cette partie cela fonctionne  
 
2. lorsque que j’appuie sur le bouton modifier je voudrais ouvrir une autre form en lui passant la variable id pour pouvoir l'utiliser pour modifier/supprimer la ligne sql mais c'est la que je cale  
 

Code :
  1. protected void buttonModif_Click(object sender, EventArgs e)
  2.         {
  3.            modif_article fc = new modif_article();
  4.           fc.MdiParent = form_base.ActiveForm;
  5.           fc.Show();
  6.            
  7.         }


 
et voici le code de la form modifier qui est appeler  
 

Code :
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace BigRedOne_Gestion
  11. {
  12.     public partial class modif_article : Form
  13.     {
  14.         public modif_article()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void modif_article_Load(object sender, EventArgs e)
  19.         {
  20.               MessageBox.Show(id);
  21.         }
  22.     }
  23. }


 
voila déjà si je pouvez réussir a afficher l'id de la ligne sql dans la form modifier se serait déjà une grande victoire ;)  
 
merci de votre aide

mood
Publicité
Posté le 12-04-2013 à 11:13:57  profilanswer
 

n°2185562
x1fr
Posté le 12-04-2013 à 11:25:16  profilanswer
 

Code :
  1. protected void buttonModif_Click(object sender, EventArgs e)
  2.         {
  3.            modif_article fc = new modif_article( (string) ((Button)sender).Tag );
  4.           fc.MdiParent = form_base.ActiveForm;
  5.           fc.Show();
  6.          
  7.         }
 
Code :
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace BigRedOne_Gestion
  11. {
  12.     public partial class modif_article : Form
  13.     {
  14.         protected string _ID;
  15.         public modif_article(string id)
  16.         {
  17.             InitializeComponent();
  18.             _ID = id;
  19.         }
  20.         private void modif_article_Load(object sender, EventArgs e)
  21.         {
  22.               MessageBox.Show(_ID);
  23.         }
  24.     }
  25. }
 

Et dans la fenêtre modif_article tu as accès à l'identifiant dans _ID (tu peux aussi garder l'id sous forme int64, pas besoin de passer par du string.

 

Sinon pour la mise en page, il y a des solutions plus simple (genre datagrid), parce que tout construire à la main, ça doit être relou :P


Message édité par x1fr le 12-04-2013 à 11:25:56

---------------
Origin / PSN / Steam / Uplay : x1fr - bnet : Fab#2717
n°2185584
angelz7
Posté le 12-04-2013 à 12:06:00  profilanswer
 

merci pour ta réponse,  
 
si je test  
 
modif_article fc = new modif_article( (string) ((Button)sender).Tag );
j'ai l'erreur :
 
Erreur 5 'projet.modif_article' ne contient pas un constructeur qui accepte des arguments 1 d:\utilisateurs\angelz\documents\visual studio 2012\Projects\projet-Gestion\projet-Gestion\gestion_de_stock.cs 301 32 projet-Gestion

n°2185591
x1fr
Posté le 12-04-2013 à 12:55:59  profilanswer
 

tu n'as pas mis à jour la classe modif_article comme dans l'exemple


---------------
Origin / PSN / Steam / Uplay : x1fr - bnet : Fab#2717
n°2185594
angelz7
Posté le 12-04-2013 à 13:22:05  profilanswer
 

merci beaucoup tu es un dieu ^^
 
par contre maintenant que ce problème est résolu , encore merci ;-)
 
je suis intéressé par ta solution datagrid :) '(c'est vrai que tout construire manuellement c relou ^^)
pourrais-tu m'expliquer un peu ou si tu a un lien correcte avec de la doc sur cela :)
 
mercii

n°2185599
x1fr
Posté le 12-04-2013 à 13:50:28  profilanswer
 

Je me suis trompé, datagrid c'est l'ancien contrôle en fait, maintenant il s'appelle DataGridView
 
un exemple sur msdn:
http://msdn.microsoft.com/fr-fr/library/fbk67b6z.aspx


---------------
Origin / PSN / Steam / Uplay : x1fr - bnet : Fab#2717
n°2185605
alien cons​piracy
hardtrance addict
Posté le 12-04-2013 à 14:49:56  profilanswer
 

x1fr a écrit :

Je me suis trompé, datagrid c'est l'ancien contrôle en fait, maintenant il s'appelle DataGridView
 
un exemple sur msdn:
http://msdn.microsoft.com/fr-fr/library/fbk67b6z.aspx


Pour etre précis, DataGrid est le nom d'un objet dans WPF tandis DataGridView est le nom d'un objet dans Windows Forms.

n°2185607
x1fr
Posté le 12-04-2013 à 15:02:32  profilanswer
 

Même pas, je parlais d'un contrôle présent sur Visual Studio 2003 vu que c'est ce que l'on utilisait au boulot jusqu'à il y a encore quelques semaines :P

 

edit : Cela dit je ne remets pas en cause l'existence du controle wpf :)


Message édité par x1fr le 12-04-2013 à 15:05:43

---------------
Origin / PSN / Steam / Uplay : x1fr - bnet : Fab#2717

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  passage de variable a la volée vers une form

 

Sujets relatifs
Passage de variable dans une autre formBoucle vérification d'une valeur dans une variable
git : cloner un repository vers un autre serveur ?innertHML et variable de session PHP
C++ - Cplex problem de variable dans une boucle for & randomPassage paramètre Cookie Javascript
Extraction chaine de caractere variable [RESOLU]Copie d'une ArrayList d'une classe vers une autre
Plus de sujets relatifs à : passage de variable a la volée vers une form


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR