DataGridView的一般用法(1)

本文介绍了 DataGridView 控件的基本用法,包括获取单元格属性、设置编辑属性、调整行列显示及尺寸等,并提供了取得选定行、列及单元格的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据。它的一般用法如下:

 

 

一.取得当前单元格的属性

1.       取得当前选中单元格的值

DataGridView1.CurrentCell.Value


2.       当前所在单元格所在列

 DataGridView1.CurrentCell.ColumnIndex


3.       当前所在单元格所在行

DataGridView1.CurrentCell.RowIndel


4.       将控件中(0,0)处的值赋给当前单元格

 DataGridView1.CurrentCell=DataGridView1(0,0)


二.DataGridView编辑属性

1.第二行设置为只读属性

DataGridView1.Rows.RemoveAt(0)


 

DataGridView1.Rows(2).ReadOnly=True


三.DataGridView行列不表示或者删除

1.行表头不表示

 DataGridView1.RowHeadersVisible=False


2.指定行删除

DataGridView1.Rows.RemoveAt(0)


四.行列宽度高度设置为不能编辑

 

DataGridView1.AllowUserToResizeColumns=False
DataGridView1.AllowUserToResizeRows = False


2. 指定行列宽度高度不能编辑

DataGridView1.Columns(0).Resizable=DataGridViewTriState.False
DataGridView1.Rows(0).Resizable = DataGridViewTriState.False


五.DataGridView选择的行、列、单元格取得

For Each c As DataGridViewCell In DataGridView1.SelectedCells  

    Console.WriteLine("{0}, {1}", c.ColumnIndex, c.RowIndex)  

Next c

For Each r As DataGridViewRow In DataGridView1.SelectedRows  

    Console.WriteLine(r.Index)  

Next r

For Each c As DataGridViewColumn In DataGridView1.SelectedColumns  

    Console.WriteLine(c.Index)  

Next c


六.取得指定单元格

DataGridView1(0, 0).Selected = True

DataGridView1.Rows(1).Selected = True

DataGridView1.Columns(2).Selected = True


 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值