DataGridView控件用法合集_第1頁
DataGridView控件用法合集_第2頁
DataGridView控件用法合集_第3頁
DataGridView控件用法合集_第4頁
DataGridView控件用法合集_第5頁
已閱讀5頁,還剩196頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、DataGridView控件用法合集1. DataGridView當前的單元格屬性取得、變更2. DataGridView編輯屬性3. DataGridView最下面一列新追加行非表示4. DataGridView判斷當前選中行是否為新追加的行5. DataGridView刪除行可否設定6. DataGridView行列不表示和刪除7. DataGridView行列寬度高度設置為不能編輯8. DataGridView行高列幅自動調(diào)整9. DataGridView指定行列凍結(jié)10. DataGridView列順序變更可否設定11. DataGridView行復數(shù)選擇12. DataGridVie

2、w選擇的行、列、單元格取得13. DataGridView指定單元格是否表示14. DataGridView表頭部單元格取得15. DataGridView表頭部單元格文字列設定16. DataGridView選擇的局部拷貝至剪貼板17. DataGridView粘貼18. DataGridView單元格上ToolTip表示設定(鼠標移動到相應單元格上時,彈出說明信息)19. DataGridView中的ContextMenuStrip屬性20. DataGridView指定滾動框位置21. DataGridView手動追加列22. DataGridView全體分界線樣式設置23. DataG

3、ridView根據(jù)單元格屬性更改顯示內(nèi)容24. DataGridView新追加行的行高樣式設置25. DataGridView新追加行單元格默認值設置26. DataGridView單元格數(shù)據(jù)錯誤標簽表示27. DataGridView單元格內(nèi)輸入值正確性判斷28. DataGridView單元格輸入錯誤值事件的捕獲29. DataGridView行排序點擊列表頭自動排序的設置30. DataGridView自動行排序新追加值也會自動排序31. DataGridView自動行排序禁止情況下的排序32. DataGridView指定列指定排序33. DataGridView單元格樣式設置34.

4、DataGridView文字表示位置的設定35. DataGridView單元格內(nèi)文字列換行36. DataGridView單元格DBNull值表示的設定37. DataGridView單元格樣式格式化38. DataGridView指定單元格顏色設定39. DataGridView單元格文字字體設置40. DataGridView根據(jù)單元格值設定單元格樣式41. DataGridView設置單元格背景顏色42. DataGridView行樣式描畫43. DataGridView顯示行號44. DataGridView焦點所在單元格焦點框不顯示的設定45. DataGridView列中顯示選擇

5、框CheckBox46. DataGridView中顯示下拉框ComboBox47. DataGridView單擊翻開下拉框48. DataGridView中顯示按鈕49. DataGridView中顯示鏈接50. DataGridView中顯示圖像51. DataGridView編輯中單元格控件取得52. DataGridView輸入自動完成53. DataGridView單元格編輯時鍵盤KEY事件取得54. DataGridView下拉框ComboBox單元格編輯時事件取得55. DataGridView下拉框ComboBox單元格允許文字輸入設定56. DataGridView根據(jù)值不同

6、在另一列中顯示相應圖片57. DataGridView中顯示進度條ProgressBar58. DataGridView中添加MaskedTextBox59. DataGridView中Enter鍵按下焦點移至旁邊的單元格60. DataGridView行集合化Group1.當前的單元格屬性取得、變更VB.NET現(xiàn)在値表示Console.WriteLine(DataGridView1.CurrentCell.Value)現(xiàn)在列表示Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex)現(xiàn)在行表示Console.WriteLine(Data

7、GridView1.CurrentCell.RowIndex)(0, 0)現(xiàn)在DataGridView1.CurrentCell = DataGridView1(0, 0)C#/現(xiàn)在値表示Console.WriteLine(DataGridView1.CurrentCell.Value);/現(xiàn)在列表示Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);/現(xiàn)在行表示Console.WriteLine(DataGridView1.CurrentCell.RowIndex); /(0, 0)現(xiàn)在DataGridView1.CurrentC

