C# 如何在DataGridColumn中执行Drop操作?
我有一个项目,其中必须将数据从excel导入SQL数据库,excel信息未格式化,必须验证并与SQL列的表配对到我的应用程序中 我有一个表示SQL列的ListView,还有一个表示excel信息的Datagrid AutoGenerateColumns=true 现在用户必须将ListView项与DataGrid列配对,我正在尝试使用DragDrop来完成,ListView的所有操作都已完成,但DataGrid是动态生成的,无法对每个列进行编程以设置DragDrop操作 如何在自动生成的DataGridColumns上设置DragDrop操作 有什么建议吗 我在寻找DataGrid属性,在那里我可以强制DataGrid使用我自己的ImportDataGridColumn:DataGridColumn,但找不到在哪里执行 更新:C# 如何在DataGridColumn中执行Drop操作?,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个项目,其中必须将数据从excel导入SQL数据库,excel信息未格式化,必须验证并与SQL列的表配对到我的应用程序中 我有一个表示SQL列的ListView,还有一个表示excel信息的Datagrid AutoGenerateColumns=true 现在用户必须将ListView项与DataGrid列配对,我正在尝试使用DragDrop来完成,ListView的所有操作都已完成,但DataGrid是动态生成的,无法对每个列进行编程以设置DragDrop操作 如何在自动生成的Data
终于可以添加我自己的列了,但是现在在列上找不到AllowDrop。。。。这真是一场噩梦。当DataGrid的AutoGenerateColumns=False时,您可以在运行时使用其Columns属性添加列。最终无法找到直接方法对列执行拖放操作。。。 然后将AllowDrop=true设置为我的数据网格 当对我的数据网格执行拖放时,我使用列宽搜索列。下面是帮助我进行水平滚动偏移的代码tks
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"))
{
...
}
}
}
}
有样品吗?