简体   繁体   中英

Put data from text file and put ComboBox in DataGridView

I try to get data from text file what I,ve done. And next I want one of column as comboBox. I have no idea how to do that. Should I do it in opposite order? How to put ColumnComboBox in existing DataGridView?

Code to create column 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);
        }

    }

You have to add the column to the datagridview. As an example, see sample code below :

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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM