从Excel读取数据未显示double
字段的纬度和经度。我尝试过同时使用string
和Double
。如果我使用string
,我会收到错误:
无法投射“System.Double”类型的对象。输入' System.String'。
如果我使用double?
我没有收到任何错误,并且DataGridView
中的字段显示为空白。
private struct GIS
{
public string AccountNo;
public string MeterNo;
public string Mobile;
public string Name;
public string GeoCode;
public string Welaiyh;
public string Area;
public string Region;
public string Xcord;
public string Ycord;
public Double? Longitude;
public Double? Latitude;
//public string Longitude;
//public string Latitude;
public DateTime? Createddate;
public string Status;
}
List<GIS> xl = xlDT.AsEnumerable().Select(g => new GIS()
{
AccountNo = g.Field<string>("Account No"),
MeterNo = g.Field<string>("Meter No#"),
Mobile = g.Field<string>("Mobile "),
Name = g.Field<string>(" Name"),
GeoCode = g.Field<string>("Geo Code"),
Welaiyh = g.Field<string>("Welaiyh"),
Area = g.Field<string>("Area"),
Region = g.Field<string>("Region"),
Xcord = g.Field<string>("X-Coordinate"),
Ycord = g.Field<string>("Y-Coordinate"),
Longitude = g.Field<Double?>("Longitude")??0,
Latitude = g.Field<Double?>("Latitude") ??0,
Createddate = g.Field<DateTime?>("Created On"),
Status = g.Field<string>("Status (Account No#)")
}).ToList();
答案 0 :(得分:0)
您需要做的是在尝试分配之前测试该字段为null。所以请改用它(假设您要显示0为null):
Longitude = g.IsNull("Longitude") ? 0.0 : g.Field<Double>("Longitude"),
Latitude = g.IsNull("Latitude") ? 0.0 : g.Field<Double>("Latitude"),