Dear Professionals,
I've implemented a
Custom Control inheriting from
DataGridView for the purpose of having certain common properties and methods for all DataGridViews used throughout my project.
In this control class, I have a method which sets the DataGridView's currentcell when Enter key is pressed. At this stage I want to check if the column to which I have to move next is frozen or not to decide my next move.
I have programmatically created the datatable and set it as the datasource to the DataGridView and have set some columns to freeze.
DataTable dtkotitems = new DataTable();
dtkotitems.Columns.Add("Sl.No", typeof(int));
dtkotitems.Columns.Add("Code", typeof(String));
dtkotitems.Columns.Add("Item Description", typeof(String));
dtkotitems.Columns.Add("Dispersal", typeof(String));
dtkotitems.Columns.Add("Rate", typeof(Double));
dtkotitems.Columns.Add("Qty.", typeof(Double));
dtkotitems.Columns.Add("Amount", typeof(Double));
dtkotitems.Rows.Add(1, "", "", "", 0.00, 0.00, 0.00);
grdKotItems.DataSource = dtkotitems;
grdKotItems.Columns[0].DefaultCellStyle.BackColor = Color.Beige;
grdKotItems.Columns[0].Width = 60;
grdKotItems.Columns[1].Width = 80;
grdKotItems.Columns[2].Width = 350;
grdKotItems.Columns[3].Width = 100;
grdKotItems.Columns[4].Width = 90;
grdKotItems.Columns[5].Width = 90;
grdKotItems.Columns[6].Width = 175;
grdKotItems.Enabled = true;
grdKotItems.Columns[0].Frozen = true;
grdKotItems.Columns[1].Frozen = false;
grdKotItems.Columns[2].Frozen = true;
grdKotItems.Columns[3].Frozen = true;
grdKotItems.Columns[4].Frozen = true;
grdKotItems.Columns[5].Frozen = false;
grdKotItems.Columns[6].Frozen = true;
grdKotItems.CurrentCell = grdKotItems.Rows[0].Cells[1];
In the above code snippet the Columns[1] and Columns[5] are not frozen.
In my control class method I am using the below code on press of the 'Enter' Key to check if the column is frozen to determine the next move.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
TraverseGrid(keyData);
return base.ProcessCmdKey(ref msg, keyData);
}
void TraverseGrid(Keys KeyPressedInGridView)
{
Int32 row = grdKotItems.CurrentCell.RowIndex;
Int32 col = grdKotItems.CurrentCell.ColumnIndex;
if (KeyPressedInGridView == Keys.Enter)
{
if (col == grdKotItems.Columns.Count - 1)
{
row += 1;
dtkotitems.Rows.Add(row + 1, "", "", "", 0.00, 0.00, 0.00);
col = 1;
}
else
{
for (int mi = col + 1; mi < grdKotItems.Columns.Count; mi++)
{
if (grdKotItems.Columns[mi].Frozen == false)
{
col = mi;
break;
}
}
}
grdKotItems.CurrentCell = grdKotItems.Rows[row].Cells[col];
}
}
The underlined code always returns true. I have no idea where Iam going wrong. Kindly help.