欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格

這篇文章主要介紹DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

在通化縣等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè),通化縣網(wǎng)站建設(shè)費(fèi)用合理。

目的:

DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格

擴(kuò)展 C# WinForm 自帶的表格控件,使其可以自動(dòng)判斷數(shù)據(jù)的上下界限值,并標(biāo)識(shí)溢出。

這里使用的方法是:擴(kuò)展 表格的列 對(duì)象:DataGridViewColumn。

1.創(chuàng)建類:DecimalCheckCell

 /// <summary>
 /// 可進(jìn)行范圍檢查的 數(shù)值單元格
 /// </summary>
 public class DecimalCheckCell : DataGridViewTextBoxCell
 {
 private bool checkMaxValue = false;
 private bool checkMinValue = false;
 private decimal maxValue = 0;
 private decimal minValue = 0;

 public decimal MaxValue
 {
  get { return maxValue; }
  internal set { maxValue = value; }
 }

 public decimal MinValue
 {
  get { return minValue; }
  internal set { minValue = value; }
 }

 public bool CheckMaxValue
 {
  get { return checkMaxValue; }
  internal set { checkMaxValue = value; }
 }
 
 public bool CheckMinValue
 {
  get { return checkMinValue; }
  internal set
  {
  checkMinValue = value;
  }
 }


 public override object Clone()
 {
  DecimalCheckCell c = base.Clone() as DecimalCheckCell;
  c.checkMaxValue = this.checkMaxValue;
  c.checkMinValue = this.checkMinValue;
  c.maxValue = this.maxValue;
  c.minValue = this.minValue;
  return c;
 }

 protected override void Paint(Graphics graphics, Rectangle clipBounds,
  Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
  object value, object formattedValue, string errorText,
  DataGridViewCellStyle cellStyle,
  DataGridViewAdvancedBorderStyle advancedBorderStyle,
  DataGridViewPaintParts paintParts)
 {
  // Paint the base content
  base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
  value, formattedValue, errorText, cellStyle,
  advancedBorderStyle, paintParts);

  // 上下界限溢出判斷
  if (this.RowIndex < 0 || this.OwningRow.IsNewRow) // 行序號(hào)不為-1,且不是新記錄行(貌似沒(méi)用)
  return;
  if (value == null) return;

  decimal vCurValue = Convert.ToDecimal(value);
  bool overValue = false;
  Image img = null;
  if (checkMaxValue)
  {
  overValue = vCurValue > maxValue;
  img = VsTest.Properties.Resources.Undo; // 圖片來(lái)自 添加的資源文件
  }
  if (checkMinValue && !overValue)
  {
  overValue = vCurValue < minValue;
  img = VsTest.Properties.Resources.Redo; // 圖片來(lái)自 添加的資源文件
  }

  // 將圖片繪制在 數(shù)值文本后面
  if (overValue && img != null)
  {
  var vSize = graphics.MeasureString(vCurValue.ToString(), cellStyle.Font);

  System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
  graphics.SetClip(cellBounds);
  graphics.DrawImageUnscaled(img, new Point(cellBounds.Location.X + (int)vSize.Width, cellBounds.Location.Y));
  graphics.EndContainer(container);
  }
 }

 protected override bool SetValue(int rowIndex, object value)
 {
  if (rowIndex >= 0)
  {
  try
  {
   decimal vdeci = Convert.ToDecimal(value); // 篩選非數(shù)字
   base.ErrorText = string.Empty;
  }
  catch (Exception ex)
  {
   base.ErrorText = "輸入錯(cuò)誤" + ex.Message;
   return false;
  }
  }
  return base.SetValue(rowIndex, value);
 }

 
 }

