·¢²¼Ê±¼ä : ÐÇÆÚÎå ÎÄÕ³¬È«¾µäDataGridView+±à³Ì36¼Æ¸üÐÂÍê±Ï¿ªÊ¼ÔĶÁa4ccc203eff9aef8951e0605
.NET 2.0 - WinForm Control - DataGridView ±à³Ì36¼Æ£¨Ò»£© Ŀ¼£º
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ¢Ù ¢Ú ¢Û ¢Ü ¢Ý ¢Þ ¢ß ¢à ¢á ¢â ? ? ? ? ? ? ? ? È¡µÃ»òÕßÐ޸ĵ±Ç°µ¥Ôª¸ñµÄÄÚÈÝ É趨µ¥Ôª¸ñÖ»¶Á ²»ÏÔʾ×îÏÂÃæµÄÐÂÐÐ ÅжÏÐÂÔöÐÐ ÐеÄÓû§É¾³ý²Ù×÷µÄ×Ô¶¨Òå ÐС¢ÁеÄÒþ²ØºÍɾ³ý ½ûÖ¹ÁлòÕßÐеÄResize ÁпíºÍÐиßÒÔ¼°ÁÐÍ·µÄ¸ß¶ÈºÍÐÐÍ·µÄ¿í¶ÈµÄ×Ô¶¯µ÷Õû ¶³½áÁлòÐÐ ÁÐ˳ÐòµÄµ÷Õû ÐÐÍ·ÁÐÍ·µÄµ¥Ôª¸ñ ¼ôÇаåµÄ²Ù×÷ µ¥Ôª¸ñµÄToolTipµÄÉèÖà ÓÒ¼ü²Ëµ¥£¨ContextMenuStrip£©µÄÉèÖà µ¥Ôª¸ñµÄ±ß¿ò¡¢ Íø¸ñÏßÑùʽµÄÉ趨 µ¥Ôª¸ñ±íʾֵµÄÉ趨 Óû§ÊäÈëʱ£¬µ¥Ôª¸ñÊäÈëÖµµÄÉ趨 É趨мÓÐеÄĬÈÏÖµ
¢Ù 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 À´É趨ѡ¶¨ÐС£ ///
///
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 É趨µ¥Ôª¸ñÖ»¶Á£º
GO TO TOP
1£© ʹÓà ReadOnly ÊôÐÔ
? Èç¹ûÏ£Íû£¬DataGridView ÄÚËùÓе¥Ôª¸ñ¶¼²»¿É±à¼£¬ ÄÇôֻҪ£º [VB.NET]
' ÉèÖà DataGridView1 Ϊֻ¶Á DataGridView1.ReadOnly = True [C#]
// ÉèÖà DataGridView1 Ϊֻ¶Á DataGridView1.ReadOnly = true;
´Ëʱ£¬Óû§µÄÐÂÔöÐвÙ×÷ºÍɾ³ýÐвÙ×÷Ò²±»ÆÁ±ÎÁË¡£
? Èç¹ûÏ£Íû£¬DataGridView ÄÚij¸öµ¥Ôª¸ñ²»¿É±à¼£¬ ÄÇôֻҪ£º [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 = \AndAlso _ Not CBool(dgv(\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 == \ !(bool)dgv[\ {
// È¡Ïû±à¼
e.Cancel = true; } }