8、ell = DataGridView10, 0;全部單元格編輯屬性VB.NETDataGridView1読取専用DataGridView1.ReadOnly = TrueC#/DataGridView1読取専用DataGridView1.ReadOnly = true;指定行列單元格編輯屬性VB.NETDataGridView12番目列読取専用DataGridView1.Columns(1).ReadOnly = TrueDataGridView13番目行読取専用DataGridView1.Rows(2).ReadOnly = TrueDataGridView1(0, 0)読取専用DataGr

9、idView1(0, 0).ReadOnly = TrueC#/DataGridView12番目列読取専用DataGridView1.Columns1.ReadOnly = true;/DataGridView13番目行読取専用DataGridView1.Rows2.ReadOnly = true;/DataGridView1(0, 0)読取専用DataGridView10, 0.ReadOnly = true;根據(jù)條件判斷單元格的編輯屬性下例中column2的值是True的時候,Column1設為可編輯 VB.NETCellBeginEditPrivate Sub DataGridView1

10、_CellBeginEdit(ByVal sender As Object, _ ByVal e As DataGridViewCellCancelEventArgs) _ Handles 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 IfEnd SubC#/CellBegin

11、Editprivate void DataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) DataGridView dgv = (DataGridView)sender; /編集判斷 if (dgv.Columnse.ColumnIndex.Name = Column1 & !(bool)dgvColumn2, e.RowIndex.Value) /編集 e.Cancel = true; 3.DataGridView最下面一列新追加行非表示VB.NETDataGridView1新行追加DataG

12、ridView1.AllowUserToAddRows = FalseC#/DataGridView1新行追加DataGridView1.AllowUserToAddRows = false;4.判斷當前選中行是否為新追加的行VB.NETIf DataGridView1.CurrentRow.IsNewRow Then Console.WriteLine(現(xiàn)在行、新行。)Else Console.WriteLine(現(xiàn)在行、新行。)End IfC#if (DataGridView1.CurrentRow.IsNewRow) Console.WriteLine(現(xiàn)在行、新行。);else Con

13、sole.WriteLine(現(xiàn)在行、新行。);5. DataGridView刪除行可否設定VB.NETDataGridView1行削除DataGridView1.AllowUserToDeleteRows = FalseC#/DataGridView1行削除DataGridView1.AllowUserToDeleteRows = false;根據(jù)條件判斷當前行是否要刪除VB.NETDataGridView1UserDeletingRowPrivate Sub DataGridView1_UserDeletingRow(ByVal sender As Object, _ ByVal e As

14、 DataGridViewRowCancelEventArgs) _ Handles 削除確認 If MessageBox.Show(列削除?, 削除確認, _ MessageBoxButtons.OKCancel, MessageBoxIcon.Question) _ Windows.Forms.DialogResult.OK Then e.Cancel = True End IfEnd SubC#/DataGridView1UserDeletingRowprivate void DataGridView1_UserDeletingRow( object sender, DataGridVi

15、ewRowCancelEventArgs e) /削除確認 if (MessageBox.Show(列削除?, 削除確認, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) e.Cancel = true; 6. DataGridView行列不表示和刪除行列不表示VB.NETDataGridView1列非表示DataGridView1.Columns(0).Visible = FalseDataGridView1行非表示DataGridView1.Rows(0).Visible = FalseC#/

16、DataGridView1列非表示DataGridView1.Columns0.Visible = false;/DataGridView1行非表示DataGridView1.Rows0.Visible = false;行列表頭局部不表示VB.NET列非表示DataGridView1.ColumnHeadersVisible = False行非表示DataGridView1.RowHeadersVisible = FalseC#/列非表示DataGridView1.ColumnHeadersVisible = false;/行非表示DataGridView1.RowHeadersVisible

17、 = false;指定行列刪除VB.NETColumn1削除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);選擇的行列刪除多行列VB.NETDataGridView1選択行削除Dim r A

18、s DataGridViewRowFor Each r In If Not r.IsNewRow Then DataGridView1.Rows.Remove(r) End IfNext rC#/DataGridView1選択行削除foreach (DataGridViewRow r in DataGridView1.SelectedRows) if (!r.IsNewRow) DataGridView1.Rows.Remove(r); 7. DataGridView行列寬度高度設置為不能編輯VB.NETDataGridView1列幅変更DataGridView1.AllowUserToRes

