C # data binding example of the DataGridView -- format the content of a cell
C # format the cell content in the data binding example of the DataGridView.
When using the DataGridView to display data in the database, we need to format the data display format of a column.
Real-time data is used here, for example:
Format the data in the third column during display, for example:
Test data construction 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 maid (object sender, maid 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 code download: c # DataGridView data binding example format the content source code of cells