如果数量不大于1,则选中停止复选框

时间:2017-10-06 07:29:30

标签: c# winforms

如果该网格行的“数量”为“0”,如何阻止用户选择网格行的复选框。 我有下面的代码,但它仍然允许,因为只要选择了一行,它就允许选择第二行,依此类推而不更改Qty值。 然后将上面每个按钮的选定行发送到form2。

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();


            }
        }
    }

屏幕:

form 1

sheet 1

1 个答案:

答案 0 :(得分:0)

您可以使用在所选单元格的编辑模式启动时发生的CellBeginEdit事件。

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