0
votes

I have a project where must import data from excel to an SQL database, Excel info is not formatted and must be validated and paired to SQL column's table into my app.

I have a ListView where SQL columns are represented, and a Datagrid (AutoGenerateColumns = true) where excel info is represented.

Now user must pair ListView Items with DataGrid columns, I 'm trying to do it using DragDrop, all at ListView Is done, but Datagrid is generated dynamically and can't program each column to Set DragDrop operations.

How Can I Do To set DragDrop operations over those autogenerated DataGridColumns ?

Any Suggestions ?

I was looking for DataGrid property where I can force DataGrid to use my own ImporDataGridColumn : DataGridColumn But can't found where to do it.

UPDATE: Finally Can add my own columns but now can't found AllowDrop on columns.... what a Nightmare.

2

2 Answers

1
votes

When DataGrid's AutoGenerateColumns= False, You can use its Columns property to add Columns in runtime.

0
votes

Finally Can't found a direct way to perform my drop over a column... Then i set AllowDrop = true to my data grid.

When the drop is performed to my data grid, I search the column using the columns widths. Here is the code (tks to @mm8) who help me with the Horizontal scroll offset

    private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null)
            return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null)
                return result;
        }
        return null;
    }


    private void DGrdDatosImportar_Drop(object sender, DragEventArgs e)
    {

        ScrollViewer sv = GetChildOfType<ScrollViewer>(DGrdDatosImportar);
        if (sv != null)
        {
            double horizontalOffset = sv.HorizontalOffset;
            var dropPos = e.GetPosition(DGrdDatosImportar);
            double relativeXPos = dropPos.X + horizontalOffset;
            double RefPos = DGrdDatosImportar.RowHeaderActualWidth;
            DataGridColumn SelecteCol = null;
            foreach (DataGridColumn Col in DGrdDatosImportar.Columns.ToList())
            {
                double ColWidth = Col.ActualWidth;
                if (relativeXPos >= RefPos && relativeXPos <= (RefPos + ColWidth))
                {
                    SelecteCol = Col;
                    break;
                }
                RefPos += ColWidth;
            }
            if (SelecteCol != null)
            {
                if (e.Data.GetDataPresent("IImportProperty"))
                {
                   ...
                }
            }
        }
    }