// <Snippet1>
    private void Clear_Clicked(object sender, System.EventArgs e)
    {
        // TablesAlreadyAdded set to false so that table styles can be added again.
        TablesAlreadyAdded = false;
        myLabel.Text       = "All Table Styles Cleared.";
        // Clear all the column styles and also table style for the grid.
        foreach (DataGridTableStyle myTableStyle in myDataGrid.TableStyles)
        {
            GridColumnStylesCollection myColumns = myTableStyle.GridColumnStyles;
            myColumns.Clear();
        }
        myDataGrid.TableStyles.Clear();
    }
        public void TestRemove()
        {
            DataGridTableStyle         ts = new DataGridTableStyle();
            GridColumnStylesCollection sc = ts.GridColumnStyles;

            sc.CollectionChanged += new CollectionChangeEventHandler(OnCollectionEventHandler);

            // Add single
            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();

            col1.MappingName = "Column1";
            sc.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();

            col2.MappingName = "Column2";
            sc.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();

            col3.MappingName = "Column3";
            sc.Add(col3);

            ResetEventData();
            sc.Remove(col2);
            Assert.AreEqual(true, eventhandled, "A1");
            Assert.AreEqual(col2, Element, "A2");
            Assert.AreEqual(CollectionChangeAction.Remove, Action, "A3");
            Assert.AreEqual(2, sc.Count, "A4");

            ResetEventData();
            sc.RemoveAt(0);
            Assert.AreEqual(true, eventhandled, "A5");
            Assert.AreEqual(col1, Element, "A6");
            Assert.AreEqual(CollectionChangeAction.Remove, Action, "A6");
            Assert.AreEqual(1, sc.Count, "A7");

            ResetEventData();
            sc.Clear();
            Assert.AreEqual(null, Element, "A8");
            Assert.AreEqual(CollectionChangeAction.Refresh, Action, "A9");
        }
Esempio n. 3
0
        public static void FillDataGrid(DataGrid grid, PropertyList columns, DataContent[] rows)
        {
            DataTable dtab = new DataTable();
            DataRow   drow;
            int       colNumber = 0;

            if (columns != null)
            {
                colNumber = columns.Size();
                dtab.Columns.Add("_UIUTilsNo_", typeof(int));
                for (IEnumerator i = columns.GetEnumerator(); i.MoveNext();)
                {
                    DataInfo info = columns.Info();
                    if (info != null)
                    {
                        DataField f = columns.Info().GetField((String)i.Current);
                        dtab.Columns.Add(f.getFieldTitle(), typeof(Object) /*f.getFieldType().ToImplType()*/);
                    }
                    else
                    {
                        dtab.Columns.Add((String)i.Current, typeof(Object));
                    }
                }

                for (int i = 0; i < rows.Length; i++)
                {
                    drow = dtab.NewRow();
                    int col = 0;
                    drow[col++] = i + 1;
                    for (IEnumerator e = columns.GetEnumerator(); e.MoveNext();)
                    {
                        Object o = rows[i].GetProperty((String)e.Current);
                        if (o == null)
                        {
                            o = DBNull.Value;
                        }
                        drow[col++] = o;
                    }
                    dtab.Rows.Add(drow);
                }
            }
            else if (rows != null && rows.Length > 0)
            {
                ICollection cols = rows[0].KeySet();
                dtab.Columns.Add("No", typeof(int));

                for (IEnumerator i = cols.GetEnumerator(); i.MoveNext();)
                {
                    dtab.Columns.Add((String)i.Current, typeof(Object));
                    colNumber++;
                }

                for (int i = 0; i < rows.Length; i++)
                {
                    drow = dtab.NewRow();
                    int col = 0;
                    drow[col++] = i + 1;
                    for (IEnumerator e = cols.GetEnumerator(); e.MoveNext();)
                    {
                        Object o = rows[i].GetProperty((String)e.Current);
                        if (o == null)
                        {
                            o = DBNull.Value;
                        }
                        drow[col++] = o;
                    }
                    dtab.Rows.Add(drow);
                }
            }
            int x = -1;

            if (colNumber > 0)
            {
                x = (grid.ClientSize.Width - 40) / colNumber;
                if (x > 0)
                {
                    grid.PreferredColumnWidth = x;
                }
            }
            grid.DataSource = dtab;
            grid.TableStyles.Clear();
            grid.TableStyles.Add(new DataGridTableStyle());
            GridColumnStylesCollection gcsColl = grid.TableStyles[0].GridColumnStyles;
            ArrayList a = new ArrayList();

            for (int i = 0; i < gcsColl.Count; i++)
            {
                if (x > 0)
                {
                    gcsColl[i].Width    = x;
                    gcsColl[i].NullText = "";
                }
                a.Add(gcsColl[i]);
            }
            gcsColl.Clear();
            UserTextBoxColumn u = new UserTextBoxColumn();

            u.HeaderText  = "";
            u.MappingName = "_UIUTilsNo_";
            u.Width       = 30;
            gcsColl.Add(u);
            for (int i = 1; i < a.Count; i++)
            {
                gcsColl.Add((DataGridColumnStyle)a[i]);
            }

            /*
             * for (int i = 0; i < gcsColl.Count; i++)
             * {
             *      if (gcsColl[i].GetType() == typeof (DataGridTextBoxColumn))
             *      {
             *              DataGridTextBoxColumn textColumn = (DataGridTextBoxColumn) gcsColl[i];
             *              if (textColumn.HeaderText == "No")
             *              {
             *                      textColumn.Width = 30;
             *                      textColumn.TextBox.BackColor=Color.Red;
             *                      //textColumn.HeaderText = "new header text";
             *                      break;
             *              }
             *      }
             * }*/

            if (dtab.Rows.Count > 0)
            {
                grid.CurrentRowIndex = 0;
                grid.Select(0);
            }
            grid.RowHeadersVisible = false;
            grid.RowHeaderWidth    = 1;
            //grid.Focus();
        }