Zdrojové kódy pro vývojáře.
Přeskočit odkazy pro navigaci Top 10 přispěvatelů
UživatelČlánky
codeshare45
sochor1
stoupa1
tomas.oplt15
Článek: Pomocník pro práci s gridem od společnosti Infragistics
Špatný Super
Autor:
Vytvořeno:
Popularita:

V profesionálních projektech je třeba používat profesionální ovládací prvky.
Dobrou volbou je určitě použití ovládacích prvků od společnosti Infragistics.
Naprogramovat a předělat je možné všechno, ale ruku na srdce: Kdo by trávil několik desítek dnů vylepšováním prvku 
GridView když je možné koupit prvek, který obsahuje všechny běžné funkce:

Vyhledávání v řádcích
Zobrazení /  Schování sloupců
Získání označení řádky z UltraGridRow
Získání textu/hodnoty z označené řádky a sloupce
Vytvoření ComboBoxu ve sloupci gridu 
Model  UltraGridExt který má další vlastnosti UltraGridExtProps 
 

 


namespace ControlHelper
{
    ///
    /// DataGrid
    ///
    public class UltraGridExt : Infragistics.Win.UltraWinGrid.UltraGrid
    {

 

        ///


        /// UltraGrid properties
        ///
        private UltraGridExtProps m_Properties;

        ///


        /// Constructor
        ///
        public UltraGridExt()
        {
            m_Properties = new UltraGridExtProps(this);
            this.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.OnInInitializeLayout);
            this.Enter += new EventHandler(UltraGridExt_Enter);
            this.Leave += new EventHandler(UltraGridExt_Leave);
        }

        ///


