我是由数据库里一次性将一个表抽到DataSet然后再放到DataGridViiew里的,但这样会将所有SELECT出来的字段都在DataGridViiew控件里显示出来,我现在只想显示一部分字段,但在SQL SELECT里是要全部字段查出来的,因为我另有他用,但不想显示出来,请问如何操作?谢了!

解决方案 »

  1.   

    请问能说清楚些吗?这个控件并没有PageSize的属性啊
      

  2.   

    设置某利的visible为false就可以了
      

  3.   

    gridview的edit columns里写自己想要的列啊,把autogeneratecolumns属性设为false
      

  4.   

    绑定到BindingSource后,在DataGridView控件的Columns属性里面(单击按钮)选中某一列,将它的Visible属性设为false
      

  5.   

    邦定示例:/// <summary>
        /// 在GridView上邦定数据
        /// </summary>
        /// <param name="delflag">false-正常绑定,不显示删除的数据
        ///                     true-正常绑定,显示所有数据包括删除的数据</param>
        private void Bind(string bh, string qssj,string kssj,string jssj, bool delflag)
        {
            string[] arrHeadName ={ "流水号", "车次编号", "车辆编号", "最大载客数", "是否加座", "加座数", "车主", "驾驶员", "发车时间", "检票口设置", "车次种类", "检票时间设置", "线路编号","是否可联网售票","可否预售","预售天数","开行方式","车次票价计划编号","票价是否调整","调整票价编号","车次报班卡编号","报班状态","运行状态","隶属公司编号","操作人","操作时间","是否为新增车次","车主名称","标志" };
            //                          0         1            2            3            4          5        6        7          8           9            10            11              12             13         14         15         16            17                18             19              20            21         22           23          24         25           26          27         28
            string[] arrFieldName ={ "流水号", "车次编号", "车辆编号", "最大载客数", "是否加座", "加座数", "车主", "驾驶员", "发车时间", "检票口设置", "车次种类", "检票时间设置", "运行线路编号", "是否可联网售票", "可否预售", "预售天数", "开行方式", "车次票价计划编号", "票价是否调整", "调整票价编号", "车次报班卡编号", "报班状态", "运行状态", "隶属公司编号", "操作人", "操作时间", "是否为新增车次", "车主名称", "标志" };
            string[] arrColStyle ={ "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1","1" };
            string[] arrFormat ={ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
            double[] arrWidth ={ 0, 10, 10, 0, 0, 0, 0, 0, 20, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0,10, 0 };
            string[] arrUrlFields ={ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
            bool[] arrVisible ={ false, true, true, false, false, false, false, false, true, false, false, false, true, false, false, false, true, false, false, false, false, true, true, false, false, false, false,true, false  };
            TDGridView1.AddCol(arrHeadName, arrFieldName, arrColStyle, arrFormat, arrWidth, arrUrlFields, arrVisible, dynPara.Lsccgl(bh, qssj,kssj,jssj, delflag));
        }
    AddCol方法如下:/// <summary>
            /// binding GridView,auto create column and give 
            /// the head、value、format、width and so on
            /// </summary>
            /// <param name="HeadName">header name</param>
            /// <param name="FieldNames">database field name</param>
            /// <param name="ColStyle">column style:1-BoundField</param>
            /// <param name="ColFormat">format</param>
            /// <param name="ColWidth">width</param>
            /// <param name="urlFields">url</param>
            /// <param name="Visibles">visible</param>
            /// <param name="dv">dataView</param>
            /// <returns>return true if success
            ///         or return false when failture</returns>
            public bool AddCol(string[] HeadName, string[] FieldNames, string[] ColStyle, string[] ColFormat, double[] ColWidth, string[] urlFields, bool[] Visibles,DataView dv)
            {
                try
                {
                    #region old code
                    //this.Columns.Clear();
                    //this.AutoGenerateColumns = false;
                    //for (int i = 0; i < ColStyle.Length; i++)
                    //{
                    //    switch (ColStyle[i])
                    //    {
                    //        case "1"://绑定列
                    //            BoundField BdCol = new BoundField();
                    //            BdCol.DataField = FieldNames[i];
                    //            BdCol.HeaderText = HeadName[i];
                    //            if (ColFormat[i] != null)
                    //            {
                    //                BdCol.DataFormatString = ColFormat[i];
                    //            }
                    //            BdCol.HeaderStyle.Width = Unit.Percentage(ColWidth[i]);
                    //            if (Visibles[i] != true)
                    //            {
                    //                BdCol.Visible = false;
                    //            }
                    //            this.Columns.Add(BdCol);
                    //            break;
                    //    }
                    #endregion
                    this.Columns.Clear();
                    this.AutoGenerateColumns = false;
                    for (int i = 0; i < ColStyle.Length; i++)
                    {
                        switch (ColStyle[i])
                        {
                            case "1"://bound field
                                BoundField BdCol = new BoundField();
                                BdCol.DataField = FieldNames[i];
                                BdCol.HeaderText = HeadName[i];
                                if (ColFormat[i] != null)
                                {
                                    BdCol.DataFormatString = ColFormat[i];
                                }                            
                                this.Columns.Add(BdCol);
                                break;
                            case "2"://checkbox field
                                CheckBoxField TpCol = new CheckBoxField ();
                                TpCol.DataField = FieldNames[i];
                                TpCol.HeaderText = HeadName[i];
                                //if (ColFormat[i] != null)
                                //{
                                //    TpCol.DataFormatString = ColFormat[i];
                                //}
                                this.Columns.Add(TpCol);
                                break;
                        }
                    }                if (dv.Table .Rows .Count == 0)
                    {
                        dv.Table.Rows.Add(dv.Table.NewRow());
                    }
                    
                    //binding dataSource
                    this.DataSource = dv;
                    this.DataBind();                   for (int i = 0; i < ColStyle.Length; i++)
                    {
                        switch (ColStyle[i])
                        {
                            case "1"://binding column
                                //visible setting
                                if (Visibles[i] != true)
                                {
                                    this.Columns[i].Visible = false;
                                }
                                //width setting
                                this.Columns[i].HeaderStyle.Width = Unit.Percentage(ColWidth[i]);
                                break;                    }
                    }  
                    return true;
                }
                catch
                {
                    return false;
                }
            }