2017-10-06 4 views
0

Comment empêcher l'utilisateur de cocher la case d'une ligne de grille si la "Qté" de cette ligne de grille est "0"? J'ai le code ci-dessous mais il le permet toujours car tant qu'il y a une ligne sélectionnée, elle permet de sélectionner la seconde et ainsi de suite sans changer la valeur de la Qté. La ligne sélectionnée de chaque bouton ci-dessus est ensuite envoyée à form2.Cochez cette case si la valeur n'est pas supérieure à 1

public Test() 
    { 
     sqlite = new SQLiteConnection("Data Source=C:/MyCedarDataConn/CedarPaints.db;"); 
     InitializeComponent(); 
     DataColumnCollection columns = Test.dt.Columns; 
     DataColumn[] dataColumn = new DataColumn[] { new DataColumn("ProdCode"), new DataColumn("Product"), new DataColumn("Pack"), new DataColumn("t1"), new DataColumn("t2"), new DataColumn("Qty"), new DataColumn("WSale") }; 
     columns.AddRange(dataColumn); 
    } 

    private void BindDataGrid(string query) 
    { 

     this.dataGridView1.DataSource = null; 
     this.dataGridView1.Columns.Clear(); 
     this.dataGridView1.ColumnCount = 7; 
     this.dataGridView1.AllowUserToAddRows = false; 
     this.dataGridView1.Columns[0].Name = "ProdCode"; 
     this.dataGridView1.Columns[0].HeaderText = "ProdCode"; 
     this.dataGridView1.Columns[0].DataPropertyName = "ProdCode"; 
     this.dataGridView1.Columns[0].Width = 100; 

     this.dataGridView1.Columns[1].HeaderText = "Product"; 
     this.dataGridView1.Columns[1].Name = "Product"; 
     this.dataGridView1.Columns[1].DataPropertyName = "Product"; 
     this.dataGridView1.Columns[1].Width = 160; 

     this.dataGridView1.Columns[2].HeaderText = "t1"; 
     this.dataGridView1.Columns[2].Name = "t1"; 
     this.dataGridView1.Columns[2].DataPropertyName = "t1"; 
     this.dataGridView1.Columns[2].Width = 1; 
     this.dataGridView1.Columns[2].Visible = false; 

     this.dataGridView1.Columns[3].HeaderText = "Pack"; 
     this.dataGridView1.Columns[3].Name = "Pack"; 
     this.dataGridView1.Columns[3].DataPropertyName = "Pack"; 
     this.dataGridView1.Columns[3].Width = 50; 


     this.dataGridView1.Columns[4].HeaderText = "t2"; 
     this.dataGridView1.Columns[4].Name = "t2"; 
     this.dataGridView1.Columns[4].DataPropertyName = "t2"; 
     this.dataGridView1.Columns[4].Width = 1; 
     this.dataGridView1.Columns[4].Visible = false; 

     this.dataGridView1.Columns[5].Name = "Qty"; 
     this.dataGridView1.Columns[5].HeaderText = "Qty"; 
     this.dataGridView1.Columns[5].DataPropertyName = "Qty"; 
     this.dataGridView1.Columns[5].Width = 50; 

     this.dataGridView1.Columns[6].Name = "WSale"; 
     this.dataGridView1.Columns[6].HeaderText = "WSale"; 
     this.dataGridView1.Columns[6].DataPropertyName = "WSale"; 
     this.dataGridView1.Columns[6].Width = 58; 
     this.dataGridView1.Columns[6].DefaultCellStyle.Format = "0.00##"; 

     DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn() 
     { 
      HeaderText = "", 
      Width = 30, 
      Name = "checkBoxColumn" 
     }; 

     dataGridView1.Columns.Insert(0, dataGridViewCheckBoxColumn); 

     using (SQLiteConnection con = new SQLiteConnection(sqlite)) 
     { 
      using (SQLiteCommand cmd = new SQLiteCommand(query, con)) 
      { 
       cmd.CommandType = CommandType.Text; 
       using (SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd)) 
       { 
        using (DataTable dt = new DataTable()) 
        { 

         sda.Fill(dt); 
         this.dataGridView1.DataSource = dt; 

        } 
       } 

      } 
     } 



private void button1_Click(object sender, EventArgs e) 
    { 
     pictureBox4.Hide(); 
     pictureBox5.Hide(); 
     pictureBox6.Hide(); 
     pictureBox15.Hide(); 
     pictureBox16.Hide(); 
     pictureBox17.Hide(); 
     pictureBox18.Hide(); 
     pictureBox19.Hide(); 
     pan1.Show(); 
     dataGridView1.Show(); 
     if (i == 1) 
     { 
      pan1.Image = imageList1.Images[28]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM PremiumSatinWallCoat"); 

     } 
     if (i == 2) 
     { 
      pan1.Image = imageList1.Images[34]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM DesignWashableSheen"); 
     } 
     if (i == 3) 
     { 
      pan1.Image = imageList1.Images[57]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM PolyurethaneEnamelTwinPack"); 
     } 
     if (i == 4) 
     { 
      pan1.Image = imageList1.Images[55]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM PremiumPlasterPrimerSolventBased"); 
     } 
     if (i == 5) 
     { 
      pan1.Image = imageList1.Images[57]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM SandingSealer"); 
     } 
     if (i == 6) 
     { 
      pan1.Image = imageList1.Images[39]; 
      BindDataGrid("SELECT ProdCode, Product, 0 as Pack, 0 as t1, 0 as t2, Qty, WSale FROM FM"); 
     } 
     if (i == 7) 
     { 
      pan1.Image = imageList1.Images[57]; 
      BindDataGrid("SELECT ProdCode, Product, 0 as Pack, 0 as t1, 0 as t2, Qty, WSale FROM ATC"); 
     } 
     if (i == 8) 
     { 
      pan1.Image = imageList1.Images[57]; 
      BindDataGrid("SELECT ProdCode, Product, Pack, 0 as t1, 0 as t2, Qty, WSale FROM Castors"); 
     } 
     if (i == 9) 
     { 
      pan1.Image = imageList1.Images[13]; 
      BindDataGrid("SELECT ProdCode, Product, 0 as Pack, 0 as t1, 0 as t2, Qty, WSale FROM FGSP"); 
     } 
    } 



