心静自然凉~

一杯清茶、二盏淡酒、邀三五知己,诉七八句心语,道九分珍重,怀十分真诚,或伤感、或快乐,无现实之隔阂 、无世俗之势利,如此人生,不亦乐乎!

导航

dataGrid 使用记录

Posted on 2009-07-28 22:22  Leo.Zhu  阅读(246)  评论(0编辑  收藏  举报

DataGrid绑定Dataset,设定cell控件。

设定隐藏的栏位:

设定DataSet的Table对应的栏位为Hidden,绑定后DataGrid上不显示该栏位:

Depds.Tables["DepartMent1"].Columns["Pkey"].ColumnMapping = MappingType.Hidden;

设定DataGrid的样式:

 

               DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
               dgdtblStyle.MappingName = this.Depds.Tables["XX"].TableName;
               if (dataGrid1.TableStyles.Contains("XX"))
                   dataGrid1.TableStyles.RemoveAt(0);

添加样式
               dataGrid1.TableStyles.Add(dgdtblStyle);
               dgdtblStyle.RowHeadersVisible = false;
               //dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;           
               dgdtblStyle.AllowSorting = false;
               //dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107);
               // dgdtblStyle.HeaderBackColor = Color.DarkGray;
               dgdtblStyle.RowHeadersVisible = true;
               dgdtblStyle.HeaderForeColor = Color.Black;

设定字体:
               dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F,
               System.Drawing.FontStyle.Bold,
               System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
               //dgdtblStyle.GridLineColor = Color.DarkGray;

设定显示的高度:
               dgdtblStyle.PreferredRowHeight = 20;
               //dataGrid1.BackgroundColor = Color.White;               
               GridColumnStylesCollection colStyle = dataGrid1.TableStyles[0].GridColumnStyles;

设定显示的栏位名称:
                   colStyle[0].HeaderText = "a";
                   colStyle[1].HeaderText = "b";
                   colStyle[2].HeaderText = "c";
                   colStyle[3].HeaderText = "d";
                   colStyle[4].HeaderText = "e";
                   colStyle[5].HeaderText = "f";

//设置其下的dropbox  CreateDropBox会创建一个DropBox控件。
                DataGridTextBoxColumn f1 = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[1];
                f1.TextBox.Controls.Add(CreateDropBox());
                DataGridTextBoxColumn f2 = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[2];
                f2.TextBox.Controls.Add(CreateDropBox());
                DataGridTextBoxColumn f3 = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[3];
                f3.TextBox.Controls.Add(CreateDropBox());
                DataGridTextBoxColumn f4 = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[4];
                f4.TextBox.Controls.Add(CreateDropBox());
                DataGridTextBoxColumn f5 = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[5];
                f5.Format = "yyyy/MM/dd HH:ss:mm";

 

public ComboBox CreateDropBox()
      {
          try
          {
              ComboBox cmbFunctionArea = new ComboBox();

              cmbFunctionArea.Items.AddRange(Floorlist.ToArray());
              cmbFunctionArea.Cursor = Cursors.Arrow;
              cmbFunctionArea.DropDownStyle = ComboBoxStyle.DropDownList;
              cmbFunctionArea.Dock = DockStyle.Fill;
              cmbFunctionArea.SelectedIndex = 0;
              cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted);
              return cmbFunctionArea;
          }
          catch (System.Exception exp)
          {
          }
          return null;
      }

 

收…………