-1

I am doing a validation on a certain column called "KPI". What I'm doing is making a .txt file that will be used for validation of the column.

The .txt file looks like this.


enter image description here


As you can see in the notepad, it has 4 values.

  1. Revenue
  2. Sales Volume
  3. Gross Con
  4. Brand Con

Anything other than that, the validation would say, "is NOT valid".

Here is the code I used to get the values from the .txt file


        List<string> fields = new List<string>();

        var lines = File.ReadAllLines(@"\\SMITSHOME01\Home_Folder_1$\vhernandez\My Documents\Visual Studio 2013\validate.txt");
        for (int i = 0; i < lines.Length; i++)
        {
            fields.Add(lines[i]);

        }

But I am not very good in looping so I'm asking if someone could help me do it. For now, what I'm doing is this.

 for (int i = 0; i < dataGridView.RowCount; i++)
            {

                //MessageBox.Show(dataGridView.Rows[i].Cells["KPI"].Value.ToString()); 
                if (dataGridView.Rows[i].Cells["KPI"].Value.ToString().Contains(fields[i]))
                {
                    //sb.AppendLine(dataGridView.Rows[i].Cells["KPI"].Value.ToString() + " is Valid.");
                }

                else if (dataGridView.Rows[i].Cells["KPI"].Value.ToString() == null || dataGridView.Rows[i].Cells["KPI"].Value.ToString() == "" || dataGridView.Rows[i].Cells["KPI"].Value.ToString() == "KPI" || dataGridView.Rows[i].Cells["KPI"].Value.ToString() == "Category")
                {

                }
                else
                {
                    //MessageBox.Show("Row not decimal:" + " [ " + dataGridView[h, i].Value.ToString() + "] in column "  + dataGridView.Columns[h].Name);
                    sb.AppendLine(dataGridView.Rows[i].Cells["KPI"].Value.ToString() + " is **NOT** Valid.");
                }
            }

I am using .Contains() for the validation but I always get System.ArgumentOutOfRangeException because of my inefficiency with for loops.

I want to dynamically add fields[] in the if .Contains() or somehow reference it, can someone help me?

Here's the picture of the form for reference:


enter image description here

Shan Coralde
  • 214
  • 2
  • 15

1 Answers1

1

I think your check in if condition should be other way around :

if(fields.Contains(dataGridView.Rows[i].Cells["KPI"].Value.ToString()))
rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
  • 1
    @ShanCoralde : np. on additional note : can't you bind your grid with strongly typed list or something of that sort ? that would make your much code cleaner. – rahulaga-msft Feb 23 '18 at 01:25
  • Care to explain? I'm fairly new to C# and on a beginner level. – Shan Coralde Feb 23 '18 at 01:28
  • Sure. I believe you binding gridView to some data source, right ? what is the type of that source ? For example if you reading say from SQL using ado.net, you can better populate some collection (say List) and bind that to gridView. That will help you to make use of beautiful linq features rather than having loops and all. – rahulaga-msft Feb 23 '18 at 01:37
  • The data source is from an excel file I've imported to the `DataGridView`. – Shan Coralde Feb 23 '18 at 02:04