【发布时间】:2018-02-08 00:24:32
【问题描述】:
我有一个 excel 验证程序,它可以导入 excel 文件并将值放在 DataGridView 上。我想验证我已经 做过的列 1 月 到 12 月,但我想通过它们的列名来验证它们因为我使用的代码是静态的,只能从上面的 Column Index 4 验证(一月是 Column Index 4)。
这是一个更好的可视化图像:
正如您在图片中看到的,一月是列索引 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);
};
【问题讨论】:
-
这看起来像应该做你想做的代码。发生了什么或没有发生什么?
-
问题是代码已经 可以满足我的要求,但是如果 Excel 文件具有不同的格式但还包含 1 月到 12 月的月份,该怎么办。比如说,1 月的列索引从 1 开始,那么它只会在 4 月之后的月份生效。
-
一个简单的数组告诉你哪一个月在哪一行应该这样做。每次在新的 Excel 文件中读取时,您都可以根据列名构建该数组。这是一个很好的例子:stackoverflow.com/questions/258793/…
-
如果您的网格数据源是
DataTable,并且如果列名称是“January”……您应该能够使用其名称引用该列。示例:dataGridView1.Rows[YourRowIndex].Cells["January"].Value.ToString()... 我只能假设 Excel 文件中的列有一个列标题名称,否则你怎么知道是哪一个月?使用上面的示例将允许“一月”在任何列号中,但是如果它不存在,上面的代码将崩溃。 -
对不起我措辞不当的评论。您应该能够参考如下所示的列:
dataGridView1.Rows[YourRowIndex].Cells["January"].Value.ToString()。这样,“一月”列可以在任何地方。您只需要在尝试引用它之前确保它存在。如果 Excel 文件没有“一月”列怎么办?...向用户显示消息比炸毁要好。
标签: c# excel winforms datagridview