I have a problem.
I want to write ALL things programmatically in C #, without VS Designer.
So, I create an image and a DataGrid (and add it as a child of the MainWindow Grid):
Image img = new Image(); Uri uri = new Uri(@"C:\d1.jpg"); img.Source = new System.Windows.Media.Imaging.BitmapImage(uri); DataGrid dg = new DataGrid(); grid1.Children.Add(dg);
Then I want to add 4 columns, for example, 3 text and one image. So first I need to create a DataTable and a DataRow with data samples:
DataTable dt = new DataTable(); dt.Columns.Add("Column1"); dt.Columns.Add("Column2"); dt.Columns.Add("Column3"); dt.Columns.Add("Column4", typeof(Image)); // type of image! DataRow dr = dt.NewRow(); dr[0] = "aaa"; dr[1] = "bbb"; dr[2] = "ccc"; dr[3] = img; // add a sample image dt.Rows.Add(dr);
Now I have a DataTable with 4 columns and 1 row of data.
Then all I need to do is set the ItemsSource DataGrid as follows:
dg.ItemsSource = dt.DefaultView;
What am I doing wrong? Why on the last grid is System.Windows.Controls.Image in the line instead of the real image? Do I need to tie it or something else?
All I need to do programmatically, without a designer.
How to display the real image instead of this line?
source share