Problem setting DataSource of a DataGridView

前端 未结 2 1235
暗喜
暗喜 2021-01-15 13:59

What happens here is when in the form opens, it shows the contextMenu and display the DataGridView on it with the value of dataSet1. But when I click the button to change th

相关标签:
2条回答
  • 2021-01-15 14:13

    It took me a while, but I found it. Controls in a ToolStripControlHost don't seem to get assigned the BindingContext carried through a regular control tree.

    You can take care of this yourself by adding the following to the first line of your SetDataSource method:

    dataGridView1.BindingContext = this.BindingContext;
    

    For fair attribution, I got the idea from this web page, where a similar situation was encountered with respect to a ComboBox. I tested it out in a sample app with your code to verify it works.

    0 讨论(0)
  • 2021-01-15 14:26

    Just changing the datasource of a control does not tell it to re-bind(refresh) its data from that new datasource. You need to execute the control's DataBind() command after you change its datasource.

    So after this:

    dataGridView1.DataSource = ds;
    

    try adding this:

    dataGridView1.DataBind();
    
    0 讨论(0)
提交回复
热议问题