C#datagridview Data Binding Example-format cell contents

Source: Internet
Author: User

C#datagridview data binding Sample formatting cell contents

When using DataGridView to display data from a database, we need to format the data display format for a column.

This uses data that is built in real time, such as:


Formats the data for the third column when it is displayed, such as:


Test data building and data binding:

        private void Form1_Load (object sender, EventArgs e)        {            DataTable dt = new DataTable ();            Dt. Columns.Add (New DataColumn ("Id"));            Dt. Columns.Add (New DataColumn ("Name"));            Dt. Columns.Add (New DataColumn ("Bit"));            Dt. Rows.Add (new object[] {"1", "ABCD", "0"});            Dt. Rows.Add (new object[] {"2", "ABCD", "1"});            Dt. Rows.Add (new object[] {"3", "ABCD", "0"});            Dt. Rows.Add (new object[] {"4", "ABCD", "1"});            Dt. Rows.Add (new object[] {"5", "ABCD", "1"});            Dt. Rows.Add (new object[] {"6", "ABCD", "0"});            Datagridview1.autogeneratecolumns = false;            Datagridview1.datasource = dt;        }
Cell Data formatting:
private void Datagridview1_cellformatting (object sender, Datagridviewcellformattingeventargs e)        {            if (Column _bit. Index = = E.columnindex)            {                if (E.value = = null)                {                    return;                }                if (E.value.equals ("0"))                {                    e.value = "no";                }                else                {                    E.value = "yes";}}        }

SOURCE Download: C#datagridview data Binding Sample Format cell content source code

Related Article

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.