19、izeColumns = FalseDataGridView1行高変更DataGridView1.AllowUserToResizeRows = FalseC#/DataGridView1列幅変更DataGridView1.AllowUserToResizeColumns = false;/DataGridView1行高変更DataGridView1.AllowUserToResizeRows = false;指定行列寬度高度設置為不能編輯VB.NETDataGridView1列幅変更DataGridView1行高変更C#/DataGridView1列幅変更DataGridView1.Colu

20、mns0.Resizable = DataGridViewTriState.False;/DataGridView1行高変更DataGridView1.Rows0.Resizable = DataGridViewTriState.False;列幅行高最小值設定VB.NET一番列幅最小100DataGridView1.Columns(0).MinimumWidth = 100一番行高最小50DataGridView1.Rows(0).MinimumHeight = 50C#/一番列幅最小100DataGridView1.Columns0.MinimumWidth = 100;/一番行高最小50D

21、ataGridView1.Rows0.MinimumHeight = 50;行列表頭局部行高列幅設置為不能編輯VB.NET列高変更DataGridView1.ColumnHeadersHeightSizeMode = _行幅変更DataGridView1.RowHeadersWidthSizeMode = _C#/列高変更DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;/行幅変更DataGridView1.RowHeadersWidthSize

22、Mode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;8. DataGridView行高列幅自動調(diào)整VB.NET內(nèi)容合、列幅自動調(diào)整內(nèi)容合、行高自動調(diào)整C#/內(nèi)容合、列幅自動調(diào)整DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;/內(nèi)容合、行高自動調(diào)整DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;表頭局部行高列幅自動調(diào)整VB.NET列高自

23、動調(diào)整DataGridView1.ColumnHeadersHeightSizeMode = _行幅自動調(diào)整DataGridView1.RowHeadersWidthSizeMode = _C#/列高自動調(diào)整DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;/行幅自動調(diào)整DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders

24、;指定列自動調(diào)整VB.NET列幅自動調(diào)整DataGridView1.Columns(0).AutoSizeMode = _C#/列幅自動調(diào)整DataGridView1.Columns0.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;9. DataGridView指定行列凍結(jié)列凍結(jié)當前列以及左側(cè)做所有列VB.NETDataGridView1左側(cè)2列固定DataGridView1.Columns(1).Frozen = TrueC#/DataGridView1左側(cè)2列固定DataGridView1.Columns1.Fro

25、zen = true;行凍結(jié)當前行以及上部所有行VB.NETDataGridView1上部2行固定DataGridView1.Rows(2).Frozen = TrueC#/DataGridView1上部2行固定DataGridView1.Rows2.Frozen = true;指定單元格凍結(jié)單元格所在行上局部所有行,列左側(cè)所有列VB.NETDataGridView1(0, 0). Frozen = TrueC#DataGridView10, 0. Frozen = true;10. DataGridView列順序變更可否設定VB.NETDataGridView1列位置変更DataGridVi

26、ew1.AllowUserToOrderColumns = TrueC#/DataGridView1列位置変更DataGridView1.AllowUserToOrderColumns = true;但是如果列凍結(jié)的情況下,凍結(jié)的局部不能變更到非凍結(jié)的局部。變更后列位置取得VB.NET列Column1現(xiàn)在位置取得Console.WriteLine(DataGridView1.Columns(Column1).DisplayIndex)列Column1先頭移動DataGridView1.Columns(Column1).DisplayIndex = 0C#/列Column1現(xiàn)在位置取得Conso

27、le.WriteLine(DataGridView1.ColumnsColumn1.DisplayIndex);/列Column1先頭移動DataGridView1.ColumnsColumn1.DisplayIndex = 0;11. DataGridView行復數(shù)選擇復數(shù)行選擇不可VB.NETDataGridView1、行、列複數(shù)選択DataGridView1.MultiSelect = FalseC#/DataGridView1、行、列複數(shù)選択DataGridView1.MultiSelect = false;單元格選擇的時候默認為選擇整行VB.NET選択行全體選択C#/選択行全體選択D

