datagrid(winform)使用指导

在网上收集的一些代码

一、增加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());
  }

转载于:https://www.cnblogs.com/fellowcheng/articles/88550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值