DataGridView 编程36计

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

 
当前单元格指的是 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 来设定选定行。


        /** 
        /// 向下遍历
        /// 
        /// 
        /// 
        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];  
        }

        /** 
        /// 向上遍历
        /// 
        /// 
        /// 
        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  设定单元格只读:

 
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  不显示最下面的新行:

 
通常 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  判断新增行:

 
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  行的用户删除操作的自定义:

 
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 事件。 在这个事件里,可以判断条件并取消删除操作。


[VB.NET]
'' DataGridView1 的 UserDeletingRow 事件
Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, _
        ByVal e As DataGridViewRowCancelEventArgs) _
        Handles DataGridView1.UserDeletingRow
    ''  删除前的用户确认。
    If MessageBox.Show("确认要删除该行数据吗?", "删除确认", _
        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) <> _
            Windows.Forms.DialogResult.OK Then
        ''  如果不是 OK,则取消。
        e.Cancel = True
    End If
End Sub 


[C#]
// DataGridView1 的 UserDeletingRow 事件
private void DataGridView1_UserDeletingRow(
    object sender, DataGridViewRowCancelEventArgs e)
{
    // 删除前的用户确认。
    if (MessageBox.Show("确认要删除该行数据吗?", "删除确认",
        MessageBoxButtons.OKCancel,
        MessageBoxIcon.Question) != DialogResult.OK)
    {
        // 如果不是 OK,则取消。
        e.Cancel = true;
    }
} 

--------------------------------------------------------------------------------
⑥ DataGridView  行、列的隐藏和删除:

 
1) 行、列的隐藏


[VB.NET]
'' DataGridView1的第一列隐藏
DataGridView1.Columns(0).Visible = False
'' DataGridView1的第一行隐藏
DataGridView1.Rows(0).Visible = False 


[C#]
// DataGridView1的第一列隐藏
DataGridView1.Columns[0].Visible = false;
// DataGridView1的第一行隐藏
DataGridView1.Rows[0].Visible = false; 
2) 行头、列头的隐藏


[VB.NET]
'' 列头隐藏
DataGridView1.ColumnHeadersVisible = False
'' 行头隐藏
DataGridView1.RowHeadersVisible = False 


[C#]
// 列头隐藏
DataGridView1.ColumnHeadersVisible = false;
// 行头隐藏
DataGridView1.RowHeadersVisible = false; 
3) 行和列的删除 


[VB.NET] 
'' 删除名为"Column1"的列
DataGridView1.Columns.Remove("Column1") 
'' 删除第一列 
DataGridView1.Columns.RemoveAt(0) 
'' 删除第一行 
DataGridView1.Rows.RemoveAt(0) 


[C#] 
'' 删除名为"Column1"的列
DataGridView1.Columns.Remove("Column1");
'' 删除第一列 
DataGridView1.Columns.RemoveAt(0);
'' 删除第一行 
DataGridView1.Rows.RemoveAt(0); 
4) 删除选中行


[VB.NET]
For Each r As DataGridViewRow In DataGridView1.SelectedRows
    If Not r.IsNewRow Then
        DataGridView1.Rows.Remove(r)
    End If
Next 


[C#] 
foreach (DataGridViewRow r in DataGridView1.SelectedRows)
{
    if (!r.IsNewRow)
    {
        DataGridView1.Rows.Remove(r);
    }
} 

--------------------------------------------------------------------------------
⑦ DataGridView 禁止列或者行的Resize:

 
1) 禁止所有的列或者行的Resize


[VB.NET]
'' 禁止用户改变DataGridView1的所有列的列宽
DataGridView1.AllowUserToResizeColumns = False

''禁止用户改变DataGridView1の所有行的行高
DataGridView1.AllowUserToResizeRows = False 


[C#]
// 禁止用户改变DataGridView1的所有列的列宽
DataGridView1.AllowUserToResizeColumns = false;

//禁止用户改变DataGridView1の所有行的行高
DataGridView1.AllowUserToResizeRows = false; 
但是可以通过 DataGridViewColumn.Width 或者 DataGridViewRow.Height 属性设定列宽和行高。

2) 禁止指定行或者列的Resize


[VB.NET]
''  禁止用户改变DataGridView1的第一列的列宽
DataGridView1.Columns(0).Resizable = DataGridViewTriState.False

''  禁止用户改变DataGridView1的第一列的行宽
DataGridView1.Rows(0).Resizable = DataGridViewTriState.False 


[C#]
// 禁止用户改变DataGridView1的第一列的列宽
DataGridView1.Columns[0].Resizable = DataGridViewTriState.False;

// 禁止用户改变DataGridView1的第一列的行宽
DataGridView1.Rows[0].Resizable = DataGridViewTriState.False; 
⇒ 关于 NoSet

当 Resizable 属性设为 DataGridViewTriState.NotSet 时, 实际上会默认以 DataGridView 的 AllowUserToResizeColumns 和  AllowUserToResizeRows 的属性值进行设定。比如: DataGridView.AllowUserToResizeColumns = False 且 Resizable 是 NoSet 设定时,Resizable = False 。


判断 Resizable 是否是继承设定了 DataGridView 的 AllowUserToResizeColumns 和  AllowUserToResizeRows 的属性值, 可以根据 State 属性判断。如果 State 属性含有 ResizableSet,那么说明没有继承设定。


3) 列宽和行高的最小值的设定


[VB.NET]
'' 第一列的最小列宽设定为 100
DataGridView1.Columns(0).MinimumWidth = 100

'' 第一行的最小行高设定为 50
DataGridView1.Rows(0).MinimumHeight = 50 


[C#]
// 第一列的最小列宽设定为 100 
DataGridView1.Columns[0].MinimumWidth = 100;

// 第一行的最小行高设定为 50
DataGridView1.Rows[0].MinimumHeight = 50; 
4) 禁止用户改变行头的宽度以及列头的高度


[VB.NET]
''  禁止用户改变列头的高度
DataGridView1.ColumnHeadersHeightSizeMode = _
    DataGridViewColumnHeadersHeightSizeMode.DisableResizing

'' 禁止用户改变行头的宽度
DataGridView1.RowHeadersWidthSizeMode = _
    DataGridViewRowHeadersWidthSizeMode.EnableResizing 


[C#]
// 禁止用户改变列头的高度
DataGridView1.ColumnHeadersHeightSizeMode =
    DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

// 禁止用户改变行头的宽度
DataGridView1.RowHeadersWidthSizeMode =
    DataGridViewRowHeadersWidthSizeMode.EnableResizing; 


--------------------------------------------------------------------------------
⑧ DataGridView 列宽和行高自动调整的设定: 
  
1) 设定行高和列宽自动调整


[VB.NET]
'' 设定包括Header和所有单元格的列宽自动调整
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

'' 设定包括Header和所有单元格的行高自动调整
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells 


[C#]
// 设定包括Header和所有单元格的列宽自动调整
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

// 设定包括Header和所有单元格的行高自动调整
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 
AutoSizeColumnsMode 属性的设定值枚举请参照 msdn 的 DataGridViewAutoSizeRowsMode 说明。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值