如何从ObjectListView中选定项目的特定列获取数据?

时间:2019-05-07 13:22:18

标签: c# objectlistview

我有一个ObjectListView,我需要获取被单击的行中特定列的值。

例如:

Imgur (我将图像直接放在此处,但我没有这样做的声誉。)

我需要从单击的行中获取ID(与单击行的位置无关-我是指该行中的哪一列)。

谢谢。

2 个答案:

答案 0 :(得分:1)

使用SelectionChanged事件并从那里访问模型对象的相应属性。假设您的模型类名为 MyDataObject

    private void olv_SelectionChanged(object sender, EventArgs e) {
        if (olv.SelectedObject is MyDataObject data) {
            // Access data.Id or whatever else you need
        } 
    }

答案 1 :(得分:0)

为使我的回答更加清楚,我提供了一个完整的工作示例供您使用。本示例创建示例数据,数据网格视图,Windows窗体,然后显示Windows窗体。然后,它会响应鼠标单击,并显示MessageBox来显示单击的行的ID。

我假设您正在使用DataGridView来显示数据,并且该数据以对象数组或列表的形式表示。也可以将DataSetDataTable一起使用,但是为简单起见,我使用了一个对象数组。

回答您直接问题的代码位于MouseClick事件处理程序中,该事件处理程序在其中设置了selectedObject变量:

void Main()
{
    var data = new[] {
        new MyData { Id = 1, Typ = "Prichozi", Castka =  500, Datum = new DateTime(2019, 4, 16), Ucel = "test" },
        new MyData { Id = 2, Typ = "Odchozi",  Castka =  250, Datum = new DateTime(2019, 4, 16), Ucel = "test" },
        new MyData { Id = 3, Typ = "Prichozi", Castka = 2000, Datum = new DateTime(2019, 4, 16), Ucel = "test" },
        new MyData { Id = 4, Typ = "Odchozi",  Castka = 3500, Datum = new DateTime(2019, 4, 16), Ucel = "test" },
        new MyData { Id = 5, Typ = "Prichozi", Castka = 5000, Datum = new DateTime(2019, 4, 16), Ucel = "test" }
    };

    var view = new DataGridView();
    view.AllowUserToAddRows = false;
    view.AllowUserToDeleteRows = false;
    view.AllowUserToResizeRows = false;
    view.Anchor = System.Windows.Forms.AnchorStyles.Top
        | System.Windows.Forms.AnchorStyles.Bottom 
        | System.Windows.Forms.AnchorStyles.Left 
        | System.Windows.Forms.AnchorStyles.Right;
    view.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
    view.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    view.ReadOnly = true;
    view.RowHeadersVisible = false;
    view.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
    view.Dock = System.Windows.Forms.DockStyle.Fill;
    view.MouseClick += (sender, args) => { 
        // This is where the magic happens
        var selectedObject = ((DataGridView)sender)
            .SelectedRows.Cast<DataGridViewRow>()
            .Select(dgvr => dgvr.DataBoundItem).Cast<MyData>()
            .FirstOrDefault();
        // Show your result
        MessageBox.Show(
            $"Selected row with ID: {selectedObject.Id}",
            "Selection Notification", 
            MessageBoxButtons.OK, 
            MessageBoxIcon.Information,
            MessageBoxDefaultButton.Button1,
            MessageBoxOptions.DefaultDesktopOnly,
            false);
        };
    view.DataSource = data;

    var form = new Form { Width = 500, Height = 300 };
    form.Controls.Add(view);
    form.ShowDialog();
}

public class MyData
{
    public int Id { get; set; }
    public string Typ { get; set; }
    public int Castka { get; set; }
    public DateTime Datum { get; set; }
    public string Ucel { get; set; }
}