Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
Hi,
 
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 21:37pm
rc89994839
Comments
SAKryukov at 4-Aug-11 2:40am
   
Look at this: "http://www.codeproject.com/Questions/235997/insert-datagridview-to-database". Are you from the same school getting the same assignment. Your question is better... :-).
--SA
Pete O'Hanlon at 4-Aug-11 8:24am
   
Now that made me laugh. Cheers.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

public static DataTable DataGridtoDataTable(DataGrid dg)
        {
          
          
            dg.SelectAllCells();
            dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            ApplicationCommands.Copy.Execute(null, dg);
            dg.UnselectAllCells();
            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];
                }
                dt.Rows.Add(Row);
            }
            return dt;
            
        }
 

check this
  Permalink  
Comments
CHill60 at 23-Feb-14 10:56am
   
OP hopefully hasn't been waiting 2 and a half years for a solution
Member 10517530 at 14-Apr-14 5:16am
   
That was funny...
Rate this: bad
good
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;
 
if(abc!=null)
{
 MessageBox.Show(abc.GetType().Name);
}
  Permalink  
Comments
Member 2231466 at 14-Jan-12 5:53am
   
Thanks.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 23 Feb 2014
Copyright © CodeProject, 1999-2014
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