Winform DataGridView控件指定某列显示为复选框CheckBox, 绑定数据源DataTable

窗体拖进一个DataGridView控件,在事件中写入以下代码
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("选择", typeof(bool));

            // set column
            DataColumn dcId = new DataColumn("标题", typeof(string));
            dcId.ReadOnly = true;
            dt.Columns.Add(dcId);

            // set value
            for (int i = 0; i < 10; ++i)
            {
                DataRow newRow = dt.NewRow();
                newRow["选择"] = 0;
                newRow["标题"] = "标题 - " + i.ToString();
                dt.Rows.Add(newRow);
            }

            // bind source
            dataGridView1.DataSource = dt;
        }

效果图如下


C# 社区推荐