        /// UltraGrid properties
        ///
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), TypeConverter(typeof(ExpandableObjectConverter))]
        public UltraGridExtProps Props
        {
            get
            {
                return m_Properties;
            }
        }

        private void OnInInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            this.DisplayLayout.TabNavigation = Infragistics.Win.UltraWinGrid.TabNavigation.NextControl;
        }

        private void UltraGridExt_Enter(object sender, EventArgs e)
        {
            if (this.DisplayLayout.BorderStyle == Infragistics.Win.UIElementBorderStyle.Default)
            {
                this.DisplayLayout.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
            }
        }

        private void UltraGridExt_Leave(object sender, EventArgs e)
        {
            if (this.DisplayLayout.BorderStyle == Infragistics.Win.UIElementBorderStyle.Solid)
            {
                this.DisplayLayout.BorderStyle = Infragistics.Win.UIElementBorderStyle.Default;
            }
        }
    }

    ///


    /// Třída vlastností controlu - možno doplnit nové vlastnosti, které budou serializovatelné
    ///
    public class UltraGridExtProps
    {
        //Private variables
        private UltraGridExt ctrl;
        private bool m_bDisplayAlternateRows;
        private bool m_bSearchRows;
        public delegate void EBDoubleClickDelegate(object sender, System.EventArgs e);
        public event EBDoubleClickDelegate InfraDoubleClick;

        //Constructor
        public UltraGridExtProps(UltraGridExt ctrl)
        {
            this.ctrl = ctrl;
            m_bDisplayAlternateRows = true;
            m_bSearchRows = true;
            this.ctrl.DoubleClick += new EventHandler(grd_DoubleClick);
        }

        public bool IsDoubleClickedInGrid()
        {
            Infragistics.Win.UIElement elementUnderMouse = this.ctrl.DisplayLayout.UIElement.ElementFromPoint(
            this.ctrl.PointToClient(System.Windows.Forms.Cursor.Position));

            // If user clicked not inside grid ( scrollbar ) return back
            if (elementUnderMouse == null) return false;
            if (elementUnderMouse is Infragistics.Win.UltraWinScrollBar.ScrollThumbUIElement) return false;
            if (elementUnderMouse is Infragistics.Win.UltraWinScrollBar.ScrollTrackSubAreaUIElement) return false;
            if (elementUnderMouse is Infragistics.Win.UltraWinScrollBar.ScrollTrackUIElement) return false;
            if (elementUnderMouse is Infragistics.Win.UltraWinScrollBar.ScrollArrowUIElement) return false;
            if (elementUnderMouse is Infragistics.Win.ScrollbarIntersectionUIElement) return false;

            return true;
        }

        private void grd_DoubleClick(object sender, EventArgs e)
        {
            if (IsDoubleClickedInGrid() == false) return;
            if (InfraDoubleClick != null) InfraDoubleClick(this, EventArgs.Empty);
        }

        ///


        /// Alternative row
        ///
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(typeof(bool), "True")]
        public bool AlternateRows
        {
            get
            {
                return m_bDisplayAlternateRows;
            }
            set
            {
                m_bDisplayAlternateRows = value;
            }
        }

        ///


        /// Search bar inside grid
        ///
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(typeof(bool), "True")]
        public bool SearchRow
        {
            get
            {
                return m_bSearchRows;
            }
            set
            {
                m_bSearchRows = value;
            }
        }

   

        ///


        /// Select row inside grid
        ///
        /// Field name
        /// Search value
        ///
        public bool SelectRowByValue(string strField, string strVal)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow gridRow;
            gridRow = this.ctrl.GetRow(Infragistics.Win.UltraWinGrid.ChildRow.First);

            while (gridRow != null)
            {
                if (gridRow.Cells[strField].Value != DBNull.Value)
                {
                    if (gridRow.Cells[strField].Value.ToString() == strVal)
                    {
                        this.ctrl.ActiveRow = gridRow;
                        this.ctrl.ActiveRow.Selected = true;
                        return true;
                    }
                }
                gridRow = gridRow.GetSibling(Infragistics.Win.UltraWinGrid.SiblingRow.Next);
            }
            return false;
        }

        ///


        /// Select row inside grid
        ///
        /// Field name
        /// Search value
        ///
        ///
        public bool SelectRowByValue(string strField, string strVal, int intBandIndex)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow gridRow;
            gridRow = this.ctrl.GetRow(Infragistics.Win.UltraWinGrid.ChildRow.First);
            if (gridRow != null)
            {
                TraverseAllGridRows(gridRow, strField, strVal, intBandIndex);
            }
            return false;
        }

        ///


        /// Returns true if Daatfield with value exists
        ///
        /// Field name
        /// Search value
        ///
        public bool ExistRow(string strField, string strVal)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow gridRow;
            gridRow = this.ctrl.GetRow(Infragistics.Win.UltraWinGrid.ChildRow.First);
            while (gridRow != null)
            {
                if (gridRow.Cells[strField].Value != DBNull.Value)
                {
                    if (gridRow.Cells[strField].Value.ToString() == strVal)
                    {
                        return true;
                    }
                }
                gridRow = gridRow.GetSibling(Infragistics.Win.UltraWinGrid.SiblingRow.Next);
            }
            return false;
        }

      
        ///


        /// Looks for all bands inside grid and search for strVal in strField. if searching is sucesfull. Row is selected
        ///
        /// Startovaci radka od ktere budeme hledat i v podrizenych bandech
        /// Field ve kterem bude hledana hodnota
        /// Hledana hodnota
        /// Band ve kterem budeme hledat
        ///
        private bool TraverseAllGridRows(Infragistics.Win.UltraWinGrid.UltraGridRow startRow, string strField, string strVal, int intBandIndex)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow row = startRow;
            while (row != null)
            {
                if (row is Infragistics.Win.UltraWinGrid.UltraGridGroupByRow)
                {
                }
                else
                {
                    if (row.Band.Index == intBandIndex)
                    {
                        if (row.Cells[strField].Value != DBNull.Value)
                        {
                            if (row.Cells[strField].Value.ToString() == strVal)
                            {
                                this.ctrl.ActiveRow = row;
                                this.ctrl.ActiveRow.Selected = true;
                                return true;
                            }
                        }
                    }
                }
                // If the row has any child rows, then process them too.
                if (row.HasChild(false))
                {
                    bool res = this.TraverseAllGridRows(row.GetChild(Infragistics.Win.UltraWinGrid.ChildRow.First), strField, strVal,intBandIndex);
                    if (res == true) return true;
                }

                row = row.GetSibling(Infragistics.Win.UltraWinGrid.SiblingRow.Next, true, false);
            }

            return false;
        }


        ///


        /// Gets DataRow for selected row inside grid
        /// Datagrid instance
        ///
        /// Selected DataRow from datagrid
        public static System.Data.DataRow GetActiveGridRow(Infragistics.Win.UltraWinGrid.UltraGrid grdGrid)
        {
            if (grdGrid.ActiveRow == null) return null;
            DataRowView rowView = grdGrid.ActiveRow.ListObject as DataRowView;
            if (rowView == null) return null;
            if (rowView.Row.RowState == DataRowState.Detached || rowView.Row.RowState == DataRowState.Deleted) return null;
            return rowView.Row;
        }

        ///


        /// Gets DataRow for UltraRow
        /// UltraRow from datagrid
        ///
        /// DataRow from DataTable
        public static System.Data.DataRow GetActiveGridRow(Infragistics.Win.UltraWinGrid.UltraGridRow UltraRow)
        {
            if (UltraRow == null) return null;
            DataRowView rowView = UltraRow.ListObject as DataRowView;
            if (rowView == null) return null;
            if (rowView.Row.RowState == DataRowState.Detached || rowView.Row.RowState == DataRowState.Deleted) return null;
            return rowView.Row;
        }


        ///


        /// Gets DataRow for selected row inside grid
        ///
        /// DataRow from DataTable
        public System.Data.DataRow GetActiveGridRow()
        {
            return ControlHelper.UltraGridExtProps.GetActiveGridRow(this.ctrl);
        }


        ///


        /// gets value from actually selected Datagrid row
        ///
        /// DataGrid instance
        /// DataFieldName
        /// empty string in case of null value otherwise value
        public static string GetActiveGridRowValue(Infragistics.Win.UltraWinGrid.UltraGrid grdGrid, string strField)
        {
            DataRow row = GetActiveGridRow(grdGrid);
            if (row == null) return null;
            return NL(row[strField], "");
        }

        ///


        /// gets value from actually selected Datagrid row
        ///
        /// sloupec, ze kterého bude vrácena hodnota
        /// prázdý řetězec v případě DBNull nebo null hodnoty, jinak hodnota z DataRow
        public string GetActiveGridRowValue(string strField)
        {
            return ControlHelper.UltraGridExtProps.GetActiveGridRowValue(this.ctrl, strField);
        }

        ///


        /// gets value from actually selected Datagrid row
        ///
        /// sloupec, ze kterého bude vrácena hodnota
        /// prázdý řetězec v případě DBNull nebo null hodnoty, jinak hodnota z DataRow
        public int GetActiveGridRowValue(string strField, int intValueIfNull)
        {
            string strResult = ControlHelper.UltraGridExtProps.GetActiveGridRowValue(this.ctrl, strField);
            if (strResult == null) return intValueIfNull;
            return Convert.ToInt32(strResult);
        }

      

        ///


        /// Show only columns  specified in parameter cols
        ///
        /// DataGrid instance
        /// column names
        public static void ShowDataGridColumns(Infragistics.Win.UltraWinGrid.UltraGridBase grd, string[] cols)
        {
            bool bFound;
            foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn col in grd.DisplayLayout.Bands[0].Columns)
            {
                bFound = false;
                foreach (string str in cols)
                {
                    if (col.Key.ToUpper() == str.ToUpper())
                    {
                        bFound = true;
                        break;
                    }
                }

                col.Hidden = !bFound;
            }
        }

        ///


        /// Show only columns  specified in parameter cols
        ///
        /// column names
        public void ShowDataGridColumns(string[] cols)
        {
            ControlHelper.UltraGridExtProps.ShowDataGridColumns(this.ctrl, cols);
        }
       
 
        ///
        /// Crate multiComboBox columns
        ///
        ///
        ///
        ///
        ///
        ///
        ///
        ///
        public Infragistics.Win.UltraWinGrid.UltraDropDown CreateMultiComboBoxColumns(string strColumnName, string strDisplayMember, string strValueMember, DataTable dtData, int intBandIndex, params string[] list)
        {
            Infragistics.Win.UltraWinGrid.UltraDropDown UltraDropDown1;
            UltraDropDown1 = new Infragistics.Win.UltraWinGrid.UltraDropDown();

            this.ctrl.Parent.Controls.Add(UltraDropDown1);
            UltraDropDown1.Location = new System.Drawing.Point(332, 455);
            UltraDropDown1.Size = new System.Drawing.Size(4, 6);
            UltraDropDown1.TabIndex = 1;
            UltraDropDown1.Visible = false;
            UltraDropDown1.DisplayMember = strDisplayMember;
            UltraDropDown1.ValueMember = strValueMember;
            UltraDropDown1.Name = "UltraDropDown_" + strColumnName;
            UltraDropDown1.DataSource = dtData;
            UltraDropDown1.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            UltraGridExtProps.ShowDataGridColumns(UltraDropDown1, list);
            UltraDropDown1.DisplayLayout.Bands[0].Columns[strDisplayMember].Width = this.ctrl.DisplayLayout.Bands[0].Columns[strColumnName].Width;

            this.ctrl.DisplayLayout.Bands[0].Columns[strColumnName].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
            this.ctrl.DisplayLayout.Bands[0].Columns[strColumnName].ValueList = UltraDropDown1;
            this.ctrl.DisplayLayout.Bands[0].Columns[strColumnName].ButtonDisplayStyle = Infragistics.Win.UltraWinGrid.ButtonDisplayStyle.Always;
            return UltraDropDown1;
        }

        ///


        /// Create combobox inside datagrid
        ///
        ///
        ///
        ///
        ///
        ///
        ///
        public void CreateComboBoxColumns(string strColName, string strDisplayMember, string strValueMember, DataTable dtData, int intBandIndex, params string[] list)
        {

            Infragistics.Win.UltraWinGrid.UltraGridColumn col;
            col = this.ctrl.DisplayLayout.Bands[intBandIndex].Columns[strColName];
            if (col == null)
                throw new Exception("Column name not found.");

            col.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
            col.AutoEdit = true;

            Infragistics.Win.ValueList vl = this.ctrl.DisplayLayout.ValueLists.Add("vl" + strColName);
            foreach (DataRow row in dtData.Rows)
            {
                vl.ValueListItems.Add(NL(row[strValueMember], ""), NL(row[strDisplayMember], ""));
            }
            col.ValueList = vl;
            col.ButtonDisplayStyle = Infragistics.Win.UltraWinGrid.ButtonDisplayStyle.Always;
        }

        public static object NL(object value, object objValueIfNull)
        {
            if (value == DBNull.Value || value == null) return objValueIfNull;
            return value;
        }

        public static int NL(object value, int intValueIfNull)
        {
            if (value == DBNull.Value) return intValueIfNull;
            string pom = value.ToString();
            pom = pom.TrimEnd(' ');

            if (pom == "" || IsNumeric(pom) == false)
            {
                return intValueIfNull;
            }
            else
            {
                return Convert.ToInt32(pom);
            }
        }
     
      
        public static string NL(object value, string strValueIfNull)
        {
            if (value == DBNull.Value) return strValueIfNull;
            string pom = value.ToString();
            pom = pom.TrimEnd(' ');
            return pom;
        }

        public static bool IsNumeric(object objValue)
        {
            string strValue = objValue.ToString();
            double dblOut = 0;
            return double.TryParse(strValue, System.Globalization.NumberStyles.Number, System.Threading.Thread.CurrentThread.CurrentCulture, out dblOut);
        }
    }

}

  Na stránku 
screen  Nový příspěvek
Název  Uživatel  Datum 
Poslední návštěva: 20:50:15, 22. září 2017 První  Předchozí  0 Záznamů  Další  Poslední  

Autor článku
Jméno
Pracovní pozice
Informace
Foto

   

Počet návštěvníků:56
 
  Kontakt