Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 23-Jun-21 19:02pm
Comments
Sergey Alexandrovich Kryukov 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 4-Aug-11 8:24am    
Now that made me laugh. Cheers.

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);
}
 
Share this answer
 
Comments
Habibur Rony 14-Jan-12 5:53am    
Thanks.
wwwx 19-Mar-17 21:44pm    
IT WORKS!
C#
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
 
Share this answer
 
Comments
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?

regards
my new solution




public DataTable DataGridtoDataTable(DataGrid dg)
        {

            DataTable dt = new DataTable();

            for (int i = 0; i <= dg.Columns.Count - 1; i++)
            {
                dt.Columns.Add(dg.Columns[i].Header.ToString(), typeof(string));
            }
            DataRow Row;

            for (int i = 0; i <= dg.Items.Count - 1; i++)
            {
                Row = dt.NewRow();

                for (int k = 0; k <= dg.Columns.Count - 1; k++)
                {
                    Row[dg.Columns[k].Header.ToString()] = gettabelcell(i, k,dg);

                }
                dt.Rows.Add(Row);
            }

            return dt;

        }

public String gettabelcell(int x, int y,DataGrid dg)
        {
            try
            {
                String myString = dg.GetCell(x, y).ToString();

                if (myString.Contains(":"))
                {
                    String[] s = myString.Split(':');
                    String item = s[1];
                    return item.Trim();
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }



this class

static class ExtensionHelpers
   {

       public static T GetVisualChild<T>(Visual parent) where T : Visual
       {
           T child = default(T);
           int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
           for (int i = 0; i < numVisuals; i++)
           {
               Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
               child = v as T;
               if (child == null)
               {
                   child = GetVisualChild<T>(v);
               }
               if (child != null)
               {
                   break;
               }
           }
           return child;
       }

       public static DataGridCell GetCell(this DataGrid grid, int row, int column)
       {
           DataGridRow rowContainer = grid.GetRow(row);
           return grid.GetCell(rowContainer, column);
       }
       public static DataGridRow GetSelectedRow(this DataGrid grid)
       {
           return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
       }
       public static DataGridRow GetRow(this DataGrid grid, int index)
       {
           DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
           if (row == null)
           {
               // May be virtualized, bring into view and try again.
               grid.UpdateLayout();
               grid.ScrollIntoView(grid.Items[index]);
               row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
           }
           return row;
       }

       public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
       {
           if (row != null)
           {
               DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

               if (presenter == null)
               {
                   grid.ScrollIntoView(row, grid.Columns[column]);
                   presenter = GetVisualChild<DataGridCellsPresenter>(row);
               }

               DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
               return cell;
           }
           return null;
       }
   }
 
Share this answer
 
Comments
CHill60 22-Oct-20 7:43am    
Please do not post multiple solutions to the same question. It is confusing and makes you look like a rep-point hunter. You should either use the "Improve solution" link next to your original post (however, 6 years down the line I'm not sure how much value that will add) or if you are that desperate to get this out there, consider writing it as a tip

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900