本文介绍了在同一个DataGridView列中的控件不会在初始化网格时呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 GOOD BAD GOOD BAD 我正在托管不同的控件在DataGridView列。当我同时添加控件时,我初始化网格,控件显示为文本框(BAD)。如果我在DataGridView初始化后添加控件,控件正确呈现(GOOD)。I'm hosting different controls in DataGridView column. When I add controls at the same time I initialize the grid the controls show as Textboxes (BAD). If I add the controls after the DataGridView has been initialized controls render correctly (GOOD).public Form1(){ InitializeComponent(); ControlsInGridViewColumn(); //<- renders controls as textboxes}private void button1_Click(object sender, EventArgs e){ ControlsInGridViewColumn(); //<- does correctly render controls}private void ControlsInGridViewColumn(){ DataTable dt = new DataTable(); dt.Columns.Add("name"); for (int j = 0; j < 10; j++) { dt.Rows.Add(""); } this.dataGridView1.DataSource = dt; this.dataGridView1.Columns[0].Width = 200; DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell(); ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" }); this.dataGridView1[0, 0] = ComboBoxCell; this.dataGridView1[0, 0].Value = "bbb"; DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell(); this.dataGridView1[0, 1] = TextBoxCell; this.dataGridView1[0, 1].Value = "some text"; DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell(); CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; this.dataGridView1[0, 2] = CheckBoxCell; this.dataGridView1[0, 2].Value = true;} 在初始化过程中如何使控件正确呈现? strong> How does one make the controls render correctly during initialization? (我动态添加DataGridViews)。 更新: Jacob Seleznev的答案适用于表单但不是用户控件,这是我需要的...这是如何重现:UPDATE: Jacob Seleznev's answer works for Forms but not user controls which is what I need... Here is how to reproduce it:在表单:private void button3_Click(object sender, EventArgs e){ this.Controls.Add(new userCtrl());}用户控制:public partial class userCtrl : UserControl{ public userCtrl() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { ControlsInGridViewColumn(); base.OnLoad(e); } public void ControlsInGridViewColumn() { //same code as above }} 推荐答案这对我有用: protected override void OnLoad(EventArgs e) { ControlsInGridViewColumn(); //<- does correctly render controls base.OnLoad(e); } 这篇关于在同一个DataGridView列中的控件不会在初始化网格时呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 14:07