随笔-6  评论-1  文章-7  trackbacks-0
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]中的控件 不解 
posted on 2008-04-10 12:41 romce 阅读(13) 评论(0)  编辑 收藏 所属分类: asp.net控件使用

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-04-10 13:28 编辑过
 
另存  打印