是否可以根据列名验证列?

时间:2018-02-08 00:24:33

标签: c# excel winforms datagridview

我有一个excel验证程序,可以导入excel文件并将值放在DataGridView上。我想验证列 1月 12月,我已 ,但我想通过列名验证它们因为我使用的代码是静态的,只能从上面的列索引4中验证(1月是列索引4)。

这是一张更好的可视化图像:

enter image description here

如图所示,1月是列索引4.(0表示类型)。

以下是我每月验证的代码:

public static void Validate_Month(DataGridView dataGridView, int month, int select, string kpisel)
    {
        try
        {
            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)
            {
                sw.WriteLine("----------------------------");
                sw.WriteLine("");

                if (dataGridView.Columns.Contains("Category"))
                {


                    for (int h = select; h <= month; h++)
                    {
                        for (int i = 0; i < dataGridView.RowCount; i++)
                        {
                            if ((dataGridView[2, i].Value.ToString() == kpisel || kpisel == "" || kpisel == null) && !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("");
                                }
                            }
                        }
                    }
                }
                else if (!dataGridView.Columns.Contains("Category"))
                {
                    for (int h = select - 1; h <= month; h++)
                    {
                        for (int i = 0; i < dataGridView.RowCount; i++)
                        {


                            if ((dataGridView[2, i].Value.ToString() == kpisel || kpisel == "" || kpisel == null) && !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;

                    if (dataGridView.Columns.Contains("Category"))
                    {
                        MessageBox.Show("No errors in month of " + dataGridView.Columns[h].Name + ".");
                    }
                    else if (!dataGridView.Columns.Contains("Category"))
                    {
                        MessageBox.Show("No errors in month of " + dataGridView.Columns[h - 1].Name + ".");
                    }
                }
            }

            sw.WriteLine("----------------------------");
            sw.Flush();
            sw.Close();
        }
        catch(Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

这是调用Validate_Month方法

的方法
    public static void Validate(CheckBox cb, DataGridView dataGridView1, String month, int i, ComboBox cboColumn)
    {
        if (cb.Checked == true && dataGridView1.Columns.Contains(month) )
        {
            if (cboColumn.SelectedItem != null)
            {
                ExcelMethods.Validate_Month(dataGridView1, 4 + i, 4 + i, cboColumn.SelectedItem.ToString());
            }

            else
            {
                MessageBox.Show("Select a KPI!", "No KPI Selected!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

最后,复选框上每个标签页的动态btnValidate

 btnValidate.Click += (s, e) =>         //btnValidate Event
                        {
                            Form1.Validate(chkJan, dataGridView, "January", 0, cbo);
                            Form1.Validate(chkFeb, dataGridView, "February", 1, cbo);
                            Form1.Validate(chkMar, dataGridView, "March", 2, cbo);
                            Form1.Validate(chkApr, dataGridView, "April", 3, cbo);
                            Form1.Validate(chkMay, dataGridView, "May", 4, cbo);
                            Form1.Validate(chkJun, dataGridView, "June", 5, cbo);
                            Form1.Validate(chkJul, dataGridView, "July", 6, cbo);
                            Form1.Validate(chkAug, dataGridView, "August", 7, cbo);
                            Form1.Validate(chkSep, dataGridView, "September", 8, cbo);
                            Form1.Validate(chkOct, dataGridView, "October", 9, cbo);
                            Form1.Validate(chkNov, dataGridView, "November", 10, cbo);
                            Form1.Validate(chkDec, dataGridView, "December", 11, cbo);
                        };

1 个答案:

答案 0 :(得分:1)

您需要的是将每个列映射到特定月份的方法,反之亦然。一旦你明白了,你只需要在处理过程中使用地图。

假设月份从未被拼错,请创建一个简单的12元素数组。该指数对应于您所看到的“年度月份”。该值是可在其中找到月份的列。

然后只需查看标题即可。每次找到一个月时,请记下那个月对应的列。

对于“1月是第4行”示例,数组看起来像这样:

{4, 5, 6, 7, ... }

如果Januray是第1行,它看起来像:

{1, 2, 3, 4, 5,...}

如果第一行中的月份顺序(12月到1月)与12月相反,它将如下所示:

 { 12, 11, 10, 9, ... }

在进行实际处理时,您使用映射数组和您提取的列ID。

如果您需要支持轻微的拼写错误和国际拼写,您必须包含一个正确的Parse函数,而不是仅仅进行字符串比较以确定哪个列是哪个月。