2.創(chuàng)建類:DecimalCheckColumn

 /// <summary>
 /// 可進(jìn)行范圍檢查的 數(shù)值列
 /// </summary>
 public class DecimalCheckColumn : DataGridViewColumn
 {
  private bool checkMaxValue = false;
  private bool checkMinValue = false;
  private decimal maxValue = 0;
  private decimal minValue = 0;

  public decimal MaxValue
  {
   get { return maxValue; }
   set
   {
    maxValue = value;
    (base.CellTemplate as DecimalCheckCell).MaxValue = value;
   }
  }

  public decimal MinValue
  {
   get { return minValue; }
   set
   {
    minValue = value;
    (base.CellTemplate as DecimalCheckCell).MinValue = value;
   }
  }

  /// <summary>
  /// 是否對(duì)值上界限進(jìn)行檢查,與MaxValue配合使用
  /// </summary>
  public bool CheckMaxValue
  {
   get { return checkMaxValue; }
   set
   {
    checkMaxValue = value;
    (base.CellTemplate as DecimalCheckCell).CheckMaxValue = value;
   }
  }
  /// <summary>
  /// 是否對(duì)值下界限進(jìn)行檢查,與MinValue配合使用
  /// </summary>
  public bool CheckMinValue
  {
   get { return checkMinValue; }
   set
   {
    checkMinValue = value;
    (base.CellTemplate as DecimalCheckCell).CheckMinValue = value;
   }
  }

  public DecimalCheckColumn()
   : base(new DecimalCheckCell())
  {
   
  }

  public override object Clone()
  {
   DecimalCheckColumn c = base.Clone() as DecimalCheckColumn;
   c.checkMaxValue = this.checkMaxValue;
   c.checkMinValue = this.checkMinValue;
   c.maxValue = this.maxValue;
   c.minValue = this.minValue;

   return c;
  }

 }

3.現(xiàn)在就可以使用了,在窗體上拖一個(gè) dataGridView 控件,添加如下代碼:

 private void TestForm_Load(object sender, EventArgs e)
  {
   InitControlsProperties(); // 初始化

   // 綁定數(shù)據(jù)
   DataTable dTabel = new DataTable();
   dTabel.Columns.Add("ID",typeof(int));
   dTabel.Columns.Add("TestValue",typeof(decimal));
   Random rnd = new Random();
   for (int i = 0; i < 10; i++) // 隨機(jī)10個(gè)數(shù)
   {
    var vdr = dTabel.NewRow();
    vdr[0] = i + 1;
    vdr[1] = rnd.Next(50);
    dTabel.Rows.Add(vdr);
   }
   this.dataGridView1.DataSource = dTabel;
  }

  private void InitControlsProperties()
  {
   DecimalCheckColumn ColumnRoleID = new DecimalCheckColumn();
   ColumnRoleID.DataPropertyName = "ID";
   ColumnRoleID.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
   ColumnRoleID.Name = "ID";
   ColumnRoleID.HeaderText = "序號(hào)";
   ColumnRoleID.Width = 50;
   this.dataGridView1.Columns.Add(ColumnRoleID);

   DecimalCheckColumn ColumnRoleName = new DecimalCheckColumn();
   ColumnRoleName.DataPropertyName = "TestValue";
   ColumnRoleName.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
   ColumnRoleName.Name = "TestValue";
   ColumnRoleName.HeaderText = "測(cè)試數(shù)據(jù)";
   ColumnRoleName.Width = 100;

   ColumnRoleName.CheckMaxValue = true; // 進(jìn)行最大值檢查
   ColumnRoleName.MaxValue = 41;
   ColumnRoleName.CheckMinValue = true; // 進(jìn)行最小值檢查
   ColumnRoleName.MinValue = 7;

   this.dataGridView1.Columns.Add(ColumnRoleName);

   //this.dataGridView1.AllowUserToAddRows = false;
   //this.dataGridView1.AllowUserToDeleteRows = false;
   //this.dataGridView1.ReadOnly = true;
   this.dataGridView1.AutoGenerateColumns = false;

  }

運(yùn)行效果如下圖左所示

DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格

DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格

以上是“DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標(biāo)題:DataGridView如何實(shí)現(xiàn)帶圖標(biāo)的單元格
本文地址:http://www.chinadenli.net/article26/ggicjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站手機(jī)網(wǎng)站建設(shè)面包屑導(dǎo)航網(wǎng)站策劃外貿(mào)建站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)