尝试传递 table 的第一个 DataRow 时出错 "Cannot implicitly convert type 'System.Data.DataRow' to..."
Error "Cannot implicitly convert type 'System.Data.DataRow' to..." when trying to pass the first DataRow of a table
我在 VS 2017 DS 设计器中创建了一个数据集,我正在尝试检索从 DataAdapter 返回的第一行。当我尝试在 TypesRow 中传递第一行 ([TblEventTypes.Rows[0])
时,出现错误
"Cannot implicitly convert type 'System.Data.DataRow' to
'DSEventTypes.EventTypesRow'. An explicit conversion exists (are you
missing a cast)?"
这是我的代码:
private DSEventTypesTableAdapters.EventTypesTableAdapter _EventTypesDataAdapter;
protected DSEventTypesTableAdapters.EventTypesTableAdapter EventTypesAdapter
{
get
{
if (_EventTypesDataAdapter == null)
{
_EventTypesDataAdapter = new DSEventTypesTableAdapters.EventTypesTableAdapter();
}
return _EventTypesDataAdapter;
}
}
public bool GetEventTypeByID(int EventTypeID, ref string EventType, ref string Results)
{
DSEventTypes.EventTypesRow TypesRow;
DSEventTypes.EventTypesDataTable TblEventTypes = EventTypesAdapter.GetDataById(EventTypeID);
if (TblEventTypes.Rows.Count > 0)
{
TypesRow = TblEventTypes.Rows[0];
}
return true;
}
错误出现在TypesRow = TblEventTypes.Rows[0];
行,红色错误行出现在TblEventTypes.Rows[0];
下。 “TypesRow”的声明是正确的,因为 VS 2017 智能感知确实列出了 table.
中的所有列名
我是 C# 的新手(几年前使用过 C++),多年来我一直在创建我的 Windows,Web 应用程序是 VB.NET,所以我可能遗漏了一些简单的东西,我将不胜感激我能得到的所有帮助。
尝试
TypesRow = (EventTypesRow)(tblEventTypes.Rows[0]);
我在 VS 2017 DS 设计器中创建了一个数据集,我正在尝试检索从 DataAdapter 返回的第一行。当我尝试在 TypesRow 中传递第一行 ([TblEventTypes.Rows[0])
时,出现错误
"Cannot implicitly convert type 'System.Data.DataRow' to 'DSEventTypes.EventTypesRow'. An explicit conversion exists (are you missing a cast)?"
这是我的代码:
private DSEventTypesTableAdapters.EventTypesTableAdapter _EventTypesDataAdapter;
protected DSEventTypesTableAdapters.EventTypesTableAdapter EventTypesAdapter
{
get
{
if (_EventTypesDataAdapter == null)
{
_EventTypesDataAdapter = new DSEventTypesTableAdapters.EventTypesTableAdapter();
}
return _EventTypesDataAdapter;
}
}
public bool GetEventTypeByID(int EventTypeID, ref string EventType, ref string Results)
{
DSEventTypes.EventTypesRow TypesRow;
DSEventTypes.EventTypesDataTable TblEventTypes = EventTypesAdapter.GetDataById(EventTypeID);
if (TblEventTypes.Rows.Count > 0)
{
TypesRow = TblEventTypes.Rows[0];
}
return true;
}
错误出现在TypesRow = TblEventTypes.Rows[0];
行,红色错误行出现在TblEventTypes.Rows[0];
下。 “TypesRow”的声明是正确的,因为 VS 2017 智能感知确实列出了 table.
我是 C# 的新手(几年前使用过 C++),多年来我一直在创建我的 Windows,Web 应用程序是 VB.NET,所以我可能遗漏了一些简单的东西,我将不胜感激我能得到的所有帮助。
尝试
TypesRow = (EventTypesRow)(tblEventTypes.Rows[0]);