最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年08月26日 08:43:51   作者:云夢鴻  
這篇文章主要為大家詳細(xì)介紹了DataGridView帶圖標(biāo)的單元格的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了DataGridView帶圖標(biāo)的單元格實(shí)現(xiàn)具體代碼,供大家參考,具體內(nèi)容如下

目的:

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

這里使用的方法是:擴(kuò)展 表格的列 對象: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) // 行序號不為-1,且不是新記錄行(貌似沒用)
  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; // 圖片來自 添加的資源文件
  }
  if (checkMinValue && !overValue)
  {
  overValue = vCurValue < minValue;
  img = VsTest.Properties.Resources.Redo; // 圖片來自 添加的資源文件
  }

  // 將圖片繪制在 數(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>
  /// 是否對值上界限進(jìn)行檢查,與MaxValue配合使用
  /// </summary>
  public bool CheckMaxValue
  {
   get { return checkMaxValue; }
   set
   {
    checkMaxValue = value;
    (base.CellTemplate as DecimalCheckCell).CheckMaxValue = value;
   }
  }
  /// <summary>
  /// 是否對值下界限進(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 = "序號";
   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 = "測試數(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)行效果如下圖左所示

 那右邊圖是什么?

現(xiàn)在還有一個(gè)問題沒有解決:默認(rèn)第一次加載出來的數(shù)據(jù),并不能完全判斷出是否超界限,有時(shí)會有一兩個(gè)能判斷,有時(shí)完全不能判斷,但只需要用鼠標(biāo)去點(diǎn)擊各單元格,它又可以自動識別。暫時(shí)沒有發(fā)現(xiàn)問題原因所在。

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法

    C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法

    這篇文章主要介紹了C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法,是C#應(yīng)用程序設(shè)計(jì)中非常實(shí)用的一個(gè)功能,需要的朋友可以參考下
    2014-08-08
  • C#在MySQL大量數(shù)據(jù)下的高效讀取、寫入詳解

    C#在MySQL大量數(shù)據(jù)下的高效讀取、寫入詳解

    最近由于工作的原因,經(jīng)常需要對海量數(shù)據(jù)進(jìn)行處理,做的數(shù)據(jù)爬蟲相關(guān),動輒千萬級別的數(shù)據(jù),單表幾十個(gè)G 都是都是家常便飯。 那么主要的開發(fā)語言是C#,數(shù)據(jù)庫使用的是MySQL。下面通過這篇文章我們來一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • C# 清除cookies的代碼

    C# 清除cookies的代碼

    不同的瀏覽器會把cookie文件保存在不同的地方.這篇文章主要介紹了C# 清除cookies的代碼,需要的朋友可以參考下
    2016-10-10
  • C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法

    C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法

    這篇文章主要介紹了C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法,涉及C#實(shí)現(xiàn)單實(shí)例程序運(yùn)行的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • 使用C#實(shí)現(xiàn)簡單的線性回歸的代碼詳解

    使用C#實(shí)現(xiàn)簡單的線性回歸的代碼詳解

    最近注意到了NumSharp,想學(xué)習(xí)一下,最好的學(xué)習(xí)方式就是去實(shí)踐,因此從github上找了一個(gè)用python實(shí)現(xiàn)的簡單線性回歸代碼,然后基于NumSharp用C#進(jìn)行了改寫,需要的朋友可以參考下
    2024-01-01
  • C#文件流進(jìn)行壓縮和解壓縮的方法

    C#文件流進(jìn)行壓縮和解壓縮的方法

    這篇文章主要介紹了C#文件流進(jìn)行壓縮和解壓縮的方法,涉及C#文件流操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#獲取注冊表指定鍵值操作

    C#獲取注冊表指定鍵值操作

    這篇文章主要介紹了C#獲取注冊表指定鍵值操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C# 8.0中的范圍類型(Range Type)示例詳解

    C# 8.0中的范圍類型(Range Type)示例詳解

    這篇文章主要給大家介紹了關(guān)于C# 8.0中范圍類型(Range Type)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • c#字符長度查詢代碼

    c#字符長度查詢代碼

    下面的代碼用了查詢字符串中的一些類型,需要的朋友可以參考下
    2012-06-06
  • C# FileStream文件讀寫詳解

    C# FileStream文件讀寫詳解

    本文主要介紹C#使用 FileStream 讀取數(shù)據(jù),寫入數(shù)據(jù)等操作,希望能幫到大家。
    2016-04-04

最新評論

仙桃市| 沙洋县| 富宁县| 淮滨县| 松潘县| 弥勒县| 开远市| 汶川县| 汕尾市| 米林县| 岗巴县| 大邑县| 钦州市| 莱西市| 康平县| 阜康市| 吉木乃县| 紫云| 建昌县| 和硕县| 保靖县| 绥化市| 琼结县| 江北区| 淮北市| 岳普湖县| 滦平县| 信宜市| 鄂伦春自治旗| 岳池县| 逊克县| 龙里县| 滨海县| 衢州市| 绥阳县| 长春市| 新邵县| 兴安盟| 漳浦县| 清新县| 麻栗坡县|