none
Relativ zum DataGridView.CurrentCell einen ListView positionieren ? RRS feed

  • Frage

  • Hallo,

    ich habe ein DataGridView(Zeilen=5, Spalten=5) und ein ListView.

    Beim Klicken auf irgend eine Zelle will ich einen ListView gefüllt mit InfoDaten unterhalb der dgv.Zelle an der linken Position der dgv.Zelle ausgeben.

    Wie mache ich das ??

    MFG

    Dienstag, 23. April 2013 11:28

Antworten

  • Hallo ati.sah,

    z.B. via MouseUp/Down Eventhandler kannst du die aktuelle Position mit e.Location, e.X oder e.Y bekommen, dann kannst du die gewünschte Position ausrechnen.

    Grüße

    Tu

    • Als Antwort markiert ati.sah Mittwoch, 24. April 2013 04:58
    Dienstag, 23. April 2013 14:25

Alle Antworten

  • Hallo ati.sah,

    z.B. via MouseUp/Down Eventhandler kannst du die aktuelle Position mit e.Location, e.X oder e.Y bekommen, dann kannst du die gewünschte Position ausrechnen.

    Grüße

    Tu

    • Als Antwort markiert ati.sah Mittwoch, 24. April 2013 04:58
    Dienstag, 23. April 2013 14:25
  • Hallo

    Also ich habe das damals mit der Funktion

    GetCellDisplayRectangle

    gemacht. Da du ja eh CurrentCell auswertest, erscheint mir das logischer.
    (Man hat halt auch gleich die Position der Zelle)
    Vielleicht musst du die Koordinaten dann noch mit PointToScreen umrechnen, damit die
    Positionierung dann funktioniert. Das kommt dann darauf an wie du die ListView einbindest.

    Ist aber auch Geschmackssache und viele Wege führen nach Rom.

    LG

    Christian





    Mittwoch, 24. April 2013 06:54
  • Hallo,

    ein Problem habe ich noch

    soweit ich im unteren Zeilen der DGV bin erscheint der ListView nicht außerhalb des Forms dh DGV ragt nicht aus dem Form aus sondern wird abgeschnitten.

    Warum ?

    MFG

    Mittwoch, 24. April 2013 15:01
  • Hallo

    Ich nehme an, du hast das Listview als Control auf der Form eingebunden, auf dem das DGV auch ist.

    Du solltest das Listview lieber in ein eigenes Formular geben und das Form dann aufrufen und zur Spalte positionieren, dann wird es auch außerhalb angezeigt.

    LG

    Christian



    Donnerstag, 25. April 2013 07:03
  • Hallo ati.sah,

    Control von der Form kann die Formgrenze nicht überschreiten. Packe also dein ListView in ein Userform dann sollte es gehen.

    Grüße

    Tu

    Donnerstag, 25. April 2013 07:24
  • Hallo,

    gemacht, aber ergibt sich ein weiteres Problem zwar:

    den selektierten Auswahlwert aus dem frm1.ListView zurück in frm2.DataGridView[SelectedRows][2] zu schreiben es sind 2 verschiedene Formen (frm1 <=> frm2) ?

    MFG

    Freitag, 26. April 2013 07:07
  • Hallo

    In diesem Fall würde es sich auf jeden Fall anbieten, dem Form mit deinem ListView,
    das Grid im Konstruktor mit zu übergeben und das Grid dann als globale Variable im Form zu haben.

    Somit hast du in deinem ListViewForm auch immer Zugriff auf das (richtige) Grid.

    LG
    Christian

    Freitag, 26. April 2013 07:24
  • Hallo,

    bitte um einen Bspl.

    MFG

    Freitag, 26. April 2013 08:20
  • Hallo

    OK, habe mal etwas änliches gebaut, allerdings nicht mit ListView, sondern mit einer einfach Textbox.
    Das ganze dient dann dazu, dass man bei (kleinen) Gridzellen ein separates Editorfenster aufmachen kann.

    Weiter funktioniert das Beispiel auch bei Textboxen.
    Deshalb ist bei dem Beispiel etwas mehr drinnen als vielleicht benötigt und du musst es halt auf dein ListView umdenken.
    Aber ich denke es sollte trotzdem übersichtlich genug sein.

    Auch die Positionierung ist hier drinnen enthalten.

    Das Editorfenster besteht aus einer Textbox, einem OK-Button und Abbrechen-Button.

    public partial class frmTextWindow : Form { #region --> Global <-- private System.Windows.Forms.DataGridView dtgWork; private System.Windows.Forms.TextBox txtWork; private string OldValue = null; #endregion #region --> Konstruktor <-- public frmTextWindow(System.Windows.Forms.DataGridView dtg) { InitializeComponent(); if (dtg == null) throw new ArgumentNullException("dtg"); dtgWork = dtg; } public frmTextWindow(System.Windows.Forms.TextBox txt) { InitializeComponent(); if (txt == null) throw new ArgumentNullException("txt"); txtWork = txt; } #endregion #region --> Public <-- #region --> Properties <-- #endregion #region --> Funktionen <-- public new DialogResult ShowDialog() { return this.ShowDialog(null); } public new DialogResult ShowDialog(IWin32Window owner) { DialogResult ret = DialogResult.None; if (dtgWork != null) { if (HandleDataGridView()) { ret = base.ShowDialog(owner); } } else if (txtWork != null) { if (HandleTextBox()) { ret = base.ShowDialog(owner); } } return ret; } #endregion #endregion #region --> Privat <-- #region --> Funktionen <-- #region --> HandleDataGridView <-- private bool HandleDataGridView() { bool show = false; if (dtgWork.CurrentCell == null) { show = false; } else { if (dtgWork.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl)) { txtValue.ReadOnly = dtgWork.CurrentCell.ReadOnly; if (dtgWork.CurrentCell.ReadOnly) txtValue.Text = Convert.ToString(dtgWork.CurrentCell.FormattedValue); else { //DataGridViewTextBoxCell txttemp = (DataGridViewTextBoxCell)dtgWork.CurrentCell; DataColumn col = null; if (dtgWork.DataSource != null && string.IsNullOrEmpty(dtgWork.CurrentCell.OwningColumn.DataPropertyName) == false) { if (dtgWork.DataSource.GetType() == typeof(System.Data.DataTable)) { System.Data.DataTable dt = dtgWork.DataSource as System.Data.DataTable; col = dt.Columns[dtgWork.CurrentCell.OwningColumn.DataPropertyName]; } else if (dtgWork.DataSource.GetType() == typeof(System.Data.DataView)) { System.Data.DataView dv = dtgWork.DataSource as System.Data.DataView; col = dv.Table.Columns[dtgWork.CurrentCell.OwningColumn.DataPropertyName]; } else {

    // MessageboxAusgabe ! Helper.Messages.NewDeveloperInformationMessage("DataSourceType nicht erkannt!" + Environment.NewLine + "frmTextWindow.HandleDataGridView()" + Environment.NewLine + dtgWork.DataSource.ToString()); } } if (col != null) txtValue.MaxLength = col.MaxLength; if (dtgWork.CurrentCell.IsInEditMode) txtValue.Text = Convert.ToString(dtgWork.CurrentCell.EditedFormattedValue); else txtValue.Text = Convert.ToString(dtgWork.CurrentCell.Value); } show = true; } else if (dtgWork.CurrentCell.EditType == typeof(DataGridViewComboBoxEditingControl)) { txtValue.ReadOnly = true; txtValue.Text = Convert.ToString(dtgWork.CurrentCell.FormattedValue); show = true; } else if (dtgWork.CurrentCell.EditType == typeof(DataGridView.DataGridViewEditingControls.CalendarEditingControl)) { txtValue.ReadOnly = true; txtValue.Text = Convert.ToString(dtgWork.CurrentCell.FormattedValue); show = true; } OldValue = txtValue.Text; Point pdtg = dtgWork.PointToScreen(dtgWork.GetCellDisplayRectangle(dtgWork.CurrentCell.ColumnIndex, dtgWork.CurrentCell.RowIndex, false).Location); this.Left = pdtg.X; this.Top = pdtg.Y - this.Height - 10; this.Text = dtgWork.CurrentCell.OwningColumn.HeaderText; } return show; } #endregion #region --> HandleTextBox <-- private bool HandleTextBox() { bool show = true; if (txtWork.PasswordChar != 0 || txtWork.UseSystemPasswordChar) { show = false; } else { Point ptxt = Point.Empty; string name = string.Empty; txtValue.ReadOnly = !(txtWork.ReadOnly == false && txtWork.Enabled); txtValue.Text = txtWork.Text; txtValue.MaxLength = txtWork.MaxLength; OldValue = txtValue.Text; ptxt = this.Helper.GetLocationOnClientToScreen(txtWork); this.Left = ptxt.X; this.Top = ptxt.Y - this.Height - 10; name = txtWork.Name.Replace("txt", "lbl"); if (txtWork.Parent.Controls.ContainsKey(name)) { this.Text = txtWork.Parent.Controls[name].Text; } //else //{ // this.Text = txtWork.Parent.Controls[name].Text; //} } return show; } #endregion #endregion #endregion #region --> Events <-- private void btnOK_Click(object sender, EventArgs e) { if (txtValue.ReadOnly == false && OldValue != txtValue.Text) { if (dtgWork != null && dtgWork.CurrentCell != null) { if (dtgWork.CurrentCell.IsInEditMode) { dtgWork.CancelEdit(); } dtgWork.CurrentCell.Value = txtValue.Text; //dtgWork.EndEdit(); } else if (txtWork != null) { txtWork.Text = txtValue.Text; } } this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void frmTextWindow_Shown(object sender, EventArgs e) { txtValue.Select(); txtValue.SelectionStart = 0; txtValue.SelectionLength = 0; } private void frmTextWindow_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) { txtValue.SelectAll(); } } #endregion }

    LG
    Christian


    Freitag, 26. April 2013 09:45