当前位置: 首页>>代码示例>>C#>>正文


C# DataGridControl.OnAddingNewDataItem方法代码示例

本文整理汇总了C#中Xceed.Wpf.DataGrid.DataGridControl.OnAddingNewDataItem方法的典型用法代码示例。如果您正苦于以下问题:C# DataGridControl.OnAddingNewDataItem方法的具体用法?C# DataGridControl.OnAddingNewDataItem怎么用?C# DataGridControl.OnAddingNewDataItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Xceed.Wpf.DataGrid.DataGridControl的用法示例。


在下文中一共展示了DataGridControl.OnAddingNewDataItem方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddNewDataItem

    public static object AddNewDataItem(
      IEnumerable itemsSourceCollection,
      DataGridControl dataGridControl,
      out int itemIndex )
    {
      DataGridCollectionViewBase dataGridCollectionViewBase = itemsSourceCollection as DataGridCollectionViewBase;

      if( dataGridCollectionViewBase != null )
      {
        if( !dataGridCollectionViewBase.CanAddNew )
          throw new InvalidOperationException( "An attempt was made to add a new data item to a source that does not support insertion." );

        itemIndex = dataGridCollectionViewBase.Count;
        return dataGridCollectionViewBase.AddNew();
      }

      if( ( dataGridControl != null ) && ( dataGridControl.ItemsSource == null ) )
      {
        //unbound
#pragma warning disable 618
        AddingNewDataItemEventArgs eventArgs = new AddingNewDataItemEventArgs();
        dataGridControl.OnAddingNewDataItem( eventArgs );
        object newItem = eventArgs.DataItem;
#pragma warning restore 618

        if( newItem == null )
          throw new InvalidOperationException( "The AddingNewDataItem event did not return a new data item because the grid is not bound to a data source." );

        itemIndex = dataGridControl.Items.Add( newItem );
        return newItem;
      }

      DataView dataView = itemsSourceCollection as DataView;

      if( dataView == null )
      {
        CollectionView collectionView = itemsSourceCollection as CollectionView;

        dataView = ( collectionView == null ) ?
          null : collectionView.SourceCollection as DataView;
      }

      if( dataView != null )
      {
        itemIndex = dataView.Count;
        return dataView.AddNew();
      }

      IBindingList bindingList = itemsSourceCollection as IBindingList;

      if( bindingList == null )
      {
        CollectionView collectionView = itemsSourceCollection as CollectionView;

        bindingList = ( collectionView == null ) ?
          null : collectionView.SourceCollection as IBindingList;
      }

      if( ( bindingList != null ) && ( bindingList.AllowNew ) )
      {
        itemIndex = bindingList.Count;
        return bindingList.AddNew();
      }

      Type itemType = ItemsSourceHelper.GetItemTypeFromEnumeration( itemsSourceCollection );

      if( itemType == null )
        throw new InvalidOperationException( "An attempt was made to use a source whose item type cannot be determined." );

      try
      {
        itemIndex = -1;
        return Activator.CreateInstance( itemType );
      }
      catch( MissingMethodException exception )
      {
        throw new InvalidOperationException( "An attempt was made to use a source whose item type does not have a default constructor.", exception );
      }
      catch( Exception exception )
      {
        throw new InvalidOperationException( "An unsuccessful attempt was made to create an instance of the source's item type using the item's default constructor.", exception );
      }
    }
开发者ID:Torion,项目名称:WpfExToolkit,代码行数:83,代码来源:ItemsSourceHelper.cs


注:本文中的Xceed.Wpf.DataGrid.DataGridControl.OnAddingNewDataItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。