这个问题与带有点的数据列名称有关,这使它们不适合WPF的datagrid控件?
我正在使用datagrid显示临时查询构建器的结果,因此数据列列可以是任何东西。
我以最多的选票遵循答案(14)。一切都很好,但是如果我单击标头将整个应用程序崩溃的列对列进行排序。
错误:
System.IndexOutOfRangeException HResult=0x80131508 Message=Index was outside the bounds of the array. Source=AppName StackTrace: at AppName.App.ShowUnhandeledException(DispatcherUnhandledExceptionEventArgs e) in <path>App.xaml.cs:line 51 at AppName.App.AppDispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e) in <path>App.xaml.cs:line 27 at System.Windows.Threading.Dispatcher.CatchException(Exception e) at System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e) at System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at AppName.App.Main()
这是我的XAML:
<DataGrid x:Name="DgridQueryResults" Grid.Row="1" Background="LightGray" Margin="5" ItemsSource ="{Binding .}" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="true" CanUserReorderColumns="False" CanUserSortColumns="True" ClipboardCopyMode="ExcludeHeader"SelectionUnit="Cell" CopyingRowClipboardContent="DgridQueryResults_CopyingRowClipboardContent" Grid.ColumnSpan="2"/>
我尝试了上面的项目源就像itemsSource =" {binding}"但它仍然崩溃。
我以以下方式编程添加括号:
private void DgridQueryResults_ColumnSetup()
{
DgridQueryResults.Columns.Clear();
DgridQueryResults.AutoGenerateColumns = false;
foreach (DataColumn column in _results.Columns)
{
var gridColumn = new DataGridTextColumn()
{
Header = column.ColumnName,
Binding = new Binding("[" + column.ColumnName + "]")
};
DgridQueryResults.Columns.Add(gridColumn);
}
}
我在设置项目源之前设置了列,如下所示:
private void ExecuteSql()
{
if (_dataFormData.GetDataViaReader(_sql))
{
_results = _dataFormData.DT;
DgridQueryResults_ColumnSetup();// <-- column setup
DgridQueryResults.DataContext = _results.DefaultView;
}
else
{
DgridQueryResults.DataContext = null;
}
QueryResults.Focus();
}
评论列设置工作正常,但是我无法具有违反绑定的列名(即,完全stop(。))。
还将设置用户设置为XAML中的false将其设置为FALSE修复它,但是当然,用户无法对数据进行排序。
我正在使用vs 2017 15.5.1针对框架4.6.1。
其他人是否经历过?感谢任何帮助。谢谢!
我弄清楚了。添加列时,我必须添加排序成员路径。
private void DgridQueryResults_ColumnSetup()
{
DgridQueryResults.Columns.Clear();
DgridQueryResults.AutoGenerateColumns = false;
foreach (DataColumn column in _results.Columns)
{
var gridColumn = new DataGridTextColumn()
{
Header = column.ColumnName,
SortMemberPath = column.ColumnName, //<--added
Binding = new Binding("[" + column.ColumnName + "]")
};
DgridQueryResults.Columns.Add(gridColumn);
}
}