Sunday 15 August 2010

c# - How to display a property instead of the value in a databound DataGridViewComboBox? -


I'm new to C # and .NET

Let me display a matching name for a value Need Datagrid in a database viewbackbox, but I do not understand how to do this.

I have the following code:

  bs = new binding source (); B. Datasource = Type (CR); DataGridView1.AutoGenerateColumns = false; Column 1 Wide = 400; Column1.DataPropertyName = "CR_NAME"; Column 2 Deteport Property Name = "CR_STATE_S"; Column 2 Value = "CR_STATE_S"; Column 2 Displaymile = "GetStateName"; Column2.Items.Add ("0"); // How to set the matching value here? Column2.Items.Add ("1"); Column2.Items.Add ("2"); DataGridView1.DataSource = BS;  

GetStateName is a property of the CR category which gives the matching name of CR status. I need to display the name of the state in the combo box. How to do it Thank you.

If you want to display something different than the values, then in the cell, Instead, one thing is to load the item of the Combo Boxes.

Instead, you have at least different fields for the two things you want to use:

  • A DisplayMember
  • field for the actual representation of the field and the value for the actual data values ​​named ValueMember

This field is a DataTable but you can also use any other archive with the appropriate properties.

We can create a very simple class and create a list of that class:

class item class {public string display {get; Set;} public string value {get; Set; } Public item class (string D, string v) {display = d; Value = V;}} list & lt; Item class & gt; MyItems = new list & lt; Itemclass & gt; (); Private Zero LoadBitton_Link (Object Sender, EventArgse E) {// Load the list with all the values: myItems.Add (new item class ("zero", "0")); MyItems.Add (new item class ("one", "1"); MyItems.Add (new item class ("two", "2"); MyItems.Add (new item class ("three", "3")); MyItems.Add (new item class ("four", "4")); MyItems.Add (new item class ("five", "5"); MyItems.Add (new item class ("six", "6"); // Create DataGride View 'DGV': DGV Columns Claire (); Datagrid vimeobboxcale cCell = new datagriddiving resourcebox (); Datagrid view columbox column CCOL = new datagrid viewcombobe column (); DGV.Columns.Add (cCol); CCol.DisplayMember = "Display"; CCol.ValueMember = "value"; CCol.DataSource = myItems; CCol.ValueType = typeof (string); // Add some lines for the test: Add DGV.ro. (7); For (Int i = 0; I & lt; DGV.ROS.Count; I ++) DGV. ROS [I] [0]. Value = I + ""; }

In the example I manually load items . Generally you want to draw values ​​from the database or any other source. You can do that either by loading the data source list as above or you can do a lookup table either independently or in DataSet

if all Cells require personal lookup values, you need to load them separately and do not use columns, but insert each code on DataGridViewComboBoxCell .


No comments:

Post a Comment