Click here to Skip to main content
13,045,920 members (47,686 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am migrating the code from windows forms to WPF.

in windows forms the code is:
Datatable dt= datagrid1.Datasource as DataTable

Can anyone help me what will it migrate to in WPF.
I tried with
Datatable dt= datagrid1.ItemsSource as DataTable

But I am getting dt=null
Posted 3-Aug-11 20:37pm
SAKryukov 4-Aug-11 2:40am
Look at this: "". Are you from the same school getting the same assignment. Your question is better... :-).
Pete O'Hanlon 4-Aug-11 8:24am
Now that made me laugh. Cheers.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

public static DataTable DataGridtoDataTable(DataGrid dg)
            dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            ApplicationCommands.Copy.Execute(null, dg);
            String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
            string[] Lines = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            string[] Fields;
            Fields = Lines[0].Split(new char[] { ',' });
            int Cols = Fields.GetLength(0);
            DataTable dt = new DataTable();
            //1st row must be column names; force lower case to ensure matching later on.
            for (int i = 0; i < Cols; i++)
                dt.Columns.Add(Fields[i].ToUpper(), typeof(string));
            DataRow Row;
            for (int i = 1; i < Lines.GetLength(0)-1; i++)
                Fields = Lines[i].Split(new char[] { ',' });
                Row = dt.NewRow();
                for (int f = 0; f < Cols; f++)
                    Row[f] = Fields[f];
            return dt;

check this
CHill60 23-Feb-14 10:56am
OP hopefully hasn't been waiting 2 and a half years for a solution
Member 10517530 14-Apr-14 5:16am
That was funny...
Ronny_jair 17-Jun-15 0:01am
Hi, To be wanting to operate in any case put the sigueinte code at the beginning : dg.SelectionMode = DataGridSelectionMode.Extended ;
Furthermore, when the column is a combobox , I do not want it then appears the ID of the combobox but the text , any ideas?

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

what you are assigned to datagrid1.ItemsSource, you get the same thing back.

if you are not sure what you are getting back then assign it to a "var" object and check the type

var abc = datagrid1.ItemsSource;
Member 2231466 14-Jan-12 5:53am
wwwx 19-Mar-17 21:44pm

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100