简体   繁体   English

DataGridView不在ToolStripDropDown中显示数据

[英]DataGridView not displaying data in ToolStripDropDown

I'm utilizing the code posted by Jesper Palm here: Make user control display outside of form boundry 我正在使用Jesper Palm发布的代码: 在窗体边界之外显示用户控件

/// <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);
    }
}

I've translated it to VB: 我把它翻译成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

It works great with a PictureBox showing its information. 它适用于显示其信息的PictureBox。 But for some reason I cannot get the DataGridView to display anything when it is in the popup. 但由于某种原因,我无法让DataGridView在弹出窗口中显示任何内容。

If I pull the grid out of the popup it displays all of its information fine. 如果我从弹出窗口中拉出网格,它会显示所有信息。 If I pause during debug, the grid shows that it has all the data in it. 如果我在调试期间暂停,网格会显示其中包含所有数据。 It's just not displaying anything. 它只是没有显示任何东西。

Does anybody have any ideas? 有人有什么想法吗?

I have not been able to reproduce your problem. 我无法重现你的问题。 Can you provide more code? 你能提供更多代码吗? I've been testing in VS2010 RC (.NET 4) and VS2008 (.NET 3.5) and this code works in both: 我一直在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);
    }
}

Here's what it looks like: alt text http://img534.imageshack.us/img534/1640/popupcontrolwithgrid.jpg 这是它的样子: alt text http://img534.imageshack.us/img534/1640/popupcontrolwithgrid.jpg

In the designer, you should setup the BindingSource and assign it as the DataGridView's DataSource. 在设计器中,您应该设置BindingSource并将其指定为DataGridView的DataSource。

It's probably a drawing issue. 这可能是一个绘图问题。 Maybe you could try a .Refresh on either the popupcontainer or the grid after it's been shown? 也许你可以在弹出容器或网格显示后尝试.Refresh吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM