In databases, we often use some simple values (such as 0, 1, and 2) to indicate certain meanings, such as: Yes/No, male/female, correct/wrong, married/unmarried/divorced.
But how do I display it in the DataGridView control?
One way is to use the select Case statement.
The two methods are customized using the DataGridView column.
The second method is used here.
When setting the DataGridView column attribute, set ColumnType to DataGridViewComboBoxColumn, DisplayStyle to Nothing, and then code:
Enter the following code during the form loading process:
View sourceprint?
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).DisplayMember = "text" ; |
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).ValueMember = "value" ; |
DataTable dtPayMode = new DataTable(); |
dtPayMode.Columns.Add( "text" ); |
dtPayMode.Columns.Add( "value" , typeof ( byte )); |
dtPayMode.Rows.Add( new object [] { "Cash" , 0 }); |
dtPayMode.Rows.Add( new object [] { "Swipe card" , 1 }); |
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).DataSource = dtPayMode; |
In this way, when binding data to the DataGridView, the corresponding Chinese characters are automatically displayed based on the status of 0 or 1. For example:
In databases, we often use some simple values (such as 0, 1, and 2) to indicate certain meanings, such as: Yes/No, male/female, correct/wrong, married/unmarried/divorced.
But how do I display it in the DataGridView control?
One way is to use the select Case statement.
The two methods are customized using the DataGridView column.
The second method is used here.
When setting the DataGridView column attribute, set ColumnType to DataGridViewComboBoxColumn, DisplayStyle to Nothing, and then code:
Enter the following code during the form loading process:
View sourceprint?
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).DisplayMember = "text" ; |
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).ValueMember = "value" ; |
DataTable dtPayMode = new DataTable(); |
dtPayMode.Columns.Add( "text" ); |
dtPayMode.Columns.Add( "value" , typeof ( byte )); |
dtPayMode.Rows.Add( new object [] { "Cash" , 0 }); |
dtPayMode.Rows.Add( new object [] { "Swipe card" , 1 }); |
((DataGridViewComboBoxColumn)gvSaleOrder.Columns[ "payMode" ]).DataSource = dtPayMode; |
In this way, when binding data to the DataGridView, the corresponding Chinese characters are automatically displayed based on the status of 0 or 1. For example: