2017-10-06 21 views
0

グリッド行の「数量」が「0」の場合、グリッド行のチェックボックスを選択しないようにするにはどうすればよいですか。 私は以下のコードを持っていますが、1行が選択されている限り、Qty値を変更せずに2行目などを選択できるので、それでも可能です。 上記の各ボタンから選択した行は、form2に送信されます。qtyが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(); 


      } 
     } 
    } 

画面:あなたが編集モードを選択したセルのために起動したときに発生CellBeginEditイベントを使用することができます

form 1

sheet 1

+0

「Form2」のスクリーンショットが追加されたようですね。あなたが 'Form2'を修正するのを手助けする人はいないでしょう。もしあなたがそれについての一つの詳細を含んでいなければ(それに含まれているグリッド定義など)、 [質問を編集](https://stackoverflow.com/posts/46600500/edit)し、 'Form2'のグリッドとその行チェックボックスがどのように/どこに定義されているかについて十分な詳細を追加してください。 –

+0

HI。スクリーンショットは、form1のdatagridview1です。このフォームから選択されたチェックボックスは、フォーム2に送信されます。しかし、ほとんどのユーザーはチェックボックスを選択しますが、何らかの形で「数量」の値を変更することを忘れてしまいます。 – shabs

+0

あなたのタイトルと本文の違いに注意してください。 "qtyは1より大きくない"( 'qty <= 1')と" Qty "が" 0 ""( 'qty <1')です。 – Flater

答えて

0

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 
関連する問題