简体   繁体   English

如何在datagridview上验证所选组合框?

[英]How to validate selected combobox on datagridview?

I am currently making an excel validation program. 我目前正在制作一个excel验证程序。

I want to validate the selected KPI values under a specific month. 我想在特定月份下验证所选的KPI值。 Say, the selected KPI column box is "SALES VOLUME" and the checkbox that is checked is "JANUARY" , only SALES VOLUME KPI under JANUARY shall be only validated. 假设所选的KPI列框为“ SALES VOLUME” ,并且选中的复选框为“ JANUARY” ,则仅将验证JANUARY下的SALES VOLUME KPI。

Result example should be like this : 结果示例应如下所示:

A textfile would pop out showing this values on the selected KPI Combo Box and checkbox of the month. 将会弹出一个文本文件,在每月的选定KPI组合框和复选框上显示此值。

KPI: Sales Volume KPI:销量

Category: Macau & Shipstore 类别:澳门及船屋

Month: January 月:一月

Value: 1283091823 价值:1283091823

Only the KPI SALES VOLUME from the month January shall be validated. 仅从一月份开始的KPI 销售量将被验证。

For reference, here's an image of the UI . 供参考,这是UI图像

My codes are as follows: 我的代码如下:

From ExcelMethods Class: 从ExcelMethods类:

This method validates each month depending on the checkbox checked. 此方法根据选中的复选框每月验证一次。

 public void Validate_Month(DataGridView dataGridView, int month, int select)
    {
        int kpi = 2;
        int category = 3;
        decimal num;

        FileStream fs = new FileStream(@"C:\brandon\InvalidColumnsByMonth.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);

        sw.BaseStream.Seek(0, SeekOrigin.End);

        StringBuilder sb = new StringBuilder();
        if (dataGridView.ColumnCount > 3)
        {
            for (int h = select; h <= month; h++)
            {
                for (int i = 0; i < dataGridView.RowCount; i++)
                {
                    if (!Decimal.TryParse(dataGridView[h, i].Value.ToString(), out num))
                    {
                        if (dataGridView[h, i].Value.ToString() == null || dataGridView[h, i].Value.ToString() == "") 
                        {

                        }
                        else
                        {
                            sb.AppendLine("[KPI]: " + dataGridView.Rows[i].Cells[kpi].Value.ToString()); 
                            sb.AppendLine("[Category]: " + dataGridView.Rows[i].Cells[category].Value.ToString());
                            sb.AppendLine("[Month]:" + dataGridView.Columns[h].Name.ToUpper());
                            sb.AppendLine("[VALUE]:  " + dataGridView[h, i].Value.ToString() + "");
                            sb.AppendLine("");

                            sw.WriteLine("[KPI]: " + dataGridView.Rows[i].Cells[kpi].Value.ToString());
                            sw.WriteLine("[Category]: " + dataGridView.Rows[i].Cells[category].Value.ToString());
                            sw.WriteLine("[Month]:" + dataGridView.Columns[h].Name.ToUpper());
                            sw.WriteLine("[VALUE]: {" + dataGridView[h, i].Value.ToString() + "}");
                            sw.WriteLine("");

                        }
                    }
                }
            }

            if (sb.Length != 0 )
            {
                MessageBox.Show(sb.ToString());
                Process.Start(@"C:\brandon\InvalidColumnsByMonth.txt");

            }

            else
            {
                int h = select;

                MessageBox.Show("No errors in month of " + dataGridView.Columns[h].Name + ".");
            }

            sw.Flush();
            sw.Close();

        }
    }

From my Form 1 Class 从我的中一班上

This is for reference ExcelMethods method, Validate_Month 仅供参考ExcelMethods方法Validate_Month

 public void Validate(CheckBox cb, DataGridView dataGridView1, String month, int i)
    {
        if (cb.Checked == true && dataGridView1.Columns.Contains(month) )
        {
            ExcelMethods.Validate_Month(dataGridView1, 4 + i, 4 + i);
        }
    }

and lastly, from Form 1 Class is the btnValidateAll_MouseClick method 最后,在Form 1 Class中是btnValidateAll_MouseClick方法

 private void btnValidate_MouseClick(object sender, MouseEventArgs e)
    {
        Validate(checkBox1, dataGridView1, "January", 0);
        Validate(checkBox2, dataGridView1, "February", 1);
        Validate(checkBox3, dataGridView1, "March", 2);
        Validate(checkBox4, dataGridView1, "April", 3);
        Validate(checkBox5, dataGridView1, "May", 4);
        Validate(checkBox6, dataGridView1, "June", 5);
        Validate(checkBox7, dataGridView1, "July", 6);
        Validate(checkBox8, dataGridView1, "August", 7);
        Validate(checkBox9, dataGridView1, "September", 8);
        Validate(checkBox10, dataGridView1, "October", 9);
        Validate(checkBox11, dataGridView1, "November", 10);
        Validate(checkBox12, dataGridView1, "December", 11);

    }

You want to use the validating method to do grid validation. 您要使用验证方法进行网格验证。 Make sure the grid has the event attached. 确保网格已附加事件。

e has the column and row index property. e具有列和行索引属性。 You can set the e.Cancel to true to assign it to not validated. 您可以将e.Cancel设置为true以将其分配给未验证。

private void dataGridView_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{

    // Gets Your Row & Column, 4 is Jan in this case
    if ( e.ColumnIndex == 4 && e.RowIndex >= 0 )
    {

        // Do Your Validation
        // If False. Set 
        // e.Cancel = true;


    }
}

Or based on your definition of validate: 或根据您对validate的定义:

Change your method definition: 更改您的方法定义:

public void Validate_Month(DataGridView dataGridView, int month, int select, string kpi)

Change your validation: 更改您的验证:

if ((dataGridView[2, i].Value.ToString() == kpi || kpi == "" || kpi == null) &&  !Decimal.TryParse(dataGridView[h, i].Value.ToString(), out num))

This way it'll only match on the matching kpi. 这样,它将仅在匹配的kpi上匹配。 I've set that up so it'll also match all rows if kpi is passed in as blank or null. 我已经进行了设置,因此如果kpi作为空白或null传入,它也将匹配所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM