日期:2014-05-18  浏览次数:21033 次

datagridview 第一列隐藏不了
为什么我的datagridview 总是隐藏不了第一列了?我的代码是这样的
C# code

    dgvMenu.Columns.Clear();
            dgvMenu.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn No = new DataGridViewTextBoxColumn();
            No.HeaderText = "No";
            No.Width = 25;
            if (!(strtext.Contains(",No,")) && ("read".Equals(strString)))
                No.Visible = false;

            DataGridViewTextBoxColumn sMenuKey = new DataGridViewTextBoxColumn();
            sMenuKey.HeaderText = "菜单编号";
            if (!(strtext.Contains(",sMenuKey,")) && ("read".Equals(strString)))
                sMenuKey.Visible = false;

            DataGridViewTextBoxColumn sMenuText = new DataGridViewTextBoxColumn();
            sMenuText.HeaderText = "菜单名称";
            if (!(strtext.Contains(",sMenuText,")) && ("read".Equals(strString)))
                sMenuText.Visible = false;

            DataGridViewTextBoxColumn sIsFather = new DataGridViewTextBoxColumn();
            sIsFather.HeaderText = "是否父菜单";
            if (!(strtext.Contains(",sIsFather,")) && ("read".Equals(strString)))
                sIsFather.Visible = false;

            DataGridViewTextBoxColumn sFatherText = new DataGridViewTextBoxColumn();
            sFatherText.HeaderText = "父菜单名称";
            if (!(strtext.Contains(",sFatherText,")) && ("read".Equals(strString)))
                sFatherText.Visible = false;

            DataGridViewTextBoxColumn iMenuLevel = new DataGridViewTextBoxColumn();
            iMenuLevel.HeaderText = "菜单级别";
            if (!(strtext.Contains(",iMenuLevel,")) && ("read".Equals(strString)))
                iMenuLevel.Visible = false;

            DataGridViewTextBoxColumn sMenuPath = new DataGridViewTextBoxColumn();
            sMenuPath.HeaderText = "菜单路径";
            if (!(strtext.Contains(",sMenuPath,")) && ("read".Equals(strString)))
                sMenuPath.Visible = false;

            DataGridViewTextBoxColumn iMenuCount = new DataGridViewTextBoxColumn();
            iMenuCount.HeaderText = "显示顺序";
            if (!(strtext.Contains(",iMenuCount,")) && ("read".Equals(strString)))
                iMenuCount.Visible = false;

            DataGridViewTextBoxColumn sRemark = new DataGridViewTextBoxColumn();
            sRemark.HeaderText = "备注";
            if (!(strtext.Contains(",sRemark,")) && ("read".Equals(strString)))
                sRemark.Visible = false;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    if (!dt.Columns.Contains("No"))
                        dt.Columns.Add("No");
                    dgvMenu.DataSource = dt;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["No"] = i + 1;
                        if ("Y".Equals(dt.Rows[i]["sIsFather"].ToString()))
                            dt.Rows[i]["sIsFather"] = "是";
                        else
                            dt.Rows[i]["sIsFather"] = "否";
                    }
                    No.DataPropertyName = dt.Columns["No"].ColumnName;                    //映射到X列
                    sMenuKey.DataPropertyName = dt.Columns["sMenuKey"].ColumnName;
                    sMenuText.DataPropertyName = dt.Columns["sMenuText"].ColumnName;
                    sIsFather.DataPropertyName = dt.Columns["sIsFather"].ColumnName;