28、ataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;12. DataGridView選擇的行、列、單元格取得VB.NET選択表示Console.WriteLine(選択)For Each c As DataGridViewCell In Console.WriteLine(0, 1, c.ColumnIndex, c.RowIndex)Next c選択行表示Console.WriteLine(選択行)For Each r As DataGridViewRow In Console.WriteLine(r.In

29、dex)Next r選択列表示Console.WriteLine(選択列)For Each c As DataGridViewColumn In Console.WriteLine(c.Index)Next cC#/選択表示Console.WriteLine(選択);foreach (DataGridViewCell c in DataGridView1.SelectedCells) Console.WriteLine(0, 1, c.ColumnIndex, c.RowIndex);/選択行表示Console.WriteLine(選択行);foreach (DataGridViewRow r

30、 in DataGridView1.SelectedRows) Console.WriteLine(r.Index);/選択列表示Console.WriteLine(選択列);foreach (DataGridViewColumn c in DataGridView1.SelectedColumns) Console.WriteLine(c.Index);指定行、列、單元格取得VB.NET(0, 0)選択DataGridView1(0, 0).Selected = True1行選択DataGridView1.Rows(1).Selected = True2列選択DataGridView1.Co

31、lumns(2).Selected = TrueC#/(0, 0)選択DataGridView10, 0.Selected = true;/1行選択DataGridView1.Rows1.Selected = true;/2列選択DataGridView1.Columns2.Selected = true;13. DataGridView指定單元格是否表示VB.NETIf Not DataGridView1(0, 0).Displayed AndAlso _ DataGridView1(0, 0).Visible Then DataGridView1.CurrentCell = DataGri

32、dView1(0, 0)End IfC#if (!DataGridView10, 0.Displayed & DataGridView10, 0.Visible) DataGridView1.CurrentCell = DataGridView10, 0;14. DataGridView表頭部單元格取得VB.NETDataGridView1列変更DataGridView1.Columns(0).HeaderCell.Value = 列DataGridView1行変更DataGridView1.Rows(0).HeaderCell.Value = 行DataGridView1左上隅変更DataG

33、ridView1.TopLeftHeaderCell.Value = 左上C#/DataGridView1列変更DataGridView1.Columns0.HeaderCell.Value = 列;/DataGridView1行変更DataGridView1.Rows0.HeaderCell.Value = 行;/DataGridView1左上隅変更DataGridView1.TopLeftHeaderCell.Value = 左上;15. DataGridView表頭部單元格文字列設定更改列Header表示文字列VB.NETDataGridView1列変更DataGridView1.Col

34、umns(0).HeaderText = 列C#/DataGridView1列変更DataGridView1.Columns0.HeaderText = 列;更改行Header表示文字列VB.NETDataGridView1行行番號表示Dim i As IntegerFor i = 0 To DataGridView1.Rows.Count - 1 DataGridView1.Rows(i).HeaderCell.Value = i.ToString()Next i行幅自動調(diào)節(jié)DataGridView1.AutoResizeRowHeadersWidth( _ DataGridViewRowH

35、eadersWidthSizeMode.AutoSizeToAllHeaders)C#/DataGridView1行行番號表示for (int i = 0; i DataGridView1.Rows.Count; i+) DataGridView1.Rowsi.HeaderCell.Value = i.ToString();/行幅自動調(diào)節(jié)DataGridView1.AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);最左上Header單元格文字列VB.NET左上隅/表示Data

36、GridView1.TopLeftHeaderCell.Value = /C#/左上隅/表示DataGridView1.TopLeftHeaderCell.Value = /;16. DataGridView選擇的局部拷貝至剪貼板拷貝模式設定VB.NETDataGridView1.ClipboardCopyMode = _C#/DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;選中局部拷貝VB.NET選択Clipboard.SetDataObject(DataGridV

37、iew1.GetClipboardContent()C#/選択Clipboard.SetDataObject(DataGridView1.GetClipboardContent();17.DataGridView粘貼VB.NET現(xiàn)在行下If DataGridView1.CurrentCell Is Nothing Then ReturnEnd IfDim insertRowIndex As Integer內(nèi)容取得、行分Dim pasteText As String = Clipboard.GetText()If String.IsNullOrEmpty(pasteText) Then Retu

38、rnEnd IfpasteText = pasteText.Replace(vbCrLf, vbLf)pasteText = pasteText.Replace(vbCr, vbLf)pasteText.TrimEnd(New Char() vbLf)Dim lines As String() = pasteText.Split(vbLf)Dim isHeader As Boolean = TrueFor Each line As String In lines 列飛 If isHeader Then isHeader = False Else 分割 Dim vals As String()

