C# DataGridView操作

C# DataGridView操作 (2009-05-17 21:26:42)
目录:

 

取得或者修改当前单元格的内容

设定单元格只读

不显示最下面的新行

判断新增行

行的用户删除操作的自定义

行、列的隐藏和删除

禁止列或者行的Resize

★ DataGridView 取得或者修改当前单元格的内容:

GO TO TOP

当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null)

[VB.NET]

' 取得当前单元格内容

Console.WriteLine(DataGridView1.CurrentCell.Value)

' 取得当前单元格的列 Index

Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex)

' 取得当前单元格的行 Index

Console.WriteLine(DataGridView1.CurrentCell.RowIndex)

[C#]

// 取得当前单元格内容

Console.WriteLine(DataGridView1.CurrentCell.Value);

// 取得当前单元格的列 Index

Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);

// 取得当前单元格的行 Index

Console.WriteLine(DataGridView1.CurrentCell.RowIndex);

另外,使用 DataGridView.CurrentCellAddress 属性(而不是直接访问单元格)来确定单元格所在的行:DataGridView.CurrentCellAddress.Y 和列: DataGridView.CurrentCellAddress.X 。这对于避免取消共享行的共享非常有用。

当前的单元格可以通过设定 DataGridView 对象的 CurrentCell 来改变。可以通过 CurrentCell 来设定

DataGridView 的激活单元格。将 CurrentCell 设为 Nothing(null) 可以取消激活的单元格。

[VB.NET]

' 设定 (0, 0) 为当前单元格

DataGridView1.CurrentCell = DataGridView1(0, 0)

[C#]

// 设定 (0, 0) 为当前单元格

DataGridView1.CurrentCell = DataGridView1[0, 0];

在整行选中模式开启时,你也可以通过 CurrentCell 来设定选定行。

/// <summary>

/// 向下遍历

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button4_Click(object sender, EventArgs e)

...{

int row = this.dataGridView1.CurrentRow.Index + 1;

if (row > this.dataGridView1.RowCount - 1)

row = 0;

this.dataGridView1.CurrentCell = this.dataGridView1[0, row];

}

/// <summary>

/// 向上遍历

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button5_Click(object sender, EventArgs e)

...{

int row = this.dataGridView1.CurrentRow.Index - 1;

if (row < 0)

row = this.dataGridView1.RowCount - 1;

this.dataGridView1.CurrentCell = this.dataGridView1[0, row];

}

* 注意: this.dataGridView 的索引器的参数是: columnIndex, rowIndex 或是 columnName, rowIndex

这与习惯不同。

--------------------------------------------------------------------------------

★ DataGridView 设定单元格只读:

GO TO TOP

1) 使用 ReadOnly 属性

? 如果希望,DataGridView 内所有单元格都不可编辑, 那么只要:

[VB.NET]

' 设置 DataGridView1 为只读

DataGridView1.ReadOnly = True

[C#]

// 设置 DataGridView1 为只读

DataGridView1.ReadOnly = true;此时,用户的新增行操作和删除行操作也被屏蔽了。

? 如果希望,DataGridView 内某个单元格不可编辑, 那么只要:

[VB.NET]

' 设置 DataGridView1 的第2列整列单元格为只读

DataGridView1.Columns(1).ReadOnly = True

' 设置 DataGridView1 的第3行整行单元格为只读

DataGridView1.Rows(2).ReadOnly = True

' 设置 DataGridView1 的[0,0]单元格为只读

DataGridView1(0, 0).ReadOnly = True

[C#]

// 设置 DataGridView1 的第2列整列单元格为只读

DataGridView1.Columns[1].ReadOnly = true;

// 设置 DataGridView1 的第3行整行单元格为只读

DataGridView1.Rows[2].ReadOnly = true;

// 设置 DataGridView1 的[0,0]单元格为只读

DataGridView1[0, 0].ReadOnly = true;

2) 使用 EditMode 属性

DataGridView.EditMode 属性被设置为 DataGridViewEditMode.EditProgrammatically 时,用户就不能手动编辑单元格的内容了。但是可以通过程序,调用 DataGridView.BeginEdit 方法,使单元格进入编辑模式进行编辑。

[VB.NET]

DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically

[C#]

DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;

3) 根据条件设定单元格的不可编辑状态

当一个一个的通过单元格坐标设定单元格 ReadOnly 属性的方法太麻烦的时候,你可以通过 CellBeginEdit 事件来取消单元格的编辑。

[VB.NET]

'CellBeginEdit 事件处理方法

Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, _

ByVal e As DataGridViewCellCancelEventArgs) _

