DataGridViewComboBoxColumn

DataGridViewComboBoxColumn

本文介绍了无法填充在设计时创建的DataGridViewComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计时添加了一个DataGridViewComboBoxColumn,但是我想在运行时进行填充。

I have a DataGridViewComboBoxColumn being craeted at design time.But I want to polulate it at runtime. But it is not happening.

这是整个代码

public partial class Form1 : Form
    {
        private List<FileInformation> FileInformationList;
        public Form1()
        {
            InitializeComponent();
            PrepareGrid();
            DisplayResult();
        }

        private void PrepareGrid()
        {

            var fileNameColumn = new DataGridViewTextBoxColumn
            {
                Name = @"FileName",
                HeaderText = "File Name",
                DataPropertyName = @"FileName",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = false,
                Frozen = false
            };
            dataGridView1.Columns.Add(fileNameColumn);

            var downloadColumn = new DataGridViewLinkColumn

            {
                Name = @"Download",
                HeaderText = @"Download",
                DataPropertyName = @"Download",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = true
            };
            dataGridView1.Columns.Add(downloadColumn);

            var dropdownColumn = new DataGridViewComboBoxColumn

            {
                Name = @"Test",
                HeaderText = @"Country",                
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
                ReadOnly = true
            };
            dataGridView1.Columns.Add(dropdownColumn);
        }

        private void DisplayResult()
        {
            FileInformationList = LoadItems();
            dataGridView1.DataSource = FileInformationList;

            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).DataSource = GetCountryList();
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).ValueMember = "id";
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Test"]).DisplayMember = "Name";

        }

        private List<FileInformation> LoadItems()
        {
            var lstScriptInfo = new List<FileInformation>();
            for (int i = 1; i <= 5; i++)
            {
                lstScriptInfo.Add(new FileInformation { FileName = "File" + i.ToString() + ".txt" });
            }
            return lstScriptInfo;
        }

        private DataTable GetCountryList()
        {
            DataTable CountryDt = new DataTable();
            CountryDt.Columns.Add("id");
            CountryDt.Columns.Add("Name");
            CountryDt.Rows.Add("1", "Canada");
            CountryDt.Rows.Add("2", "USA");
            return CountryDt;
        }

    }

    public class FileInformation
    {
        public string FileName { get; set; }
        public string Download { get { return "Download File"; } }
    }
}

输出为

请帮助我确定问题出在哪里?

Please help me to identity what has went wrong?

推荐答案

两种实现方法:


  1. 设置列的默认空值(跳过空检查):

  1. Setting defualt null value for column (skipped null checks):

var cbColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Test"];
var ds = GetCountryList();

cbColumn.DataSource = ds;
cbColumn.ValueMember = "id";
cbColumn.DisplayMember = "Name";

cbColumn.DefaultCellStyle.NullValue = ds.Rows[0][0];
cbColumn.DefaultCellStyle.DataSourceNullValue  = ds.Rows[0][1];


  • 第二个要在 DataBinding 并手动设置:

    private void DataGridDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            var hasValue = row.Cells["Test"].Value != null;
    
            if (!hasValue)
            {
                 row.Cells["Test"].Value = 1;
            }
        }
    }
    


  • 我还要更改绑定顺序:

    private void DisplayResult()
    {
        var cbColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Test"];
        var ds = GetCountryList();
    
        cbColumn.DataSource = ds;
        cbColumn.ValueMember = "id";
        cbColumn.DisplayMember = "Name";
    
        cbColumn.DefaultCellStyle.NullValue = ds.Rows[0][0];
        cbColumn.DefaultCellStyle.DataSourceNullValue  = ds.Rows[0][1];
    
        FileInformationList = LoadItems();
        dataGridView1.DataSource = FileInformationList;
    }
    

    这篇关于无法填充在设计时创建的DataGridViewComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-24 06:57