Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.

C#
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.
Posted
Updated 27-Feb-15 22:49pm
v3

I'm not sure due I'm using my phone, but it seems you iterates all the colunns until you find a Frozen column, not just the current one.
 
Share this answer
 
Comments
Priya-Kiko 28-Feb-15 6:43am    
Thanks for the response.

I am iterating through all columns in my DataGridView using a for loop and checking if that Columns[counter].Frozen == true.
Joan Magnet 28-Feb-15 7:32am    
Have you checked (debug) that columns have the .Frozen value properly set?
It seems there is nothing bad in your code.
C#
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;


With this section I think you wanted to freeze just the columns 0,2,3,4,6 but that is not possible. If you go back and look at the documentation for the Frozen property it says

When a column is frozen, all the columns to its left (or to its right in right-to-left languages) are frozen as well.

The overall effect of your code is to freeze all columns up to and including column 6. A single line of code
C#
grdKotItems.Columns[6].Frozen = true;
would have had the same effect.

Alan.
 
Share this answer
 
Comments
Priya-Kiko 2-Mar-15 1:04am    
Thanks for your response. I overlooked that. My mistake. Sorry for wasting your time. Thanks agains.
Alan N 2-Mar-15 6:22am    
Well it is a strange interface. I can't help thinking that the freezing should have been implemented in the DataGridView class with the Row and Column.Frozen properties read only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900