如何确定DataGridView中的特定列是否在Windows窗体中冻结? [英] How to determine if a particular column in a DataGridView is frozen in windows forms ?
问题描述
亲爱的专业人士,
我已经实现了从 DataGridView 继承的自定义控件在我的项目中使用的所有DataGridView都有一些共同的属性和方法。
在这个控件类中,我有一个方法,当按下Enter键时设置DataGridView的currentcell。在这个阶段,我想检查我下一步要移动的列是否被冻结,以确定我的下一步行动。
我已经以编程方式创建了数据表和将它设置为DataGridView的数据源并设置一些列来冻结。
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];
在上面的代码片段中,列[1]和列[5]不是冻结。
在我的控制类方法中,我按下输入键时使用下面的代码来检查列是否被冻结以确定下一步。
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];
}
}
带下划线的代码始终返回true。我不知道我哪里出错了。请帮忙。
The underlined code always returns true. I have no idea where Iam going wrong. Kindly help.
推荐答案
我不确定是因为我正在使用我的手机,但似乎你会迭代所有的colun,直到找到一个Frozen专栏,而不仅仅是当前的。
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.
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;
在本节中,我想您只想冻结第0,2,3,4,6列,但是不可能的。如果你回过头来查看Frozen属性的文档,它会说
当一个列被冻结时,它左边的所有列(或者它的左边)从右到左的语言也被冻结。
代码的整体效果是将所有列冻结到包括列6.单行代码
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
grdKotItems.Columns[6].Frozen = true;
会产生同样的效果。
Alan。
would have had the same effect.
Alan.
这篇关于如何确定DataGridView中的特定列是否在Windows窗体中冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!