C# Datagridview仅显示最后一条记录,尽管在比较currentreading excel和invoice表时我有更多的行

C# Datagridview仅显示最后一条记录,尽管在比较currentreading excel和invoice表时我有更多的行,c#,.net,ado.net,oledb,C#,.net,Ado.net,Oledb,问题 Datagridview仅显示最后一条记录,尽管当currentreading excel小于currentreading invoice table时,我有更多的行 btn\U导入的else语句中的结果错误单击内部以获取循环计数 详细信息 我有以下表格: unitcode CurrentReading 12 2000 14 4500 我在sql server 2012的数据库中有表Invoice,它有以下字段 serial unitcod

问题

Datagridview仅显示最后一条记录,尽管当currentreading excel小于currentreading invoice table时,我有更多的行

btn\U导入的else语句中的结果错误单击内部以获取循环计数

详细信息

我有以下表格:

unitcode   CurrentReading
12          2000
14          4500
我在sql server 2012的数据库中有表Invoice,它有以下字段

serial     unitcode       CurrentReading   year  month
  1           12                 1000      2018   3
  3           14                 5000      2018   3
因此,如何在datagridview中显示单元代码14

实际上,我需要的是当excel工作表中单位代码的currentreading小于currentreading时 在表中,发票在datagridview中显示值。 公式如下

从excel导入的btn导入

从发票表中选择currentreading:

datagridview中的预期结果

unitcode   CurrentReading

14          4500

您是否调试了代码并确定了可能出现的问题?代码几乎不可读。请格式化,使用缩进。
public System.Data.DataTable Showdataprint()
{
    string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
    OleDbConnection con = new OleDbConnection(connectionString);
    con.Open();
    string str = @"SELECT    [UnitCode],[CurrentMeterReading] FROM  [Sheet5$] ";
    OleDbCommand com = new OleDbCommand();
    com = new OleDbCommand(str, con);
    OleDbDataAdapter oledbda = new OleDbDataAdapter();
    oledbda = new OleDbDataAdapter(com);
    DataSet ds = new DataSet();
    ds = new DataSet();
    oledbda.Fill(ds, "[Sheet5$]");
    con.Close();
    System.Data.DataTable dt = new System.Data.DataTable();
    dt = ds.Tables["[Sheet5$]"];
    return dt;
}
private decimal GetLastReading(string UnitCode)
{
    string sqlquery = "";
    sqlquery = @"select isnull((select top 1 CurrentMeterReading from WAHInvoice where UnitCode=" + UnitCode + " order by year , Serial desc),0)";
    object nullableValue = DataAccess.ExecuteScalar(sqlquery);
    decimal myValue;
    if (nullableValue == null || nullableValue == DBNull.Value)
    {
        myValue = 0;
    }
    else
    {
        decimal.TryParse(nullableValue.ToString(), out myValue);
    }
    return Utilities.ObjectConverter.ConvertToDecimal(DataAccess.ExecuteScalar(sqlquery));
}
unitcode   CurrentReading

14          4500