Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey
i need change cell text when i add a row in data grid

for add row i use this code
C#
      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 "
C#
((MyData)dg2.Items[0]).name = "aref";


can you help me ?
Posted
Updated 1-Jan-12 1:11am
v2

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
 
Comments
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: http://www.codeproject.com/KB/cs/Depends4Net_Part1.aspx
NandaKumer 2-Jan-12 2:24am    
nice one
Espen Harlinn 2-Jan-12 4:34am    
Thanks, NandaKumer!
 
Share this answer
 
Comments
aref.bozorgmehr 5-Jan-12 17:37pm    
sorry but i cant do this

i want to edit any item with source code
thanks

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