4 Stimmen

DataGridView zeigt keine Daten im ToolStripDropDown an

Ich verwende den von Jesper Palm hier geposteten Code: Benutzersteuerelement außerhalb der Formularbegrenzung anzeigen lassen

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

Ich habe es in VB übersetzt:

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

Es funktioniert hervorragend mit einer PictureBox, die die Informationen anzeigt. Aber aus irgendeinem Grund kann ich die DataGridView nicht bekommen, etwas anzuzeigen, wenn es in dem Popup ist.

Wenn ich das Raster aus dem Popup herausziehe, werden alle Informationen korrekt angezeigt. Wenn ich während der Fehlersuche pausiere, zeigt das Raster an, dass es alle Daten enthält. Es wird nur nichts angezeigt.

Hat jemand eine Idee?

1voto

Jason Williams Punkte 1263

Ich habe Ihr Problem nicht reproduzieren können. Können Sie mehr Code zur Verfügung stellen? Ich habe in VS2010 RC (.NET 4) und VS2008 (.NET 3.5) getestet und dieser Code funktioniert in beiden:

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

So sieht es aus: Alt-Text http://img534.imageshack.us/img534/1640/popupcontrolwithgrid.jpg

Im Designer sollten Sie die BindingSource einrichten und sie als DataSource des DataGridViews zuweisen.

0voto

Fake Punkte 488

Es ist wahrscheinlich ein Problem der Zeichnung. Vielleicht könnten Sie versuchen, ein .Refresh entweder auf den Popupcontainer oder das Gitter, nachdem es angezeigt wurde?

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X