public void RemoveItem(int itemIndex) { // clear selected item, if deleting the current selected index if (SelectedIndex == itemIndex) { SelectedIndex = -1; } // remove the correspondent data item from the controls OSDataGridTable child = (OSDataGridTable)Controls[0]; // find the row corresponding to this index int rowIndexToRemove = -1; for (int i = 0; i < child.Rows.Count; i++) { OSDataGridItem item = (OSDataGridItem)child.Rows[i]; bool isItem = item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item; if (isItem && item.ItemIndex == itemIndex) { rowIndexToRemove = i; break; } } IOSList rl = DataSource; if (rowIndexToRemove == -1 || itemIndex >= rl.Length) { throw new ArgumentException("Index of out range"); } child.Rows.RemoveAt(rowIndexToRemove); // update the datasource rl.Remove(itemIndex); // update item count ViewState["_!ItemCount"] = rl.Length; // decrease higher item indexes in the data grid foreach (OSDataGridItem otherItem in child.Rows) { if (otherItem.ItemIndex > itemIndex) { otherItem.SetItemIndex(otherItem.ItemIndex - 1); } } // add empty row if necessary AddEmptyRow(); // update row ids in the viewstate StoreViewStateRowIdsandItemIndexes(); }
public static OSDataGridTable FromTable(OSDataGrid parent, System.Web.UI.WebControls.Table table, ArrayList rowIds, ArrayList itemIndexes) { OSDataGridTable osTable = new OSDataGridTable(parent.ClientID); // copy everything from table // Properties osTable.BackImageUrl = table.BackImageUrl; osTable.Caption = table.Caption; osTable.CaptionAlign = table.CaptionAlign; osTable.CellPadding = table.CellPadding; osTable.CellSpacing = table.CellSpacing; osTable.GridLines = table.GridLines; osTable.HorizontalAlign = table.HorizontalAlign; // Rows TableRow[] tableRows = new TableRow[table.Rows.Count]; table.Rows.CopyTo(tableRows, 0); int i = 0; foreach (OSDataGridItem row in tableRows) { try { // restore the original id of this row, if set if (rowIds != null) { row.ID = OSDataGridItem.FormatId((int)rowIds[i], parent.EnableLegacyRendering); } else { row.ID = OSDataGridItem.FormatId(i + 1, parent.EnableLegacyRendering); } // set the item index, if set if (itemIndexes != null) { row.SetItemIndex((int)itemIndexes[i]); } } catch (ArgumentOutOfRangeException) { throw new HEMessageException(MR.GetMessage(MessageId.AjaxListModified, "Table Records", parent.ClientID)); } osTable.Rows.Add(row); i++; } return(osTable); }
public void RefreshItem(int dataItemIndex) { // set the datasource position IOSList rl = DataSource; rl.SetPosition(dataItemIndex); SelectedIndex = dataItemIndex; // databind the correspondent datagriditem int controlIndex = GetItemControlIndex(dataItemIndex); OSDataGridTable child = (OSDataGridTable)Controls[0]; OSDataGridItem item = (OSDataGridItem)child.Rows[controlIndex]; // initialize item item.DataItem = rl.Current; InitializeItem(item, false); item.DataBind(); }
/// <summary> /// Stores the current row ids to the viewstate /// </summary> public void StoreViewStateRowIdsandItemIndexes() { // store the current ids of every child rows... OSDataGridTable child = Controls[0] as OSDataGridTable; ArrayList rowIds = new ArrayList(child.Rows.Count); ArrayList itemIndexes = new ArrayList(child.Rows.Count); int i = 1; foreach (OSDataGridItem item in child.Rows) { if (item.ID == null) { item.ID = OSDataGridItem.FormatId(i, EnableLegacyRendering); } rowIds.Add(OSDataGridItem.GetIntegerId(item.ID, EnableLegacyRendering)); itemIndexes.Add(item.ItemIndex); i++; } ViewState["RowIds"] = rowIds; ViewState["RowIdxs"] = itemIndexes; }
/// <summary> /// Adds the empty row item if necessary /// </summary> private void AddEmptyRow() { bool any = false; foreach (OSDataGridItem item in this.Controls[0].Controls) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { any = true; break; } } if (!any) { _isEmpty = true; // add empty row... OSDataGridItem dataGridItem = new OSDataGridItem(0, 0, ListItemType.Item); // mark this as empty message item dataGridItem.IsEmptyMessageItem = true; OSDataGridTableCell tableCell = new OSDataGridTableCell(); tableCell.ColumnSpan = TotalColumnSpan; tableCell.CssClass = OddLineStyle; tableCell.Text = EmptyMessage; dataGridItem.Cells.Add(tableCell); OSDataGridTable child = (OSDataGridTable)Controls[0]; child.Rows.Add(dataGridItem); // Making the id item ok based on the number of elements dataGridItem.ID = OSDataGridItem.FormatId(child.Rows.Count, EnableLegacyRendering); } else { _isEmpty = false; } }
/// <summary> /// Inserts an item to the datagrid /// </summary> /// <param name="param name="dataItemIndex"></param> /// <param name="dataItem"></param> public void InsertItem(int dataItemIndex, object dataItem) { // check if the current page is full IOSList rl = DataSource; // update the datasource rl.Insert(dataItem, dataItemIndex); // set the record list position and databind the item rl.SetPosition(dataItemIndex); // create an item at the end of the list OSDataGridItem item = (OSDataGridItem)CreateItem(dataItemIndex, dataItemIndex, ListItemType.Item); DataGridItemEventArgs e = new DataGridItemEventArgs(item); // initialize item item.DataItem = dataItem; InitializeItem(item, true); this.OnItemCreated(e); OSDataGridTable child = (OSDataGridTable)Controls[0]; // remove any empty message row... if (((OSDataGridItem)child.Rows[child.Rows.Count - 1]).IsEmptyMessageItem) { child.Rows.RemoveAt(child.Rows.Count - 1); } // insert the item before the footer and pager items int footerIndex = child.Rows.Count - 2; int footerId; if (child.Rows[footerIndex].ID != null) { footerId = OSDataGridItem.GetIntegerId(child.Rows[footerIndex].ID, EnableLegacyRendering); } else { footerId = footerIndex + 1; } child.Rows.AddAt(footerIndex, item); // generate a new item id for this control int id = footerId; item.ID = OSDataGridItem.FormatId(id, EnableLegacyRendering); // increase the footer and pager items, so that no repateated ids exist in the page child.Rows[child.Rows.Count - 2].ID = OSDataGridItem.FormatId(id + 1, EnableLegacyRendering); child.Rows[child.Rows.Count - 1].ID = OSDataGridItem.FormatId(id + 2, EnableLegacyRendering); ViewState["_!ItemCount"] = rl.Length; _isEmpty = false; // increase higher item indexes in the data grid foreach (OSDataGridItem otherItem in child.Rows) { if (otherItem.ItemIndex >= dataItemIndex && otherItem != item) { otherItem.SetItemIndex(otherItem.ItemIndex + 1); } } // update row ids in the viewstate StoreViewStateRowIdsandItemIndexes(); SelectedIndex = dataItemIndex; item.DataBind(); this.OnItemDataBound(e); item.DataItem = null; }