DataGridView控件用法合集Word文档格式.docx

上传人:b****6 文档编号:22239434 上传时间:2023-02-03 格式:DOCX 页数:65 大小:29.90KB
下载 相关 举报
DataGridView控件用法合集Word文档格式.docx_第1页
第1页 / 共65页
DataGridView控件用法合集Word文档格式.docx_第2页
第2页 / 共65页
DataGridView控件用法合集Word文档格式.docx_第3页
第3页 / 共65页
DataGridView控件用法合集Word文档格式.docx_第4页
第4页 / 共65页
DataGridView控件用法合集Word文档格式.docx_第5页
第5页 / 共65页
点击查看更多>>
下载资源
资源描述

DataGridView控件用法合集Word文档格式.docx

《DataGridView控件用法合集Word文档格式.docx》由会员分享,可在线阅读,更多相关《DataGridView控件用法合集Word文档格式.docx(65页珍藏版)》请在冰豆网上搜索。

DataGridView控件用法合集Word文档格式.docx

Column2"

e.RowIndex).Value)Then

e.Cancel=True

EndIf

EndSub

3.DataGridView最下面一列新追加行非表示

DataGridView1.AllowUserToAddRows=False

4.判断当前选中行是否为新追加的行

IfDataGridView1.CurrentRow.IsNewRowThen