39、= line.Split(ControlChars.Tab) 列數(shù)合調(diào) If vals.Length - 1 DataGridView1.ColumnCount Then Throw New ApplicationException(列數(shù)違。) End If Dim row As DataGridViewRow = DataGridView1.Rows(insertRowIndex) 設定 row.HeaderCell.Value = vals(0) 各値設定 Dim i As Integer For i = 0 To row.Cells.Count - 1 row.Cells(i).Valu

40、e = vals(i + 1) Next i 次行 insertRowIndex += 1 End IfNext lineC#/現(xiàn)在行下if (DataGridView1.CurrentCell = null) return;int insertRowIndex = DataGridView1.CurrentCell.RowIndex;/內(nèi)容取得、行分string pasteText = Clipboard.GetText();if (string.IsNullOrEmpty(pasteText) return;pasteText = pasteText.Replace(rn, n);past

41、eText = pasteText.Replace(r, n);pasteText.TrimEnd(new char n );string lines = pasteText.Split(n);bool isHeader = true;foreach (string line in lines) /列飛 if (isHeader) isHeader = false; continue; /分割 string vals = line.Split(t); /列數(shù)合調(diào) if (vals.Length - 1 != DataGridView1.ColumnCount) throw new Applic

42、ationException(列數(shù)違。); DataGridViewRow row = DataGridView1.RowsinsertRowIndex; /設定 row.HeaderCell.Value = vals0; /各値設定 for (int i = 0; i row.Cells.Count; i+) row.Cellsi.Value = valsi + 1; /次行 insertRowIndex+;18. DataGridView單元格上ToolTip表示設定(鼠標移動到相應單元格上時,彈出說明信息)VB.NET表示ToolTip設定DataGridView1(0, 0).Tool

43、TipText = 変更列表示ToolTip設定DataGridView1.Columns(0).ToolTipText = 列數(shù)字入力行表示ToolTip設定DataGridView1.Rows(0).HeaderCell.ToolTipText = 行変更C#/表示ToolTip設定DataGridView10, 0.ToolTipText = 変更;/列表示ToolTip設定DataGridView1.Columns0.ToolTipText = 列數(shù)字入力;/行表示ToolTip設定DataGridView1.Rows0.HeaderCell.ToolTipText = 行変更;Cel

44、lToolTipTextNeeded事件,在多個單元格使用相同的ToolTips的時候,可以用該事件,下例為顯示當前單元格的行號和列號VB.NETCellToolTipTextNeededPrivate Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _ Handles e.ToolTipText = e.ColumnIndex.ToString() + , + e.RowIndex.ToString(

45、)End SubC#/CellToolTipTextNeededprivate void DataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) e.ToolTipText = e.ColumnIndex.ToString() + , + e.RowIndex.ToString();19. DataGridView中的ContextMenuStrip屬性VB.NETDataGridViewContextMenuStrip設定DataGridView1.Con

46、textMenuStrip = Me.ContextMenuStrip1列ContextMenuStrip設定DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2列ContextMenuStrip設定DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2行ContextMenuStrip設定DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3Context

47、MenuStrip設定DataGridView1(1, 0).ContextMenuStrip = Me.ContextMenuStrip4C#/DataGridViewContextMenuStrip設定DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;/列ContextMenuStrip設定DataGridView1.Columns0.ContextMenuStrip = this.ContextMenuStrip2;/列ContextMenuStrip設定DataGridView1.Columns0.HeaderCell.Co

48、ntextMenuStrip = this.ContextMenuStrip2;/行ContextMenuStrip設定DataGridView1.Rows0.ContextMenuStrip = this.ContextMenuStrip3;/ContextMenuStrip設定DataGridView10, 1.ContextMenuStrip = this.ContextMenuStrip4;也可以用CellContextMenuStripNeeded、RowContextMenuStripNeeded屬性進行定義VB.NETCellContextMenuStripNeededPriva

