C# Combox控件如何绑定自定义数据

创建一个Combox控件为cbxSelectPilot,加载指定目录中的文件名到cbxSelectPilot中

di = new DirectoryInfo(defaultPilotParFolder);
            fileInfo = di.GetFiles("*.json");
            SortAsFileCreationTime(ref fileInfo);
            pilotPathList = new List<string>();
            foreach (var item in fileInfo)
            {
                string filename = item.ToString().Substring(item.ToString().LastIndexOf("\\") + 1, item.ToString().Length - item.ToString().LastIndexOf("\\")-1);
                pilotPathList.Add(filename);//添加到list中  
            }

 DataTable dt = new DataTable();

            dt.Columns.Add("name");
            dt.Columns.Add("value");
            for (int i = 0; i < pilotPathList.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = pilotPathList[i];
                dr[1] = i.ToString();
                dt.Rows.Add(dr);
            }
            cbxSelectPilot.DataSource = dt;
            cbxSelectPilot.DisplayMember = "name";
            cbxSelectPilot.ValueMember = "value";
            cbxSelectPilot.SelectedIndex = 0;

猜你喜欢

转载自blog.csdn.net/lwjzjw/article/details/80568408