2010-03-12 59 views
4

我利用张贴加斯帕棕榈这里的代码不显示数据:Make user control display outside of form boundry的DataGridView在ToolStripDropDown

/// <summary> 
/// A simple popup window that can host any System.Windows.Forms.Control 
/// </summary> 
public class PopupWindow : System.Windows.Forms.ToolStripDropDown 
{ 
    private System.Windows.Forms.Control _content; 
    private System.Windows.Forms.ToolStripControlHost _host; 

    public PopupWindow(System.Windows.Forms.Control content) 
    { 
     //Basic setup... 
     this.AutoSize = false; 
     this.DoubleBuffered = true; 
     this.ResizeRedraw = true; 

     this._content = content; 
     this._host = new System.Windows.Forms.ToolStripControlHost(content); 

     //Positioning and Sizing 
     this.MinimumSize = content.MinimumSize; 
     this.MaximumSize = content.Size; 
     this.Size = content.Size; 
     content.Location = Point.Empty; 

     //Add the host to the list 
     this.Items.Add(this._host); 
    } 
} 

我把它翻译到VB:

Public Class PopupWindow 
    Inherits System.Windows.Forms.ToolStripDropDown 

    Private _content As System.Windows.Forms.Control 
    Private _host As System.Windows.Forms.ToolStripControlHost 

    Public Sub New(ByVal content As System.Windows.Forms.Control) 

     Me.AutoSize = False 
     Me.DoubleBuffered = True 
     Me.ResizeRedraw = True 

     Me._content = content 
     Me._host = New System.Windows.Forms.ToolStripControlHost(content) 

     Me.MinimumSize = content.MinimumSize 
     Me.MaximumSize = content.MaximumSize 
     Me.Size = content.Size 
     content.Location = Point.Empty 

     Me.Items.Add(Me._host) 

    End Sub 

End Class 

它的伟大工程同一个PictureBox显示其信息。但出于某种原因,我无法让DataGridView在弹出窗口中显示任何内容。

如果我从弹出窗口中拉出网格,它会显示其所有信息。如果我在调试期间暂停,网格显示它包含所有数据。它只是没有显示任何东西。

有没有人有任何想法?

回答

1

我一直无法重现您的问题。你能提供更多的代码吗?我在VS2010 RC(.NET 4)和VS2008(.NET 3.5)一直在测试这个代码在两个:

public partial class Form1 : Form 
{ 
    public class Person 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string PhoneNumber { get; set; } 
    } 

    List<Person> _People; 

    public Form1() 
    { 
     InitializeComponent(); 

     _People = new List<Person>(); 
     _People.Add(new Person() { FirstName = "John", LastName = "Smith", PhoneNumber = "123-456-7890" }); 
     _People.Add(new Person() { FirstName = "Jane", LastName = "Doe", PhoneNumber = "098-765-4321" }); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     pictureBox1.Image = Image.FromFile("barcode.png"); 
     pictureBox1.Location = new Point(-1000, -1000); 

     dataGridView1.DataSource = _People; 
     dataGridView1.Location = new Point(-1000, -1000); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     PopupControl popup = new PopupControl(pictureBox1); 
     popup.Show(new Point(this.Location.X - 128, this.Location.Y)); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     PopupControl popup = new PopupControl(dataGridView1); 
     popup.Show(new Point(this.Location.X - 128, this.Location.Y)); 

     //optionally change the items in the data source 
     _People.Add(new Person() { FirstName = "NewFirst", LastName = "NewLast", PhoneNumber = "123-333-3322" }); 

     //reset the bindings 
     bindingSource1.DataSource = _People; 
     bindingSource1.ResetBindings(true); 
    } 
} 

这里是什么样子: alt text http://img534.imageshack.us/img534/1640/popupcontrolwithgrid.jpg

在设计,您应该设置BindingSource并将其分配为DataGridView的DataSource。

+0

我给你的代码一个镜头,它的工作方式很棒。但是我做了一些修改,使其工作更像我的情况,而那时我又开始遇到同样的问题。 而不是每次创建一个新的弹出对象,创建一个全局对象,每次点击都会重新显示。 如果在将数据源添加到弹出控件之前将数据源设置为网格,那么它将显示网格将显示数据。但是,如果将数据网格添加到弹出控件中,则至少在视觉上忽略对数据源的任何分配。 – jblaske 2010-03-13 04:18:29

+0

我刚更新了我原来的评论。它有助于您使用BindingSource而不是直接分配DataGridView.DataSource属性。 – 2010-03-13 05:32:45

+0

在对bindingsource进行了一些调整之后,我能够使其工作。非常感谢你! – jblaske 2010-03-13 05:47:01

0

这可能是一个绘图问题。也许你可以尝试一个.Refresh在弹出容器或网格后显示它?

+0

我试过在控件本身上使用.Refresh,以及datagridview。这一切都没有奏效。 – jblaske 2010-03-12 21:15:34