How to make the DataGridView display "yes" or "no" Based on the Data "0" or "1" equivalent"

Source: Internet
Author: User
Tags case statement

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.