我创建了一个相当简单的表格;它会打开一个 Excel 文件,并根据该电子表格的内容填充一个数据表。完成版本 1,经过测试、调试 - 它工作正常。一旦加载功能完成,我表单上的 datagridview 就会显示数据。
版本 2 需要一个“验证”按钮,其背后有逻辑,所以我补充说,测试,调试......并注意到数据不再出现在 datagridview 中。我看不到我为更改加载逻辑所做的任何事情,也看不到表定义等。
所以,我恢复到版本 1 的保存副本(为什么,是的,我以前做过 - 嘿!);它仍然像以前一样工作,在加载结束时显示数据。
一旦我向表单添加任何控件,数据就不再显示。有趣的是,我还在 Form1.Designer.cs 中收到了一堆警告:从未使用过“CSVScanApp.Form1.FileName”字段 有趣/巧合的是,数据表中的每一列都会重复。
我知道你会想看代码,所以这里是:
public Form1()
{
try
{
InitializeComponent();
cxApp = new cExcel.Application();
dsScanRows = new DataSet();
dtScanTable = dsScanRows.Tables.Add();
dtScanTable.Columns.Add("FileName", typeof(string));
dtScanTable.Columns.Add("Company", typeof(string));
dtScanTable.Columns.Add("LeaseNo", typeof(string));
dtScanTable.Columns.Add("DocDate", typeof(string));
dtScanTable.Columns.Add("Function", typeof(string));
dtScanTable.Columns.Add("Category", typeof(string));
dtScanTable.Columns.Add("DocType", typeof(string));
dtScanTable.Columns.Add("Integration", typeof(string));
dtScanTable.Columns.Add("ScanDate", typeof(string));
// Category "codes" and their corresponding full names
CtgyDict.Add("DataSheet", "Data Sheet");
CtgyDict.Add("Surface", "Surface/ROW/Pipeline Agreements");
CtgyDict.Add("TitleReport", "Title Report");
CtgyDict.Add("MapPlats", "Map & Plats");
}
catch (Exception ex)
{
MessageBox.Show("Error(s) encountered:" + crlf + ex.Message);
}
finally
{
}
}
以及来自加载按钮的逻辑,它实际上将 Excel 解析到数据表中:
private void btnLoad_Click(object sender, EventArgs e)
{
try
{
cxWB = cxApp.Workbooks.Open(tbSourceFile.Text, 0, true, 5, "", "", true,
cExcel.XlPlatform.xlWindows, "", false, false, 0, false, 1, 0); // open Read-Only
cExcel.Sheets cxSheets = cxWB.Worksheets;
cExcel._Worksheet cxSheet = (cExcel._Worksheet)cxSheets.get_Item(1); // Sheet 1
cExcel.Range cxRange = cxSheet.UsedRange;
int iRowMax = cxRange.Rows.Count;
int iRow;
string sFileName, sLease,sDocDate,sCategory,sCtgyName,sDocType;
Double dblDocDate;
string sCompany = tbCompany.Text; // specs said "CompanyNN", but were wrong...
string sScanDate = tbScanDate.Text; // hope it's correctly entered... :p
dataGridView1.DataSource = null; // reset from any previous data
dtScanTable.Clear(); // make sure this starts empty
// loop through each row in the source
for (iRow = 2; iRow <= iRowMax; iRow++)
{
sFileName = (string)(cxRange.Cells[iRow, iSourceColFileName] as cExcel.Range).Value2;
if (sFileName == null | sFileName == "")
{ } // ignore blank rows
else
{
sLease = sFileName.Split('_')[0]; // e.g., 100845.00A_1.pdf --> 100845.00A
dblDocDate = Convert.ToDouble((cxRange.Cells[iRow, iSourceColDocDate] as cExcel.Range).Value2);
sDocDate = DateTime.FromOADate(dblDocDate).ToShortDateString();
//if (sDocDate == "") { sDocDate = sScanDate; } // default to today???
sCategory = (string)(cxRange.Cells[iRow, iSourceColCtgy] as cExcel.Range).Value2;
try
{
sCtgyName = CtgyDict[sCategory];
}
catch
{
sCtgyName = sCategory; // i.e., not in the replacement list
}
sDocType = (string)(cxRange.Cells[iRow, iSourceColDocType] as cExcel.Range).Value2;
dtScanTable.Rows.Add(sFileName,sCompany,sLease,sDocDate,sFunction,sCtgyName,sDocType,sIntegration,sScanDate);
}
}
dataGridView1.DataSource = dtScanTable;
btnValidate.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("Error(s) encountered:" + crlf + ex.Message);
btnSave.Enabled = false;
}
}
而且,在 Form1.Designer.cs 中,这些是与警告相关联的行,这些行仅在我向表单添加控件后才会显示:
private System.Windows.Forms.DataGridViewTextBoxColumn FileName;
private System.Windows.Forms.DataGridViewTextBoxColumn Company;
private System.Windows.Forms.DataGridViewTextBoxColumn LeaseNo;
private System.Windows.Forms.DataGridViewTextBoxColumn DocDate;
private System.Windows.Forms.DataGridViewTextBoxColumn Function;
private System.Windows.Forms.DataGridViewTextBoxColumn Category;
private System.Windows.Forms.DataGridViewTextBoxColumn DocType;
private System.Windows.Forms.DataGridViewTextBoxColumn Integration;
private System.Windows.Forms.DataGridViewTextBoxColumn ScanDate;
如果相关,则在该块之后添加与新验证按钮对应的新行。
我显然在我的头上,在这里; 我以某种方式,在某个地方做了一些事情——但根本无法想象是什么,也无法想象如何克服这一点。(幸运的是,原始版本和修改版本都创建了我需要的输出,所以这并不紧急 - 但它让我发疯,网格不会显示我的数据!)
谢谢!