當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。