簡體   English   中英

如何通過IList中的列名獲取列的索引?

[英]How do you get the index of a column by the column name in an IList?

如果這碰巧是一個簡單的問題,我深表歉意,但是我是編碼新手。 幾天來一直斷斷續續地把我的頭撞在牆上,並進行了大量的互聯網搜索,但是我總是空手而歸。

我已經將數據從數據庫視圖捕獲到IList中。 因為數據來自視圖,並且可能(有可能)改變需要更改(添加列等)的列的顯示順序,所以我想以編程方式通過列名從IList中提取列的索引。

使用gridview可以很容易地完成它,因為它具有一個不錯的“ HeaderRow”屬性,該屬性使我可以編寫一些代碼以遍歷任何行並提取headerRow名稱,將其與我要查找的名稱進行比較,然后找到它時,拉一個遞增的計數數字,然后瞧!然后我有了索引。

問題是,我試圖避免不得不從已經從實體框架復制的列表中復制數據,而該實體框架是從數據庫本身復制到另一個容器(gridview)中的,以便進行操作。 似乎它變得越來越復雜,涉及gridview。

因此,如果有人可以告訴我是否可以使用IList類的有用屬性通過列名獲取列索引#,請告訴我,非常感謝!

編輯:

回應nvoigt的評論“一個IList對列一無所知”。

我確定它一定有一些主意,因為當我單步執行代碼並查看IList中的第一行數據時,它在那里具有列名(“ AppAnswer”等)。 我想要的是通過某種方式查看它們並(通過屬性或代碼)確定列名所在的索引的方式。 參見圖片以進行澄清。
我必須在這里鏈接它,因為我的代表不夠高才能發布照片: https : //www.dropbox.com/s/2xg3nduhqnzbl8n/ILisst.JPG

另外,這是從視圖捕獲數據並將其放入列表的代碼:

IList<View_AppQnA_All> list1 = useful.GetQuestionAnswerListForJobPosition(1);

public IList<View_AppQnA_All> GetQuestionAnswerListForJobPosition(int jobID)
{
    IList<View_AppQnA_All> QuestionAnswerList = new List<View_AppQnA_All>();

    IQueryable<View_AppQnA_All> view = Repository.Current.ObjectContext.
                View_AppQnA_All.Where(Q => Q.AppQuestionForJobPosition == jobID);
    QuestionAnswerList = view.ToList<View_AppQnA_All>();

    return QuestionAnswerList;
}

#region Imports
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
#endregion

namespace AOJobApplication
{
    class Repository
    {
        #region Declarations

        private static Repository m_instance;
        private static AOApplicationContext CWEntities;
        // Lock synchronization object
        //private static object syncLock = new object();
        private static readonly object syncLock = new object();

        #endregion

        #region Properties

        /// <summary>
        /// Returns the one and only Repository
        /// </summary>
        public static Repository Current
        {
            get
            {
                if (m_instance == null)
                {
                    lock (syncLock)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new Repository();
                        }
                    }
                }

                return m_instance;
            }
        }

        #endregion

        /// <summary>
        /// Object context to get entity objects application void
        /// </summary>
        public AOApplicationContext ObjectContext
        {
            get
            {
                if (CWEntities != null)
                {
                    return CWEntities;
                }
                else
                {
                    CWEntities = new AOApplicationContext();

                    return CWEntities;
                }
            }
        }
    }
}

您存儲在列表中的對象具有根據視圖建模的屬性。 類中的屬性沒有任何順序。 它們只是屬性,它們彼此相鄰存在。 您可以按照自己喜歡的任何方式對其進行排序,Visual Studio調試器似乎按名稱對其進行排序。

這些類是生成的,不是動態生成的。 如果您的視圖發生了巨大變化(我想不僅僅是按領域順序),您將不得不再次生成它們。 這意味着重新編譯。

暫無
暫無

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

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