fellowcheng

无可奈何花落去

博客园 首页 新随笔 联系 订阅 管理

在网上收集的一些代码

一、增加COMBOBOX列:
//在网格控件中增加一列为下拉列表框
   DataGridTableStyle dgdtblStyle = new DataGridTableStyle();

    dgdtblStyle.MappingName = dtlList.TableName;
    dataGridIn.TableStyles.Add(dgdtblStyle);

   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dataGridIn.TableStyles[0].GridColumnStyles[columnsName];
   ComboBox cmbFunctionArea = new ComboBox();

   object[] fID=new object[dtlFId.Rows.Count];
   for(int i=0;i<fID.Length;i++)
    fID[i]=new object();

   cmbFunctionArea.Items.AddRange(fID);

   cmbFunctionArea.Cursor = Cursors.Arrow;
   cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
   cmbFunctionArea.Dock = DockStyle.Fill;
   // 在选定项发生更改并且提交了该更改后发生
   cmbFunctionArea.SelectionChangeCommitted +=new EventHandler(cmbFunctionArea_SelectionChangeCommitted);
   // 把ComboBox添加到DataGridTableStyle的第一列
   dgtb.TextBox.Controls.Add(cmbFunctionArea);


private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
  {
   this.dataGridIn[this.dataGridIn.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();
  }

二、隐藏一列
       DataGridTableStyle dgdtblStyle = new DataGridTableStyle();

  dgdtblStyle.MappingName = dtl.TableName;
  yourdataGrid.TableStyles.Add(dgdtblStyle);
       yourdataGrid.TableStyles[0].GridColumnStyles[columnsName].Width=0;

三、改变颜色
      public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);

  private void setDataGrid()
  {
   DataGridTableStyle tableStyle = new DataGridTableStyle();
   tableStyle.MappingName = "表";

   int numCols = fl_DB.Tables["表"].Columns.Count;
   DataGridCellColorTextBoxColumn columnTextColumn ;
   for(int i = 0; i < numCols; ++i)
   {
    columnTextColumn = new DataGridCellColorTextBoxColumn();
    columnTextColumn.HeaderText = fl_DB.Tables["表"].Columns[i].ColumnName;
    columnTextColumn.MappingName = fl_DB.Tables["表"].Columns[i].ColumnName;

    //为每个单元格建立设置背景色的事件.
    columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);

    tableStyle.GridColumnStyles.Add(columnTextColumn);
   }
  
   this.dgBrowse.TableStyles.Clear();
   this.dgBrowse.TableStyles.Add(tableStyle);
   DataTable dtlSort=new DataTable("表");
   
   for (int i=0;i<fl_DB.Tables["表"].Columns.Count;i++)
   {
    dtlSort.Columns.Add(fl_DB.Tables["表"].Columns[i].ToString());
   }
   
   //dtlSort.Rows.Add(fl_DB.Tables["表"].Select("数量<>0","数量 DESC"));
   DataRow[] dr=fl_DB.Tables["表"].Select("数量>=0","数量 DESC");

   MessageBox.Show(dr.Length.ToString()+fl_DB.Tables["表"].Rows.Count.ToString());
   for (int i=0;i<fl_DB.Tables["表"].Rows.Count;i++)
   {
    dtlSort.ImportRow(dr[i]);
   }
   
   this.dgBrowse.DataBindings.Clear();
   this.dgBrowse.DataSource = dtlSort;
  }
  
  public void SetColorValues(object sender, DataGridCellColorEventArgs e)
  {
   //根据条件, 将相关行设置不同的背景色.
   //数量>1000,数量<100
   if (Convert.ToInt16(dgBrowse[e.Row,1])>1000)
    e.BackColor=Color.PowderBlue;
   else if (Convert.ToInt16(dgBrowse[e.Row,1])<100)
    e.BackColor=Color.Pink;
  }

public class DataGridCellColorEventArgs : EventArgs
  {
   private int _row;
   private Color _backcolor;

   public DataGridCellColorEventArgs(int row, Color val)
   {
    _row = row;
    _backcolor = val;
   }
   public int Row
   {
    get{ return _row;}
    set{ _row = value;}
   }
   public Color BackColor
   {
    get{ return _backcolor;}
    set{ _backcolor = value;}
   }
  }

 
  public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
  {
   public event CellColorEventHandler CheckCellColor;

   public DataGridCellColorTextBoxColumn()
   {
   }

   //继承DataGridTextBoxColumn的Pain事件.
   protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds,

    System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush,

    System.Drawing.Brush foreBrush, bool alignToRight)
   {
    if(CheckCellColor != null)
    {
     //重绘画时,设置当前行的背景色
     DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
     CheckCellColor(this, e);
   
     if(e.BackColor != Color.White)
      backBrush = new SolidBrush(e.BackColor);
    }

    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
   }

   protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum,

    System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
   {
    base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
   }
  }
四、设置Grid中textBox的KeyPress事件
  //测试GRID的KeyPress事件
  private void button1_Click(object sender, System.EventArgs e)
  {
   DataGridTableStyle dgdtblStyle = new DataGridTableStyle();

    dgdtblStyle.MappingName = this.dtlTray.TableName;
    this.gridTray.TableStyles.Add(dgdtblStyle);

   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)this.gridTray.TableStyles[0].GridColumnStyles["条码"];
   dgtb.TextBox.KeyPress+=new KeyPressEventHandler(TextBox_KeyPress);

  }

  private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   MessageBox.Show(e.KeyChar.ToString());
  }

posted on 2005-01-08 10:23  乌鸦  阅读(1356)  评论(0编辑  收藏  举报