如何编写在数据网格完成渲染后运行的代码?

时间:2012-08-05 17:18:09

标签: winforms user-controls datagridview onload

以下更新自定义列的代码" hello"当网格在表单上并且此代码在form_load上时,它可以正常工作。但是当网格在用户控件上并且代码在UserControl1_Load上时,它不会更新网格(除非我在按钮点击内添加此代码等) - 我责备微软!

我试图找到一个解决方法,在网格渲染完成后运行该代码,因为它可以工作(例如当我在button_click事件上写它时),任何想法?

List<MyClass> all_customers = new List<MyClass>();

   all_customers.Add(new MyClass() { MyProperty = 2, MyProperty2 = "33" });
   all_customers.Add(new MyClass() { MyProperty = 2, MyProperty2 = "33" });
   all_customers.Add(new MyClass() { MyProperty = 2, MyProperty2 = "33" });

   dataGridView1.DataSource = all_customers;

   DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
   c.Name = "hello";
   c.HeaderText = "hello";
   dataGridView1.Columns.Add(c);
   dataGridView1.Rows[0].Cells["hello"].Value = "text2";

1 个答案:

答案 0 :(得分:0)

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  {
   dataGridView1.Rows[0].Cells["hello"].Value = "pp";

  }

这就是诀窍。从某种原因来看,它之前并不适用于我,但是当我从所有复杂的代码行中删除代码时,它确实有效。哦,好吧