簡體   English   中英

嘗試傳遞表的第一個 DataRow 時出現錯誤“無法將類型 'System.Data.DataRow' 隱式轉換為...”

[英]Error "Cannot implicitly convert type 'System.Data.DataRow' to..." when trying to pass the first DataRow of a table

我在 VS 2017 DS 設計器中創建了一個數據集,我正在嘗試檢索從 DataAdapter 返回的第一行。 當我嘗試傳遞([TblEventTypes.Rows[0])的第一行([TblEventTypes.Rows[0]) ,出現錯誤

“無法將類型‘System.Data.DataRow’隱式轉換為‘DSEventTypes.EventTypesRow’。存在顯式轉換(您是否缺少演員表)?”

這是我的代碼:

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];下出現紅色錯誤行TblEventTypes.Rows[0]; . “TypesRow”的聲明是正確的,因為 VS 2017 智能感知確實列出了表中的所有列名。

我是 C# 的新手(多年前使用 C++),多年來我一直在創建我的 Windows 和 Web 應用程序是 VB.NET,所以我可能缺少一些簡單的東西,我會很感激我能得到的所有幫助。

嘗試

TypesRow = (EventTypesRow)(tblEventTypes.Rows[0]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM