您的位置:首页 > 其它

DataGrid 通过内部控件获取所在控件的列索引

2008-04-10 12:41 281 查看
DataGrid 通过内部控件获取所在控件的列索引, 原来以为列所引很容易通过属性得到,没想到费了一番周转,花了不少时间,太过费劲了,不知有更简单的方法没?

foreach (DataGridItem i in this.DtGrid.Controls[0].Controls)

{

if (i.ItemType == ListItemType.Header)

{

for (int month_i = 1; month_i <= 12; month_i++)

{

Label myLabel = (Label)i.FindControl("lblMonth" + month_i.ToString());

myLabel.Text = myYear.ToString() + "." + myMonth.ToString();

myMonth++;

if (myMonth > 12)

{

myMonth = myMonth - 12;

myYear += 1;

}

//判断改列是否要隐藏,是则隐藏

for(int m =0; m < i.Cells.Count; m++)

{

if(i.Cells[m].Controls.Contains(myLabel))

{

bool isHas = false;

for (int n = 0; n < yearMonth.Length; n++)

{

if(myLabel.Text == yearMonth
.Replace('-','.'))

{

isHas= true;

break;

}

}

if(!isHas)

{

DtGrid.Columns[m].Visible = false;

}

break;

}

}

}

break;

}

}

}

关键是通过 i.Cells[m].Controls.Contains(控件)判断是否在cells[m]存在要找的控件

用cells[].findControls()是找不到对应列的控件,居然会把整个cells[]所在行的控件都找出来

如cells[0]。findControls()可以找到cells[1]中的控件 不解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: