method Items.AddItem ([Value as Variant])

Adds a new item, and returns a handle to the newly created item.

 TypeDescription 
   Value as Variant A variant expression that indicates the cell's value for the first column or a safe array that holds the values for each column.  
 ReturnDescription 
  HITEMA long expression that indicates the handle of the newly created item. 

Use the AddItem property to add new items/cards that have no parent ( usually when your control acts like a list or in CardView mode ). Adding new items fails, if the control has no columns. Use the Add method to add new columns to the control. Use InsertItem to insert child items ( usually  when your control acts like a tree ). Use the InsertControlItem to insert ActiveX items. Use PutItems method to load an array of variants. When a new item is added to the Items collection, the control fires the AddItem event. If the control contains more than one column use the CellValue property to set the cell's value. Use the CellValueFormat property to specify whether the value contains HTML format or computed fields. If the control has  no columns the AddItem method fails.  Use Add method to insert new columns to the control. The AddItem method is not available if the control is running in the virtual mode. Use the LockedItemCount property to lock or unlock items to the top or bottom side of the control. Use the MergeCells method to combine one or more cells in a single cell. Use the SplitCell property to split a cell. If the CauseValidateValue property is True, the control fires the ValidateValue property when the user adds a new item. Please notice that the Change event is fired when adding a new item, if you are specifying a value using the Value argument ). Use the BeginUpdate and EndUpdate methods to maintain performance while adding new columns and items. Use the CellEditor property to assign an editor to a cell. Use the Editor property to assign an editor to all cells in the column. Use the ConditionalFormats method to apply formats to a cell or range of cells, and have that formatting change depending on the value of the cell or the value of a formula.

The following VB6 sample uses the VB Array function to add two items: 

With Grid1
    .BeginUpdate
    
        .Columns.Add "Column 1"
        .Columns.Add "Column 2"
        .Columns.Add "Column 3"
        
        With .Items
            .AddItem Array("Item 1.1", "Item 1.2", "Item 1.3")
            .AddItem Array("Item 2.1", "Item 2.2", "Item 2.3")
        End With
        
    .EndUpdate
End With  

In VB/NET using the /NET assembly, the Array equivalent is New Object such as follows:

With Grid1
    .BeginUpdate()

    .Columns.Add("Column 1")
    .Columns.Add("Column 2")
    .Columns.Add("Column 3")

    With .Items
        .AddItem(New Object() {"Item 1.1", "Item 1.2", "Item 1.3"})
        .AddItem(New Object() {"Item 2.1", "Item 2.2", "Item 2.3"})
    End With

    .EndUpdate()
End With

In C# using the /NET assembly, the Array equivalent is new object such as follows:

exgrid1.BeginUpdate();

exgrid1.Columns.Add("Column 1");
exgrid1.Columns.Add("Column 2");
exgrid1.Columns.Add("Column 3");

exgrid1.Items.AddItem(new object[] { "Item 1.1", "Item 1.2", "Item 1.3" });
exgrid1.Items.AddItem(new object[] { "Item 2.1", "Item 2.2", "Item 2.3" });

exgrid1.EndUpdate();

The following C# sample adds a new item to the control:

EXGRIDLib.Items items = axGrid1.Items;
int i = items.AddItem("new items");
EXGRIDLib.Editor editor = items.get_CellEditor(i, 0);
editor.EditType = EXGRIDLib.EditTypeEnum.EditType;

The following VB.NET sample adds a new item to the control:

With AxGrid1.Items
    Dim i As Integer = .AddItem("new item")
    With .CellEditor(i, 0)
        .EditType = EXGRIDLib.EditTypeEnum.EditType
    End With
End With

The following C++ sample adds a new item to the control:

#include "Items.h"
#include "Editor.h"
COleVariant vtMissing; V_VT( &vtMissing) = VT_ERROR;
CItems items = m_grid.GetItems();
COleVariant vtItem( items.AddItem( COleVariant("new item") ) ), vtColumn( long(0) );
CEditor editor = items.GetCellEditor( vtItem, vtColumn );
editor.SetEditType( 1 /*EditType*/ );

The following VFP sample adds a new item to the control:

with thisform.Grid1.Items
	.DefaultItem = .AddItem("new item")
	With .CellEditor(0, 0) 
		.EditType = 1 && EditType
	EndWith
endwith

The following VB sample uses the VB Array function to add items to a multiple columns control:

With Grid1
    .BeginUpdate
    
    .LinesAtRoot = exLinesAtRoot
    .HasLines = exNoLine
    .HasButtons = exArrow
    
    .Columns.Add "Column 1"
    .Columns.Add "Column 2"
    
    With .Items
        Dim h As HITEM
        h = .AddItem(Array("Cell 1", "Cell 2"))
        .InsertItem h, , Array("Sub Cell 1.1", "Sub Cell 2.1")
        .InsertItem h, , Array("Sub Cell 1.2", "Sub Cell 2.2")
        
    End With
    
    .EndUpdate
End With

The following VB sample adds items to the control:

' Adds two columns
With .Columns
    With .Add("Column 1").Editor
        .EditType = EditTypeEnum.ColorType
    End With
    With .Add("Column 2").Editor
        .EditType = EditTypeEnum.EditType
    End With
End With

'Adds few items
With .Items
    Dim h As HITEM
    h = .AddItem(vbRed)
    .CellValue(h, 1) = "Simple Text"
    h = .AddItem(vbBlue)
    .CellValue(h, 1) = "Simple Text"
End With

The following VB sample loads a table row by row:

' Creates an ADO Recordset
Set rs = CreateObject("ADODB.Recordset")
rs.Open "Employees", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB", 3

With .Columns
    Dim f As Object
    For Each f In rs.Fields
        .Add f.Name
    Next
End With

' Adds an item for each record
With .Items
    rs.MoveFirst
    While Not rs.EOF
        .AddItem rs(0).Value
        rs.MoveNext
    Wend
End With
Private Sub Grid1_AddItem(ByVal Item As EXGRIDLibCtl.HITEM)
    ' Gets the value for each field, in the current record
    With Grid1.Items
        .ItemData(Item) = rs.Bookmark
        For i = 1 To Grid1.Columns.Count - 1
            .CellValue(Item, i) = rs(i).Value
        Next
    End With
End Sub
 

Send comments on this topic.
1999-2017 Exontrol.COM, Software. All rights reserved.