第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將列表綁定到dataGridView?

如何將列表綁定到dataGridView?

慕斯王 2019-12-02 09:53:05
我似乎在圈子里跑來跑去,并且在過去的幾個小時中一直在這樣做。我想從字符串數(shù)組中填充一個datagridview。我已經(jīng)讀過它不可能直接實現(xiàn),我需要創(chuàng)建一個自定義類型來將字符串作為公共屬性保存。所以我上了一堂課:public class FileName    {        private string _value;        public FileName(string pValue)        {            _value = pValue;        }        public string Value        {            get             {                return _value;            }            set { _value = value; }        }    }這是容器類,它只是具有帶有字符串值的屬性。當我將其數(shù)據(jù)源綁定到列表時,我現(xiàn)在想要的只是該字符串出現(xiàn)在datagridview中。我也有此方法BindGrid(),我想用它填充datagridview。這里是:    private void BindGrid()    {        gvFilesOnServer.AutoGenerateColumns = false;        //create the column programatically        DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn();        DataGridViewCell cell = new DataGridViewTextBoxCell();        colFileName.CellTemplate = cell; colFileName.Name = "Value";        colFileName.HeaderText = "File Name";        colFileName.ValueType = typeof(FileName);        //add the column to the datagridview        gvFilesOnServer.Columns.Add(colFileName);        //fill the string array        string[] filelist = GetFileListOnWebServer();        //try making a List<FileName> from that array        List<FileName> filenamesList = new List<FileName>(filelist.Length);        for (int i = 0; i < filelist.Length; i++)        {            filenamesList.Add(new FileName(filelist[i].ToString()));        }        //try making a bindingsource        BindingSource bs = new BindingSource();        bs.DataSource = typeof(FileName);        foreach (FileName fn in filenamesList)        {            bs.Add(fn);        }        gvFilesOnServer.DataSource = bs;    }最后,問題是:字符串數(shù)組填充成功,列表創(chuàng)建成功,但是我在datagridview中得到了一個空列。我也直接嘗試了datasource = list <>,而不是= bindingsource,還是一無所獲。我非常感謝您的建議,這使我發(fā)瘋。
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

使用BindingList并設置列的DataPropertyName -Property。


請嘗試以下操作:


...

private void BindGrid()

{

    gvFilesOnServer.AutoGenerateColumns = false;


    //create the column programatically

    DataGridViewCell cell = new DataGridViewTextBoxCell();

    DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn()

    {

        CellTemplate = cell, 

        Name = "Value",

        HeaderText = "File Name",

        DataPropertyName = "Value" // Tell the column which property of FileName it should use

     };


    gvFilesOnServer.Columns.Add(colFileName);


    var filelist = GetFileListOnWebServer().ToList();

    var filenamesList = new BindingList<FileName>(filelist); // <-- BindingList


    //Bind BindingList directly to the DataGrid, no need of BindingSource

    gvFilesOnServ


查看完整回答
反對 回復 2019-12-02
  • 3 回答
  • 0 關注
  • 542 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號