简体   繁体   English

将对象列表绑定到datagridview中的单元格

[英]Bind list of objects to cell in datagridview

I'm trying to represent a list of objects from my entity framework in datagridview cell. 我试图在datagridview单元中表示来自我的实体框架的对象列表。

In order for them to display I am overriding the cellFormatting event and setting the value in the cell to a list of the ids of the objects separated by commas. 为了让它们显示,我覆盖了cellFormatting事件,并将单元格中的值设置为以逗号分隔的对象ID的列表。

Now if the user wants to edit that cell then I need to do something with the input. 现在,如果用户想要编辑该单元格,那么我需要对输入内容进行一些操作。

The thing I thought was to override Cellparsing and in there to set the correct objects in the data structure and also make the cell display. 我想到的是重写Cellparsing并在那里设置数据结构中的正确对象,并使单元格显示。 However the cell is just blank because I can't see where to set its contents without the control throwing a cast exception. 但是,该单元格只是空白,因为在没有控件抛出强制转换异常的情况下,我看不到要设置其内容的位置。 Can I call the formatting event again? 我可以再次调用格式化事件吗?

Should I just forget this approach and have another layer in the code that outputs a view that can be bound without all this to the datagrid? 我是否应该忘记这种方法,而在代码中有另一层可以输出一个视图,而无需将所有视图绑定到数据网格呢?

Thankyou. 谢谢。

    private void dataGridView1_CellParsing(object sender,  DataGridViewCellParsingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name == "Categories")
        {
            String catids = dataGridView1.CurrentCell.Value.ToString();

            Product p = (Product)productBS.Current;

            //DataRow dataRow = ((DataRowView)bindingSource1.Current).Row;

            p.Categories.Clear();

            String[] catstrings = catids.Split(',');

            foreach (var cs in catstrings)
            {
                int catid = 0;

                int.TryParse(cs, out catid);

                if (catid != 0)
                {
                    // Find this cat list in the BS and change it
                    p.Categories.Add(_db.Categories.Find(catid));
                }
            }

            e.Value = p.Categories;

            e.ParsingApplied = true;


        }


    }

(1) Do not do in cell formatting event its expensive. (1)不要在单元格格式化事件中使其昂贵。 First format the data and then display 首先格式化数据,然后显示

(2) Now if user wants edit and you want to format the data. (2)现在,如果用户要编辑并且您要格式化数据。 Then use the belo event to track editing 然后使用belo事件跟踪编辑

  private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        // here check for formatting.
    }

If I format the data before I bind it I will have to create another object like a view of simple properties, so it's another layer of objects for all my complex entities that I will show. 如果在绑定数据之前格式化数据,则必须创建另一个对象,例如简单属性视图,因此它将是我将显示的所有复杂实体的另一层对象。 I can't just use a simple linq expression on them as it uses anonynous objects (any other way?) which then means those columns are read only. 我不能只对它们使用简单的linq表达式,因为它使用匿名对象(以其他方式?),这意味着这些列是只读的。

Now for handling edited data I don't know what to put in endEdit. 现在,对于处理已编辑的数据,我不知道要在endEdit中放入什么。 I can't get string from cell and convert back to object, it still throws invalid cast exception in pushValue part of pipeline. 我无法从单元格获取字符串并将其转换回对象,它仍然在管道的pushValue部分中引发无效的强制转换异常。 Where do I convert string back to object? 在哪里将字符串转换回对象?

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

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