更改datagrid(WPF)中的文本单元格

问题描述:


在数据网格中添加一行时,我需要更改单元格文本

对于添加行,我使用此代码

hey
i need change cell text when i add a row in data grid

for add row i use this code

      public struct MyData
       {
                public int ID { set; get; }
                   public string name { set; get; }
                        public float cost { set; get; }
                        public float tedad { set; get; }
       }

            int i,j = 0;
DataGridTextColumn col1 = new DataGridTextColumn();
DataRow dr; DataTable dt;

dg2.Items.Add(new MyData() { ID = j, name = a[1], cost = Convert.ToInt64(b[1]) });
col1.Binding = new Binding("ID");
DataGridTextColumn col2 = new DataGridTextColumn();
col2.Binding = new Binding("name");
DataGridTextColumn col3 = new DataGridTextColumn();
col3.Binding = new Binding("cost");
 
DataGridTextColumn col4 = new DataGridTextColumn();
col4.Binding = new Binding("tedad");
if (i == 0)
{
    ++i;
    dg2.Columns.Add(col1);
    dg2.Columns.Add(col2);
    dg2.Columns.Add(col3);
    dg2.Columns.Add(col4);
}


现在我想更改单元格名称中的名称
当我使用此代码时,出现错误
无法修改拆箱转换的结果"


now i want change name in cell name
when i use this code , i have a error
" Cannot modify the result of an unboxing conversion "

((MyData)dg2.Items[0]).name = "aref";



您能帮我吗?



can you help me ?

看看 ^ ].

我更喜欢将datagrid绑定到BindingListCollectionView的实例,然后只为新的MyData实例设置各种属性.

最好的问候
Espen Harlinn
Have a look at BindingListCollectionView[^].

I would prefer to bind the datagrid to an instance of the BindingListCollectionView and then just set the various properties for the new MyData instance.

Best regards
Espen Harlinn


检查此博客
http://www.scottlogic.co .uk/blog/colin/2009/01/wpf-datagrid-committing-changes-cell-by-cell/ [
check this blog
http://www.scottlogic.co.uk/blog/colin/2009/01/wpf-datagrid-committing-changes-cell-by-cell/[^]
--NDK