Console.WriteLine("

現在のセルがある行は、新しい行です。

"

Else

現在のセルがある行は、新しい行ではありません。

EndIf

5.DataGridView删除行可否设定

DataGridView1.AllowUserToDeleteRows=False

根据条件判断当前行是否要删除

PrivateSubDataGridView1_UserDeletingRow(ByValsenderAsObject,_

ByValeAsDataGridViewRowCancelEventArgs)_

HandlesDataGridView1.UserDeletingRow

IfMessageBox.Show("

この列を削除しますか?

"

削除の確認"

_

MessageBoxButtons.OKCancel,MessageBoxIcon.Question)<

>

Windows.Forms.DialogResult.OKThen

6.DataGridView行列不表示和删除

行列不表示

DataGridView1.Columns(0).Visible=False

DataGridView1.Rows(0).Visible=False

行列表头部分不表示

DataGridView1.ColumnHeadersVisible=False

DataGridView1.RowHeadersVisible=False

指定行列删除

DataGridView1.Columns.Remove("

DataGridView1.Columns.RemoveAt(0)

DataGridView1.Rows.RemoveAt(0)

选择的行列删除(多行列)

DimrAsDataGridViewRow

ForEachrInDataGridView1.SelectedRows

IfNotr.IsNewRowThen

DataGridView1.Rows.Remove(r)

Nextr

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

DataGridView1.AllowUserToResizeColumns=False

DataGridView1.AllowUserToResizeRows=False

指定行列宽度高度设置为不能编辑

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

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

列幅行高最小值设定

DataGridView1.Columns(0).MinimumWidth=100

DataGridView1.Rows(0).MinimumHeight=50

行列表头部分行高列幅设置为不能编辑

DataGridView1.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.DisableResizing

DataGridView1.RowHeadersWidthSizeMode=DataGridViewRowHeadersWidthSizeMode.EnableResizing

8.DataGridView行高列幅自动调整

DataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells

DataGridView1.AutoSizeRowsMode=DataGridViewAutoSizeRowsMode.AllCells

表头部分行高列幅自动调整

DataGridView1.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize

DataGridView1.RowHeadersWidthSizeMode= 

DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders

指定列自动调整

DataGridView1.Columns(0).AutoSizeMode=DataGridViewAutoSizeColumnMode.DisplayedCells

9.DataGridView指定行列冻结

列冻结(当前列以及左侧做所有列)

DataGridView1.Columns

(1).Frozen=True

行冻结(当前行以及上部所有行)

DataGridView1.Rows

(2).Frozen=True

指定单元格冻结(单元格所在行上部分所有行,列左侧所有列)

DataGridView1(0,0).Frozen=True

10.DataGridView列顺序变更可否设定

DataGridView1.AllowUserToOrderColumns=True

但是如果列冻结的情况下,冻结的部分不能变更到非冻结的部分。

变更后列位置取得

Console.WriteLine(DataGridView1.Columns("

).DisplayIndex)

DataGridView1.Columns("

).DisplayIndex=0

11.DataGridView行复数选择

复数行选择不可

DataGridView1.MultiSelect=False

单元格选择的时候默认为选择整行

DataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect

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

Console.WriteLine("

選択されているセル"

ForEachcAsDataGridViewCellInDataGridView1.SelectedCells

{0},{1}"

c.ColumnIndex,c.RowIndex)

Nextc

選択されている行"

ForEachrAsDataGridViewRowInDataGridView1.SelectedRows

Console.WriteLine(r.Index)

選択されている列"

ForEachcAsDataGridViewColumnInDataGridView1.SelectedColumns

Console.WriteLine(c.Index)

指定行、列、单元格取得

DataGridView1(0,0).Selected=True

DataGridView1.Rows

(1).Selected=True

DataGridView1.Columns

(2).Selected=True

13.DataGridView指定单元格是否表示

IfNotDataGridView1(0,0).DisplayedAndAlso_

DataGridView1(0,0).VisibleThen

DataGridView1.CurrentCell=DataGridView1(0,0)

14.DataGridView表头部单元格取得

DataGridView1.Columns(0).HeaderCell.Value="

はじめの列"

DataGridView1.Rows(0).HeaderCell.Value="

はじめの行"

DataGridView1.TopLeftHeaderCell.Value="

左上"

15.DataGridView表头部单元格文字列设定

更改列Header表示文字列

DataGridView1.Columns(0).HeaderText="

更改行Header表示文字列

DimiAsInteger

Fori=0ToDataGridView1.Rows.Count-1

DataGridView1.Rows(i).HeaderCell.Value=i.ToString()

Nexti

DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)

最左上Header单元格文字列

/"

16.DataGridView选择的部分拷贝至剪贴板

拷贝模式设定

DataGridView1.ClipboardCopyMode=DataGridViewClipboardCopyMode.EnableWithoutHeaderText

选中部分拷贝

Clipboard.SetDataObject(DataGridView1.GetClipboardContent())

17.DataGridView粘贴

IfDataGridView1.CurrentCellIsNothingThen

Return

DiminsertRowIndexAsInteger=DataGridView1.CurrentCell.RowIndex

DimpasteTextAsString=Clipboard.GetText()

IfString.IsNullOrEmpty(pasteText)Then

pasteText=pasteText.Replace(vbCrLf,vbLf)

pasteText=pasteText.Replace(vbCr,vbLf)

pasteText.TrimEnd(NewChar(){vbLf})

DimlinesAsString()=pasteText.Split(vbLf)

DimisHeaderAsBoolean=True

ForEachlineAsStringInlines

IfisHeaderThen

isHeader=False

Else

DimvalsAsString()=line.Split(ControlChars.Tab)

Ifvals.Length-1<

DataGridView1.ColumnCountThen

ThrowNewApplicationException("

列数が違います。

DimrowAsDataGridViewRow=DataGridView1.Rows(insertRowIndex)

row.HeaderCell.Value=vals(0)

DimiAsInteger

Fori=0Torow.Cells.Count-1

row.Cells(i).Value=vals((i+1))

Nexti

insertRowIndex+=1

Nextline

18.DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息)

DataGridView1(0,0).ToolTipText="

このセルは変更できません"

DataGridView1.Columns(0).ToolTipText="

この列には数字を入力できます"

DataGridView1.Rows(0).HeaderCell.ToolTipText="

この行のセルは変更できません"

CellToolTipTextNeeded事件,在多个单元格使用相同的ToolTips的时候,可以用该事件,下例为显示当前单元格的行号和列号

PrivateSubDataGridView1_CellToolTipTextNeeded(ByValsenderAsObject,_

ByValeAsDataGridViewCellToolTipTextNeededEventArgs)_

HandlesDataGridView1.CellToolTipTextNeeded

e.ToolTipText=e.ColumnIndex.ToString()+"

+e.RowIndex.ToString()

19.DataGridView中的ContextMenuStrip属性

DataGridView1.ContextMenuStrip=Me.ContextMenuStrip1

DataGridView1.Columns(0).ContextMenuStrip=Me.ContextMenuStrip2

DataGridView1.Columns(0).HeaderCell.ContextMenuStrip=Me.ContextMenuStrip2

DataGridView1.Rows(0).ContextMenuStrip=Me.ContextMenuStrip3

DataGridView1(1,0).ContextMenuStrip=Me.ContextMenuStrip4

也可以用CellContextMenuStripNeeded、RowContextMenuStripNeeded属性进行定义

PrivateSubDataGridView1_CellContextMenuStripNeeded(_

ByValsenderAsObject,_

ByValeAsDataGridViewCellContextMenuStripNeededEventArgs)_

HandlesDataGridView1.CellContextMenuStripNeeded

Ife.RowIndex<

0Then

e.ContextMenuStrip=Me.ContextMenuStrip1

ElseIfe.ColumnIndex<

e.ContextMenuStrip=Me.ContextMenuStrip2

ElseIfTypeOf(dgv(e.ColumnIndex,e.RowIndex).Value)IsIntegerThen

e.ContextMenuStrip=Me.ContextMenuStrip3

20.指定DataGridView的滚动框位置

DataGridView1.FirstDisplayedScrollingRowIndex=0

DataGridView1.FirstDisplayedScrollingColumnIndex=0

21.DataGridView手动追加列

DataGridView1.AutoGenerateColumns=False

DataGridView1.DataSource=BindingSource1

DimtextColumnAsNewDataGridViewTextBoxColumn()

textColumn.DataPropertyName="

textColumn.Name="

textColumn.HeaderText="

DataGridView1.Columns.Add(textColumn)

22.DataGridView全体分界线样式设置

DataGridView1.BorderStyle=BorderStyle.Fixed3D

单元格上下左右分界线样式设置

DataGridView1.AdvancedCellBorderStyle.Top=DataGridViewAdvancedCellBorderStyle.InsetDouble

DataGridView1.AdvancedCellBorderStyle.Right=DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Bottom=DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Left=DataGridViewAdvancedCellBorderStyle.InsetDouble

23.根据DataGridView单元格属性更改显示内容

如下例,当该列是字符串时,自动转换文字大小写

PrivateSubDataGridView1_CellFormatting(ByValsenderAsObject,_

ByValeAsDataGridViewCellFormattingEventArgs)_

HandlesDataGridView1.CellFormatting

TypeOfe.ValueIsStringThen

DimstrAsString=e.Value.ToString()

e.Value=str.ToUpper()

e.FormattingApplied=True

24.DataGridView新追加行的行高样式设置

行高设置

DataGridView1.RowTemplate.Height=50

DataGridView1.RowTemplate.MinimumHeight=50

样式设置

DataGridView1.DefaultCellStyle.BackColor=Color.Yellow

25.DataGridView新追加行单元格默认值设置

PrivateSubDataGridView1_DefaultValuesNeeded(ByValsenderAsObject,_

ByValeAsDataGridViewRowEventArgs)_

HandlesDataGridView1.DefaultValuesNeeded

e.Row.Cells("

).Value=0

).Value="

-"

26.DataGridView单元格数据错误标签表示

DataGridView1(0,0).ErrorText="

セルの値を確認してください。

DataGridView1.Rows(3).ErrorText="

負の値は入力できません。

在大量

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 中考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1