DataGridView之为每行前面添加序号

最简单的方法是在Datagridview的事件RowPostPaint事件下面添加如下代码即可

 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);


        }
好的,这是您的问题:vb.net DataGridView指定列循环添加DataGridViewCheckBoxColumn,每行代码注释。 以下是代码示例和注释: ``` ' 获取DataGridView控件中指定列的索引,这里假设列名为"CheckBoxColumn" Dim columnIndex As Integer = DataGridView1.Columns("CheckBoxColumn").Index ' 遍历DataGridView中的每一行 For Each row As DataGridViewRow In DataGridView1.Rows ' 创建一个新的DataGridViewCheckBoxCell对象 Dim newCheckBoxCell As New DataGridViewCheckBoxCell() ' 将新的DataGridViewCheckBoxCell对象赋值给指定列 row.Cells(columnIndex) = newCheckBoxCell ' 可以设置该列的其他属性,比如列的标题 DataGridView1.Columns(columnIndex).HeaderText = "选择" ' 可以设置该列的宽度 DataGridView1.Columns(columnIndex).Width = 50 ' 可以设置该列的对齐方式 DataGridView1.Columns(columnIndex).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter ' 可以设置该列的单元格的背景颜色 DataGridView1.Columns(columnIndex).DefaultCellStyle.BackColor = Color.LightGray ' 可以设置该列的单元格的字体 DataGridView1.Columns(columnIndex).DefaultCellStyle.Font = New Font("宋体", 10, FontStyle.Bold) ' 可以设置该列的单元格的边框样式 DataGridView1.Columns(columnIndex).DefaultCellStyle.BorderStyle = DataGridViewCellBorderStyle.Single ' 可以设置该列的单元格的文本颜色 DataGridView1.Columns(columnIndex).DefaultCellStyle.ForeColor = Color.DarkRed ' 可以设置该列的单元格的值 newCheckBoxCell.Value = False ' 可以设置该列的单元格是否可以编辑 newCheckBoxCell.ReadOnly = False ' 可以设置该列的单元格的提示文本 newCheckBoxCell.ToolTipText = "请勾选该项" ' 可以设置该列的单元格的样式 newCheckBoxCell.Style = DataGridView1.Columns(columnIndex).DefaultCellStyle Next ``` 以上就是循环添加DataGridViewCheckBoxColumn并添加注释的示例代码,希望能够帮到您。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值