Handles DataGridView1.CellBeginEdit

Dim dgv As DataGridView = CType(sender, DataGridView)

' 是否可以进行编辑的条件检查

If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _

Not CBool(dgv("Column2", e.RowIndex).Value) Then

' 取消编辑

e.Cancel = True

End If

End Sub

[C#]

// CellBeginEdit 事件处理方法

private void DataGridView1_CellBeginEdit(object sender,

DataGridViewCellCancelEventArgs e)

{

DataGridView dgv = (DataGridView)sender;

//是否可以进行编辑的条件检查

if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&

!(bool)dgv["Column2", e.RowIndex].Value)

{

// 取消编辑

e.Cancel = true;

}

}

--------------------------------------------------------------------------------

★ DataGridView 不显示最下面的新行:

GO TO TOP

通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * )。如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 AllowUserToAddRows 属性设置为 False。

[VB.NET]

' 设置用户不能手动给 DataGridView1 添加新行

DataGridView1.AllowUserToAddRows = False

[C#]

// 设置用户不能手动给 DataGridView1 添加新行

DataGridView1.AllowUserToAddRows = false;

但是,可以通过程序: DataGridViewRowCollection.Add 为 DataGridView 追加新行。

补足: 如果 DataGridView 的 DataSource 绑定的是 DataView, 还可以通过设置 DataView.AllowAdd

属性为 False 来达到同样的效果。

--------------------------------------------------------------------------------

★ DataGridView 判断新增行:

GO TO TOP

DataGridView的AllowUserToAddRows属性为True时也就是允许用户追加新行的场合下,DataGridView的最后一行就是新追加的行(*行)。使用 DataGridViewRow.IsNewRow 属性可以判断哪一行是新追加的行。另外,通过DataGridView.NewRowIndex 可以获取新行的行序列号。在没有新行的时候,NewRowIndex = -1。[VB.NET]

If DataGridView1.CurrentRow.IsNewRow Then

Console.WriteLine("当前行为新追加行。")

Else

Console.WriteLine("当前行不是新追加行。")

End If

--------------------------------------------------------------------------------

★ DataGridView 行的用户删除操作的自定义:

GO TO TOP

1) 无条件的限制行删除操作。

默认时,DataGridView 是允许用户进行行的删除操作的。如果设置 DataGridView对象的AllowUserToDeleteRows属性为 False 时, 用户的行删除操作就被禁止了。

[VB.NET]

' 禁止DataGridView1的行删除操作。

DataGridView1.AllowUserToDeleteRows = False

[C#]

// 禁止DataGridView1的行删除操作。

DataGridView1.AllowUserToDeleteRows = false;

但是,通过 DataGridViewRowCollection.Remove 还是可以进行行的删除。

补足: 如果 DataGridView 绑定的是 DataView 的话,通过 DataView.AllowDelete 也可以控制行的删除。

2) 行删除时的条件判断处理。

用户在删除行的时候,将会引发 DataGridView.UserDeletingRow 事件。 在这个事件里,可以判断条件并取消删除操作。

[C#]

// DataGridView1 的 UserDeletingRow 事件

private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)

{

// 删除前的用户确认。

if (MessageBox.Show("确认要删除该行数据吗?", "删除确认",

MessageBoxButtons.OKCancel,

MessageBoxIcon.Question) != DialogResult.OK)

{

// 如果不是OK,则取消。

e.Cancel = true;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值