VB.NET datagridview的操作

'空值判斷

If IsDBNull(DataGridView1.Item(1, 1).Value) = True Then

DataGridView1.Item(1, 1).Value = "空值"

End If

'删除所有行

DataGridView1.Rows.Clear()

'删除第一行

DataGridView1.Rows.RemoveAt(0)

'删除選定的行

For Each r As DataGridViewRow In DataGridView1.SelectedRows

If Not r.IsNewRow Then

DataGridView1.Rows.Remove(r)

End If

Next

 

'删除所有列

Me.DataGridView1.Columns.Clear()

'新增一列

Me.DataGridView1.Columns.Add("1", "列1")

 

'設定列寛

Me.DataGridView1.Columns(0).Width = 100

 

'關閉重新排序

DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable

 

 '获取当前被选中的行号

Me.DataGridView1.CurrentCell.RowIndex 

'获取当前被选中的列号

Me. DataGridView1.CurrentCell.ColumnIndex

'改變行顏色

DataGridView1.Rows(1 ).DefaultCellStyle.BackColor = Color.Yellow

'改變單元格顏色

Me.DataGridView1.Rows(1).Cells(1).Style.BackColor = Color.Lime

'改變字顏色

Me.DataGridView1.Rows(1).Cells(1).Style.ForeColor = Color.Red

'新增一行

DataGridView1.Rows.Add()

'附與值

DataGridView1.Item(1, 1).Value = 1

  

'表單選擇

TextBox1.Text = DataGridView1.CurrentRow.Cells("Column1").Value.ToString()

  

'表單筆數

TextBox1.Text = Me.DataGridView1.RowCount

'長度調整

DataGridView1.Height = Me.Height – 150

'寬度調整

DataGridView1.Width = Me.Width – 80

'列只讀

DataGridView1.Columns(0).ReadOnly = True

 

'行只讀

DataGridView1. Rows(0).ReadOnly = True

 

'單元格只讀

DataGridView1(0, i - 1).ReadOnly = True

'DataGridViewEnter

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

If msg.WParam.ToInt32() = CInt(Keys.Enter) Then

SendKeys.Send("{Tab}")

Return True

Return MyBase.ProcessCmdKey(msg, keyData)

End Function

'設置單元格位置

DataGridView1.CurrentCell = DataGridView1(0, 0)

猜你喜欢

转载自www.cnblogs.com/leme-chen/p/9267846.html
今日推荐