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[n].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]中的控件 不解