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.
As you can see in the notepad, it has 4 values.
- Revenue
- Sales Volume
- Gross Con
- 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?