49、te Sub DataGridView1_CellContextMenuStripNeeded( _ ByVal sender As Object, _ ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _ Handles Dim dgv As DataGridView = CType(sender, DataGridView) If e.RowIndex 0 Then 列表示ContextMenuStrip設定 e.ContextMenuStrip = Me.ContextMenuStrip1 ElseIf e.Colum

50、nIndex 0 Then 行表示ContextMenuStrip設定 e.ContextMenuStrip = Me.ContextMenuStrip2 ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then 整數(shù)型表示ContextMenuStrip変更 e.ContextMenuStrip = Me.ContextMenuStrip3 End IfEnd SubC#/CellContextMenuStripNeededprivate void DataGridView1_CellContextMenuStr

51、ipNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) DataGridView dgv = (DataGridView)sender; if (e.RowIndex 0) /列表示ContextMenuStrip設定 e.ContextMenuStrip = this.ContextMenuStrip1; else if (e.ColumnIndex 0) /行表示ContextMenuStrip設定 e.ContextMenuStrip = this.ContextMenuStrip2; else

52、 if (dgve.ColumnIndex, e.RowIndex.Value is int) /整數(shù)型表示ContextMenuStrip変更 e.ContextMenuStrip = this.ContextMenuStrip3; 20.指定DataGridView的滾動框位置VB.NET先頭行DataGridView1.FirstDisplayedScrollingRowIndex = 0先頭列DataGridView1.FirstDisplayedScrollingColumnIndex = 0C#/先頭行DataGridView1.FirstDisplayedScrollingRow

53、Index = 0;/先頭列DataGridView1.FirstDisplayedScrollingColumnIndex = 0;21. DataGridView手動追加列VB.NET列自動的作成DataGridView1.AutoGenerateColumns = False設定DataGridView1.DataSource = BindingSource1DataGridViewTextBoxColumn列作成Dim textColumn As New DataGridViewTextBoxColumn()Column1textColumn.DataPropertyName = Co

54、lumn1名前設定textColumn.Name = Column1textColumn.HeaderText = Column1列追加DataGridView1.Columns.Add(textColumn)C#/列自動的作成DataGridView1.AutoGenerateColumns = false;/設定DataGridView1.DataSource = BindingSource1;/DataGridViewTextBoxColumn列作成DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn()

55、;/Column1textColumn.DataPropertyName = Column1;/名前設定textColumn.Name = Column1;textColumn.HeaderText = Column1;/列追加DataGridView1.Columns.Add(textColumn);22. DataGridView全體分界線樣式設置VB.NETDataGridView境界線3DC#/DataGridView境界線3DDataGridView1.BorderStyle = BorderStyle.Fixed3D;單元格上下左右分界線樣式設置VB.NET上左二重線境界線、下右一

56、重線境界線DataGridView1.AdvancedCellBorderStyle.Top = dvancedCellBorderStyle.Right = vancedCellBorderStyle.Bottom = DataGridView1.AdvancedCellBorderStyle.Left = C#/上左二重線境界線、/下右一重線境界線DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble;DataGridView1.AdvancedCellBorde

57、rStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset;DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble;23.根據(jù)DataGridView單元格屬性更改顯示內(nèi)容如下例,當該列是字符串時,自動轉(zhuǎn)換文字大小寫VB.NETCellF

58、ormattingPrivate Sub DataGridView1_CellFormatting(ByVal sender As Object, _ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles Dim dgv As DataGridView = CType(sender, DataGridView) 列確認 If dgv.Columns(e.ColumnIndex).Name = Column1 AndAlso _ TypeOf e.Value Is String Then 大文字表示 Dim str As String

59、 = e.Value.ToString() e.Value = str.ToUpper() 必要知 e.FormattingApplied = True End IfEnd SubC#/CellFormattingprivate void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) DataGridView dgv = (DataGridView)sender; /列確認 if (dgv.Columnse.ColumnIndex.Name = Column1 & e.Val

60、ue is string) /大文字表示 string str = e.Value.ToString(); e.Value = str.ToUpper(); /必要知 e.FormattingApplied = true; 24. DataGridView新追加行的行高樣式設置行高設置VB.NET行高設定DataGridView1.RowTemplate.Height = 50行最低高設定DataGridView1.RowTemplate.MinimumHeight = 50C#/行高設定DataGridView1.RowTemplate.Height = 50;/行最低高設定DataGrid

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論