Applicable environment: winform development and vs2008 development tools
Problem description:
- There are a series of products, each product has a unique product ID, product name.
- You need to bind the product to the drop-down box (ComboBox). Because of the winform drop-down box control, unlike the drop-down box control in web development, you can directly add two elements to each item (Value and text ). However, it allows you to directly add object items, so it is more flexible.
- The product information needs to be displayed in the drop-down box, not just the simple display name, but some special processing is required, so it cannot be used.
Cboxproduct. displaymember = "productname ";
Cboxproduct. valuemember = "productid ";
Cboxproduct. datasource = datatable; // Product Data Source
4. You must be able to automatically select a specific product based on the product information.
Solution:
- First, we can define a product information class that contains various product information. The required information can be used as a constructor parameter so that the information must be set for the instantiated product class. At the same time, we can also extract a public class for binding the drop-down box information, because in most cases, we generally use the drop-down box to bind only two types of information. However, the data format of the information is not fixed, so you can consider generics.
- Create a data class in the public drop-down box using generics, which contains two attributes: Value and text.
- The Value Attribute must be unique as the index value of the drop-down box. Therefore, when determining whether the data items in the drop-down box are the same, it can be determined by determining whether the value is the same (it must be noted that here we have not forcibly determined whether the value is unique, you can determine when binding data ), to determine whether two objects are equal, consider rewriting the equal method.
Let's look at a practical example!
InstanceCode:
- Create a product type class (producttype class). Because the product type can be an unlimited category (the sub-product type levels under a product type are not fixed, there may be many sub-types ), when the product type is displayed in the drop-down list, the Parent and Child product types must be differentiated. The code for the product type is as follows:
Class producttype <t, TK>
{
Public producttype (T value, TK text)
{
_ Value = value;
_ Text = text;
}
Private T _ value;
/// <Summary>
/// Value
/// </Summary>
Public T value
{
Get {return _ value ;}
Set {_ value = value ;}
}
Private TK _ text;
/// <Summary>
/// Display value
/// </Summary>
Public TK text
{
Get {return _ text ;}
Set {_ text = value ;}
}
Public override string tostring ()
{
Return _ text. tostring ();
}
Public override bool equals (Object OBJ)
{
Return (producttype <t, TK>) OBJ). value. Equals (_ value );
}
}
- Create a test form and add a drop-down box control (ComboBox) named cboxproducttype. Bind the product type data to the drop-down list. Binds the product type cyclically and recursively traverses its subproduct types. The implementation code is as follows:
Private void bindingproducttype (INT parentproducttypeid, string splitstr)
{
VaR datasource = bllproducttype. getlist ("parentid =" + parentproducttypeid). Tables [0];
Foreach (datarow in datasource. Rows)
{
VaR producttypeid = convert. toint32 (datarow ["producttypeid"]);
Cboxproducttype. Items. Add (New producttype () <int, string> (producttypeid,
Splitstr + datarow ["producttypename"]);
Bindingproducttype (producttypeid, splitstr + @ "-- | ");
}
}
- After a product type ID is provided, the product type is automatically selected in the drop-down box. You can use the cboxproducttype. selecteditem attribute to set the product type. For details, refer to the Code:
Producttype <int, string> producttype = new producttype (producttypeid ,"");
Cboxproducttype. selecteditem = producttype;
Because the product type ID is a unique value, we only compare the product type with the value. Therefore, when instantiating the producttype class, the text attribute value is not required, because the text property value has nothing to do with determining whether the product type is equal. (In this case, fill in and select existing data when editing data .)