Click here to Skip to main content
11,577,710 members (64,584 online)
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 20:37pm
rc89994856
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...
Ronny_jair at 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?

regards
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 Peter Leow 180
1 DamithSL 170
2 Richard MacCutchan 125
3 OriginalGriff 115
4 Abhinav S 100
0 OriginalGriff 830
1 Sergey Alexandrovich Kryukov 701
2 Abhinav S 573
3 F-ES Sitecore 420
4 Dave Kreskowiak 374


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