how to add cells and rows via code for datagrid

Jul 1, 2009 at 8:32 AM

hi, I am new to WPF datagrid. I have a Windows Form program needs to port to WPF.

In the Windows Form program, there is a datagrid displaying some data from a byte[].

The byte[](whose name in code is RawData) data is actually a block of data from physical memory.


Code here:

for (int i = 0; i < (lens / 16 + 1); i++)



   for (int j = 0; (j < 16) && ((j + 16 * i) < lens-1); j++)


       dataGridViewDump.Rows[i].Cells[0].Value = i.ToString("X8");

       int v = RawData[j + i * 16];

       dataGridViewDump.Rows[i].Cells[j + 1].Value = v.ToString("X2");




When porting to WPF datagrid, I don't know how to do. Will anyboby kindly help me out?

My purpose is just to display a block of memory data like debug.exe does.

Jul 2, 2009 at 9:06 AM

so sad there is still no feedback.

My purpose is to display a block of bytes data in the datagrid, one byte per cell , 16 bytes per row. In future, I will add functions to support two bytes or 4bytes in a cell. So, how can I do this with wpf datagrid?

Or, am I using the wrong control? Please help to specify if there is other wpf control suitable for my requirement. 


Jul 2, 2009 at 10:21 PM

Looks as tho you already have all the data you want to display, so, create an in-memory table, and populate it with the data you want to display.  You can use a different field for each cell of data.

Then assign the default view of the table as the ItemsSource for the datagrid.

If the datagrid has been set in the XAML to autogenerate=Yes, it will generate the appropriate rows and columns.