PDA

View Full Version : در datagridview با زدن enter به سلول بعد بره نه خط بعد



$M03N$
دوشنبه 22 شهریور 1389, 21:22 عصر
با عرض سلام، میدونم این سوالم تکراریه، ولی من نتونستم از مطالب سایت چیزی دستگیرم بشه، اونم اینکه:
چطوری میتونم با زدن کلید Enter به جای خط بعدی به سلول بعدی برم و وقتی به سلول آخر رسید، به اولین سلول سطر بعدی برم ؟؟؟
ممنون

amiramt
سه شنبه 23 شهریور 1389, 07:54 صبح
این کار با کد زیر امکان پذیر است ولی هنوز کاملا درست نمی باشد ( وقتی درست کار می دهد که در سلول اطلاعات وارد نشود) . اگر توانستم تا شب کد کامل را قرار می دهم




int Selected_Row= -1 , Selected_Column = -1;
private void dataGridView1_KeyDown( object sender , KeyEventArgs e )
{
if ( e.KeyCode == Keys.Enter )
if ( Selected_Column < dataGridView1.ColumnCount - 1 )
dataGridView1.CurrentCell = dataGridView1[ Selected_Column + 1 , Selected_Row ];

else
{
if ( dataGridView1.RowCount-1 > Selected_Row )
dataGridView1.CurrentCell = dataGridView1[ 0 , Selected_Row ];
}
}




private void dataGridView1_CellEnter( object sender , DataGridViewCellEventArgs e )
{
Selected_Column = e.ColumnIndex;
Selected_Row = e.RowIndex;
}

$M03N$
سه شنبه 23 شهریور 1389, 17:02 عصر
این کار با کد زیر امکان پذیر است ولی هنوز کاملا درست نمی باشد ( وقتی درست کار می دهد که در سلول اطلاعات وارد نشود) . اگر توانستم تا شب کد کامل را قرار می دهم

[/code]

این کد را از MSDN پیدا کردم، ولی نتونستم به کار ببرمش




public class CustomDataGridView : DataGridView
{
[System.Security.Permissions.UIPermission(
System.Security.Permissions.SecurityAction.LinkDem and,
Window = System.Security.Permissions.UIPermissionWindow.All Windows)]
protected override bool ProcessDialogKey(Keys keyData)
{
// Extract the key code from the key value.
Keys key = (keyData & Keys.KeyCode);

// Handle the ENTER key as if it were a RIGHT ARROW key.
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDem and, Flags =
System.Security.Permissions.SecurityPermissionFlag .UnmanagedCode)]
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
// Handle the ENTER key as if it were a RIGHT ARROW key.
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
}