Answered by:
How to bind a datagrid with datatable in wpf ?

Question
-
Hi all,
Here is my code , but its not working .
DataTable aTable = new DataTable();
aTable.Columns.Add(new DataColumn("ProductID", typeof(int)));
aTable.Columns.Add(new DataColumn("ProductName", typeof(string)));
aTable.Rows.Add(12, "Lap top");
aTable.Rows.Add(13, "bbb");
aTable.Rows.Add(14, "ccc");
this.DataContext = aTable;MSDN Forum
- Moved by Lisa Zhu Wednesday, May 23, 2012 3:12 AM Issue about WPF (From:Visual C# General)
Monday, May 21, 2012 5:55 AM
Answers
-
Hi Amal E S,
DataGrid.ItemsSource = DataTableName.DefaultView();
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Sheldon _Xiao Monday, June 4, 2012 6:59 AM
Wednesday, May 23, 2012 5:28 AM
All replies
-
Monday, May 21, 2012 6:05 AM
-
If this is your wpf datatable, than it goes for me.
Maybe first try it with the name of the DataGrid instead of this.
Success
CorMonday, May 21, 2012 6:23 AM -
Hi Amal,
you are calling this which represents current window/page, can you change it to the name of your datagrid and try again....
Sai Kumar K (MCP)
Blog: Sai's Stuff.
WebSite: SantoshTechnologies.
MCP ID:Microsoft Transcript.Monday, May 21, 2012 6:42 AM -
Hi Amal,
you are calling this which represents current window/page, can you change it to the name of your datagrid and try again....
Sai,
Depends, if it is in a user control it can be an inherited datagrid, that was why I replied it so kind of cryptic, the OP is not very clear in this and also not in his code.
:-)
Success
Cor- Edited by Cor Ligthert Monday, May 21, 2012 8:07 AM
Monday, May 21, 2012 8:07 AM -
Hi,
create a datatable Property which get your data table,
then bind the datagrid to the new property
{Pishkari}
Monday, May 21, 2012 10:15 AM -
Monday, May 21, 2012 10:38 AM
-
Hi Amal E S,
DataGrid.ItemsSource = DataTableName.DefaultView();
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Sheldon _Xiao Monday, June 4, 2012 6:59 AM
Wednesday, May 23, 2012 5:28 AM -
If DataGrid is in a Panel (for example)
then you could use Panel.DataContext = <dataTable> and the grid would automatically use the Context of its parent control. Not sure but i think you should define the columns in this case manually.
-- greets blub
- Proposed as answer by Sheldon _Xiao Monday, June 4, 2012 6:59 AM
Wednesday, May 23, 2012 8:00 AM -
Hi Amal E S,
I am marking your issue as "Answered", if you have new findings about your issue, please let me know.
Best regards,Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, June 4, 2012 7:02 AM