【问题标题】:DataGridViewComboBoxCell show row value as defaultDataGridViewComboBoxCell 默认显示行值
【发布时间】:2019-04-30 12:47:03
【问题描述】:

我想在我的DataGridView 中的任何行的特定列中放置一个DataGridViewComboBoxCell。 现在我有一个问题......我想在我放置DataGridViewComboBoxCell的特定列中显示一个默认值,但没有显示任何内容..

String[] options = new String[] {"On", "Off" };

for(int i = 0; i< rows.Count; i++)
{
     // lets say one row looks like this {"1","On"}, {"2", "Off"}
     MydataGridView.Rows.Add(rows[i]);

     DataGridViewComboBoxCell MyComboBox= new DataGridViewComboBoxCell();
     MyComboBox.Items.AddRange(options);

     MydataGridView.Rows[i].Cells[1] = MyComboBox;

}

在我可以通过DataGridViewComboBoxCell 选择{"On","Off"} 状态之前,我想将我添加的行显示为默认值。

我希望它已经足够清楚了。 :)

【问题讨论】:

    标签: c# winforms datagridview default datagridviewcomboboxcell


    【解决方案1】:

    这个呢(DataGridView.CellFormatting Event):

    public Form1()
    {
        InitializeComponent();
        MydataGridView.CellFormatting += MydataGridView_CellFormatting;
    }
    
    private void MydataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 1) //Index of your DataGridViewComboBoxCell 
        {
            e.Value = "yourDefaultValue";
        }
    }
    

    我也看到了以下事件,它也用于设置默认值(DataGridView.DefaultValuesNeeded Event):

    public Form1()
    {
        InitializeComponent();
        MydataGridView.DefaultValuesNeeded += MydataGridView_DefaultValuesNeeded;
    }
    
    private void MydataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        e.Row.Cells[1].Value = "yourDefaultValue";
    }
    

    【讨论】:

    • 哦哈哈 :D 这是一个不错的属性。它工作正常,谢谢:) 它比我自己解决它更好。
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2020-08-22
    • 2020-08-16
    • 2019-09-10
    • 2017-09-27
    相关资源
    最近更新 更多