Click here to Skip to main content
15,749,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

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 ?
Updated 1-Jan-12 1:11am

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
Share this answer
aref.bozorgmehr 1-Jan-12 14:09pm    
thanks , do you have example
Espen Harlinn 2-Jan-12 4:35am    
No, but the process is *very* similar to what I do in my article:
NandaKumer 2-Jan-12 2:24am    
nice one
Espen Harlinn 2-Jan-12 4:34am    
Thanks, NandaKumer!
Share this answer
aref.bozorgmehr 5-Jan-12 17:37pm    
sorry but i cant do this

i want to edit any item with source code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900