Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Common methods for different objects
Message
De
20/05/2009 11:46:33
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01400964
Message ID:
01400975
Vues:
38
This message has been marked as a message which has helped to the initial question of the thread.
At least three possibilities come to mind

(1) Subclass the grid, and put the methods in that class
The grids you use will be based on that class

(2) create a class that implements those methods
Add it to the grids that will use it
Call the methods of that class

(3) Extension methods
- either for all grids
- or for a subclass of the grid's class


There will be other ways


>Hi everybody,
>
>How can I implement common logic for different grids?
>
>Say, right now I have:
>
>
>  #region Format Order Items Grid (New Order Page)
>        /// <summary>
>        /// Format the Order Items grid.
>        /// </summary>
>        /// <param name="dt"></param>
>        private void FormatOrderItemsGrid(OrderDetailsDS.OrderDetailsDataTable dt)
>        {
>            if (_gridStyleItem == null)
>            {
>                _gridStyleItem = new DataGridTableStyle();
>                this.grdOrderItems.TableStyles.Add(_gridStyleItem);
>                this.grdOrderItems.TableStyles[0].MappingName = dt.TableName;
>                //this.Width = 220;
>                //this.grdOrderItems.Width = 220;
>
>                //Order No column               
>
>                //Order Item Column
>                DataGridTextBoxColumn colManPartNo = new DataGridTextBoxColumn();
>                //colManPartNo.Owner = this.grdOrderItems;
>                colManPartNo.HeaderText = "Item No";
>                colManPartNo.MappingName = "ItemNo";
>                colManPartNo.Width = ((this.grdOrderItems.Width * 20) / 100);
>                //colManPartNo.Alignment = HorizontalAlignment.Left;
>
>                // Unfortunately the Description column is not in OrderDetails table
>                //Order Item Description
>                DataGridTextBoxColumn colDescription = new DataGridTextBoxColumn();
>                //colDescription.Owner = this.grdOrderItems;
>                colDescription.HeaderText = "Description";
>                colDescription.MappingName = "Description";
>                colDescription.Width = ((this.grdOrderItems.Width * 40) / 100);
>                //colDescription.Alignment = HorizontalAlignment.Left;
>
>                DataGridTextBoxColumn colQuantity = new DataGridTextBoxColumn();
>                //colQuantity.Owner = this.grdOrderItems;
>                colQuantity.HeaderText = "Qty";
>                colQuantity.MappingName = "Qty";
>                colQuantity.Width = ((this.grdOrderItems.Width * 10) / 100);
>                //colQuantity.Alignment = HorizontalAlignment.Center;
>
>                //Order Item Price
>                DataGridTextBoxColumn colPrice = new DataGridTextBoxColumn();
>                //colPrice.Owner = this.grdOrderItems;
>                colPrice.HeaderText = "Price";
>                colPrice.MappingName = "Price";
>                colPrice.Width = ((this.grdOrderItems.Width * 15) / 100);
>                //colPrice.Alignment = HorizontalAlignment.Right;
>                // Setup table mapping name
>
>                //Order Item Total Price
>                DataGridTextBoxColumn colTotalPrice = new DataGridTextBoxColumn();
>                //colTotalPrice.Owner = this.grdOrderItems;
>                colTotalPrice.HeaderText = "Total";
>                colTotalPrice.MappingName = "TotalPrice";
>                colTotalPrice.Width = ((this.grdOrderItems.Width * 15) / 100);
>                //colTotalPrice.Alignment = HorizontalAlignment.Right;
>
>                // Setup table mapping name
>                this.grdOrderItems.TableStyles[0].GridColumnStyles.Add(colManPartNo);
>                this.grdOrderItems.TableStyles[0].GridColumnStyles.Add(colDescription);
>                this.grdOrderItems.TableStyles[0].GridColumnStyles.Add(colQuantity);
>                this.grdOrderItems.TableStyles[0].GridColumnStyles.Add(colPrice);
>                this.grdOrderItems.TableStyles[0].GridColumnStyles.Add(colTotalPrice);
>                this.grdOrderItems.RowHeadersVisible = false;
>            }
>            this.grdOrderItems.DataSource = dt;
>            this.grdOrderItems.Refresh();
>        }
>        #endregion
>        #region Format Order Details Grid (View page)
>        private void FormatOrderDetailsGrid(OrderDetailsDS.OrderDetailsDataTable dt)
>        {
>            if (_gridStyleItem == null)
>            {
>                _gridStyleItem = new DataGridTableStyle();
>                this.grdOrderDetails.TableStyles.Add(_gridStyleItem);
>                this.grdOrderDetails.TableStyles[0].MappingName = dt.TableName;
>                //this.Width = 220;
>                //this.grdOrderItems.Width = 220;
>
>                //Order No column               
>
>                //Order Item Column
>                DataGridTextBoxColumn colManPartNo = new DataGridTextBoxColumn();
>                //colManPartNo.Owner = this.grdOrderItems;
>                colManPartNo.HeaderText = "Item No";
>                colManPartNo.MappingName = "ItemNo";
>                colManPartNo.Width = ((this.grdOrderDetails.Width * 20) / 100);
>                //colManPartNo.Alignment = HorizontalAlignment.Left;
>
>                // Unfortunately the Description column is not in OrderDetails table
>                //Order Item Description
>                DataGridTextBoxColumn colDescription = new DataGridTextBoxColumn();
>                //colDescription.Owner = this.grdOrderItems;
>                colDescription.HeaderText = "Description";
>                colDescription.MappingName = "Description";
>                colDescription.Width = ((this.grdOrderDetails.Width * 40) / 100);
>                //colDescription.Alignment = HorizontalAlignment.Left;
>
>                DataGridTextBoxColumn colQuantity = new DataGridTextBoxColumn();
>                //colQuantity.Owner = this.grdOrderItems;
>                colQuantity.HeaderText = "Qty";
>                colQuantity.MappingName = "Qty";
>                colQuantity.Width = ((this.grdOrderDetails.Width * 10) / 100);
>                //colQuantity.Alignment = HorizontalAlignment.Center;
>
>                //Order Item Price
>                DataGridTextBoxColumn colPrice = new DataGridTextBoxColumn();
>                //colPrice.Owner = this.grdOrderItems;
>                colPrice.HeaderText = "Price";
>                colPrice.MappingName = "Price";
>                colPrice.Width = ((this.grdOrderDetails.Width * 15) / 100);
>                //colPrice.Alignment = HorizontalAlignment.Right;
>                // Setup table mapping name
>
>                //Order Item Total Price
>                DataGridTextBoxColumn colTotalPrice = new DataGridTextBoxColumn();
>                //colTotalPrice.Owner = this.grdOrderItems;
>                colTotalPrice.HeaderText = "Total";
>                colTotalPrice.MappingName = "TotalPrice";
>                colTotalPrice.Width = ((this.grdOrderDetails.Width * 15) / 100);
>                //colTotalPrice.Alignment = HorizontalAlignment.Right;
>
>                // Setup table mapping name
>                this.grdOrderDetails.TableStyles[0].GridColumnStyles.Add(colManPartNo);
>                this.grdOrderDetails.TableStyles[0].GridColumnStyles.Add(colDescription);
>                this.grdOrderDetails.TableStyles[0].GridColumnStyles.Add(colQuantity);
>                this.grdOrderDetails.TableStyles[0].GridColumnStyles.Add(colPrice);
>                this.grdOrderDetails.TableStyles[0].GridColumnStyles.Add(colTotalPrice);
>                this.grdOrderDetails.RowHeadersVisible = false;
>            }
>            this.grdOrderDetails.DataSource = dt;
>            this.grdOrderDetails.Refresh();        }
>
>        #endregion
>        #endregion
>
>The code is exactly the same but applied to different grids, would I be able to pass the grid as a parameter and use GridParameter instead of this.grdOrderDetails through the code?
>
>I also have the same problem with MouseUp event. I want to use the same code for two different grids with only slight difference that I want to set one private member of my form class in case of grdSearch and another in case of grdOrderDetails. What would be good way to implement this?
>
>Thanks a lot in advance.
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform