从文本文件中放入数据并将 ComboBox 放入 DataGridView

Posted

技术标签:

【中文标题】从文本文件中放入数据并将 ComboBox 放入 DataGridView【英文标题】:Put data from text file and put ComboBox in DataGridView 【发布时间】:2014-03-09 12:46:59 【问题描述】:

我尝试从我所做的文本文件中获取数据。接下来我想要一列作为组合框。我不知道该怎么做。我应该按相反的顺序做吗?如何将 ColumnComboBox 放到已有的 DataGridView 中?

创建列 ComboBox 的代码:

private void FillUslugaComboBoxDataGridView()
    
        try
        
            StreamReader sr = new StreamReader(@"C:\Users\Radek\Documents\Visual Studio 2010\Projects\Salon Fryzjerski\Salon Fryzjerski\Salon Fryzjerski\Baza\Uslugi.txt");
            string line = sr.ReadLine();
            DataGridViewComboBoxColumn usluga = new DataGridViewComboBoxColumn();
            if (line != null)
            
                while (line != null)
                
                    usluga.Items.Add(line);
                    line = sr.ReadLine();

                
            
            else
            

                usluga.Items.Add("Error to fill, column is null");
            

        
        catch (Exception ex)
        
            MessageBox.Show(ex.Message);
        

    

【问题讨论】:

【参考方案1】:

您必须将该列添加到 datagridview。例如,请参见下面的示例代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SOWinForm

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        
        private DataGridView DataGridView1;
        private void Form1_Load(object sender, EventArgs e)
        
            DataGridView1 = new DataGridView();
            var column = CreateComboBoxColumn();
            SetAlternateChoicesUsingDataSource(column);
            DataGridView1.Columns.Add(column);
            Controls.Add(DataGridView1); 
        

        private DataGridViewComboBoxColumn CreateComboBoxColumn()
        
            DataGridViewComboBoxColumn column =
                new DataGridViewComboBoxColumn();
            
                column.DataPropertyName = "Name";
                column.HeaderText = "Name";
                column.DropDownWidth = 150;
                column.Width = 100;
                column.MaxDropDownItems = 3;
                column.FlatStyle = FlatStyle.Flat;
            
            return column;
        

        private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
        
            
                comboboxColumn.DataSource = RetrieveNames();
                comboboxColumn.ValueMember = "Name";
                comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
            
        

        private List<Student> RetrieveNames()
        
            return new List<Student>()  new Student()  Name = "Rohan" , new Student()  Name = "Ram"  ;
        

    

    public class Student
    
        public string Name  get; set; 
    

【讨论】:

以上是关于从文本文件中放入数据并将 ComboBox 放入 DataGridView的主要内容,如果未能解决你的问题,请参考以下文章

java的简单问题

从文本文件中读取数据并将数据插入到数组中

在 DataGridView 中导入文本文件,C#

C - 读取文件并将文本放入具有动态内存分配的字符指针

如何将mysql数据放入弹出窗口?

Qt Gui工程中放入一个普通的C++程序却不能正常用cin,例子在下面。