private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
    { 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 

      string str1 = row.Cells[1].Value.ToString(); 
      string str2 = row.Cells[2].Value.ToString().ToLower(); 
      string str3 = row.Cells[3].Value.ToString(); 
      string str4 = row.Cells[4].Value.ToString(); 
      string str5 = row.Cells[5].Value.ToString(); 
      string str6 = row.Cells[6].Value.ToString(); 
      string str7 = row.Cells[7].Value.ToString(); 
      //string style = "<style>.UppercaseWords</style>"; 

      foreach (DataRow dr in dt.Rows) 
      { 

       string str8 = dr["ProdCode"].ToString(); 
       string str9 = dr["Product"].ToString().ToLower(); 
       string str10 = dr["t1"].ToString(); 
       string str11 = dr["Pack"].ToString(); 
       string str12 = dr["t2"].ToString(); 
       string str13 = dr["Qty"].ToString(); 
       string str14 = dr["WSale"].ToString(); 

       if (str1 == str8 && str2 == str9 && str3 == str10 && str4 == str11 && str5 == str12 && str6 == str13 && str7 == str14) { } 
       { 
          row.Cells["checkBoxColumn"].Value = true; 

         } 

        } 
       } 



private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) 
    { 
     string value4 = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); 
     if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].EditedFormattedValue.ToString().ToLower() == "true") 
     { 
       dt.Rows.Add(dataGridView1.Rows[e.RowIndex].Cells[1].Value, UppercaseWords(value4), dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value, dataGridView1.Rows[e.RowIndex].Cells[5].Value, dataGridView1.Rows[e.RowIndex].Cells[6].Value, dataGridView1.Rows[e.RowIndex].Cells[7].Value); 
      } 
      else 
      { 
       DataRow[] dataRows = dt.Select(string.Format("ProdCode='{0}' and Product='{1}' and t1='{2}' and t2='{3}' and Pack='{4}' and Qty='{5}' and WSale='{6}'", dataGridView1.Rows[e.RowIndex].Cells[1].Value, dataGridView1.Rows[e.RowIndex].Cells[2].Value, dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value, dataGridView1.Rows[e.RowIndex].Cells[5].Value, dataGridView1.Rows[e.RowIndex].Cells[6].Value, dataGridView1.Rows[e.RowIndex].Cells[7].Value)); 

       foreach (DataRow dataRow in dataRows) 
       { 
        dt.Rows.Remove(dataRow); 
       } 

      } 
     } 



private void vButton1_Click(object sender, EventArgs e) 
    { 

     DataView dw = new DataView(dt); 
     string[] cols = { "ProdCode", "Product", "t1", "t2", "Pack", "Qty", "WSale" }; 
     DataTable dt1 = dw.ToTable(true, cols); 

     if (dt1.Rows.Count == 0) 
     { 
      MessageBox.Show("Select atleast one record"); 
     } 
     else 
     { 
      DataTable filtereRecord = (from DataRow dr in dt1.Rows 
             where dr["Qty"].ToString() != "0" 
             select dr).CopyToDataTable(); 
      if (filtereRecord.Rows.Count > 0) 
      { 
       Form2 f = new Form2(dt1); 
       f.ShowDialog(); 


      } 
     } 
    } 

L'écran:

form 1

sheet 1

+0

Il semble que la capture d'écran ajoutée soit pour 'Form2', ai-je raison? Personne ici ne peut aider à corriger votre 'Form2' si vous n'incluez pas un seul détail à ce sujet (comme la définition de la grille, etc.). S'il vous plaît [modifier la question] (https://stackoverflow.com/posts/46600500/edit) et ajouter suffisamment de détails sur la grille de 'Form2' et sur comment/où cette case à cocher Row est définie. –

+0

HI. la capture d'écran est datagridview1 de form1. La case à cocher sélectionnée de ce formulaire est ensuite envoyée à form2. Cependant, la plupart du temps, l'utilisateur sélectionne la case à cocher mais oublie en quelque sorte de changer la valeur "Qté". – shabs

+0

Notez la différence entre votre titre et votre corps: "qty n'est pas supérieur à 1" ("qty <= 1") et "if the Qty" de cette ligne de grille est "0" "(" qty <1 "). – Flater

Répondre

0

Vous pouvez utiliser l'événement CellBeginEdit qui se produit lorsque le mode d'édition démarre pour la cellule sélectionnée.

Private Sub YourDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles YourDataGridView.CellBeginEdit 

    Try 
     With Me.YourDataGridView 
      If .Columns(e.ColumnIndex).Name = "YourCheckBoxColumnName" AndAlso .Rows(e.RowIndex).Cells("YourQuantityColumnName").Value > 1 Then 
       e.Cancel = True 
      End If 
     End With 
    Catch ex As Exception 
    End Try 

End Sub