欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在GridControl控件上綁定圖片的幾種操作方式詳解

 更新時(shí)間:2018年07月10日 13:45:01   作者:wuhuacong(伍華聰)  
GridControl控件是經(jīng)常用來綁定數(shù)據(jù)的,一般以常規(guī)的字符內(nèi)容為主,有時(shí)候也會(huì)有圖片的顯示需要,這篇文章主要介紹了在GridControl控件上綁定圖片的幾種操作方式詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

我們知道,基于DevExpress的開發(fā)Winform的項(xiàng)目界面的時(shí)候,GridControl控件是經(jīng)常用來綁定數(shù)據(jù)的,一般以常規(guī)的字符內(nèi)容為主,有時(shí)候也會(huì)有圖片的顯示需要,那么如果顯示圖片,我們應(yīng)該如何實(shí)現(xiàn)呢?本篇隨筆介紹基于原生GridControl控件的圖片綁定顯示操作和基于我封裝的分頁(yè)控件(封裝GridControl的分頁(yè)控件)兩種圖片綁定顯示的操作。

1、基于原生的GridControl控件圖片綁定

綁定圖片,一般我們可以在單元格里面綁定byte[]類型或者Image類型,那么控件就會(huì)自動(dòng)顯示圖片出來,當(dāng)然我們也可以自定義對(duì)圖片路徑轉(zhuǎn)換為圖片然后顯示的,不過就是額外需要增加一些處理而已。

本例子針對(duì)這三種方式分別進(jìn)行介紹,圖片的綁定操作。

為了方便演示,我們創(chuàng)建一個(gè)菜單對(duì)象類,然后構(gòu)建一些數(shù)據(jù)用于列表的綁定操作,如下代碼所示。

/// <summary>
  /// 模擬一個(gè)菜單的對(duì)象,包括各種類型的圖片信息
  /// </summary>
  public class MenuInfo
  {
    /// <summary>
    /// 編號(hào)
    /// </summary>
    public string ID { get; set; }

    /// <summary>
    /// 圖標(biāo)名稱
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 圖片路徑
    /// </summary>
    public string ImageFilePath {get;set;}

    /// <summary>
    /// 圖標(biāo)字節(jié)
    /// </summary>
    public virtual byte[] EmbedIcon { get; set; }

    /// <summary>
    /// 圖標(biāo)圖片對(duì)象
    /// </summary>
    public Image ImageIcon { get; set; }


    /// <summary>
    /// 構(gòu)造函數(shù)
    /// 為了展示的方便,在構(gòu)造函數(shù)里面構(gòu)造相應(yīng)的數(shù)據(jù)
    /// </summary>
    public MenuInfo()
    {
      this.ID = Guid.NewGuid().ToString();
      this.Name = "測(cè)試圖片菜單";
      this.ImageFilePath = Path.Combine(System.Environment.CurrentDirectory, "app.ico");
      if (File.Exists(this.ImageFilePath))
      {
        this.EmbedIcon = FileUtil.FileToBytes(this.ImageFilePath);
        this.ImageIcon = ImageHelper.ImageFromUrl(this.ImageFilePath);
      }
    }

創(chuàng)建GridColumn的時(shí)候,我們可以利用GridVIew的擴(kuò)展函數(shù)CreateColumn進(jìn)行創(chuàng)建幾個(gè)不同的列,如下代碼所示。

public partial class FrmRepositoryItemImageEdit : BaseForm
  {
    public FrmRepositoryItemImageEdit()
    {
      InitializeComponent();

      CreateGridView();
    }

    /// <summary>
    /// 創(chuàng)建gridView1列表所需顯示的列
    /// </summary>
    private void CreateGridView()
    {
      //創(chuàng)建一個(gè)隱藏的ID列
      this.gridView1.CreateColumn("ID", "ID").Visible = false;
      //串一個(gè)名稱的列,并指定寬度
      this.gridView1.CreateColumn("Name", "名稱", 150);

      //創(chuàng)建一個(gè)圖片路徑的列,并指定它的編輯控件類型為RepositoryItemImageEdit
      //并為這個(gè)列實(shí)現(xiàn)ParseEditValue的方法,用于解析路徑為具體的圖片顯示
      this.gridView1.CreateColumn("ImageFilePath", "圖片路徑綁定", 100).CreateImageEdit().ParseEditValue += (s, e) =>
      {
        if (e.Value != null && e.Value is string && e.Value.ToString() != string.Empty)
        {
          e.Value = Image.FromFile(string.Concat(e.Value));
          e.Handled = true;
        }
      };

      //創(chuàng)建圖片字節(jié)的列,用于顯示圖片
      this.gridView1.CreateColumn("EmbedIcon", "圖片字節(jié)綁定", 100);
      //創(chuàng)建圖片對(duì)象的列,用于顯示圖片
      this.gridView1.CreateColumn("ImageIcon", "圖片對(duì)象綁定", 100);
    }

上面代碼是創(chuàng)建GridView所需要顯示的列信息,那么我們準(zhǔn)備好數(shù)據(jù)源綁定到列表控件上就可以了,如下代碼所示。

/// <summary>
/// 綁定列表數(shù)據(jù)
/// </summary>
private void BindData()

{
  //構(gòu)造只有一個(gè)記錄的集合
  List<MenuInfo> menuList = new List<MenuInfo>()
  {
    new MenuInfo()
  };
  //綁定數(shù)據(jù)源到列表控件上
  this.gridControl1.DataSource = menuList;
} 

2、基于分頁(yè)控件的圖片綁定

很多時(shí)候,我們需要對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行分頁(yè)顯示,以提高顯示的速度和效率,那么利用分頁(yè)控件就可以獲得很多這樣統(tǒng)一的界面和高效率顯示數(shù)據(jù)的好處,基于分頁(yè)控件的處理本質(zhì)上和上面的過程差不多,不過處理的代碼需要變化一下,從而可以正常的實(shí)現(xiàn)圖片綁定顯示操作。

/// <summary>
  /// 基于分頁(yè)控件的圖片顯示案例
  /// </summary>
  public partial class FrmRepositoryItemImageEdit2 : BaseForm
  {
    public FrmRepositoryItemImageEdit2()
    {
      InitializeComponent();

      CreateGridView();
    }

    /// <summary>
    /// 創(chuàng)建gridView1列表所需顯示的列
    /// </summary>
    private void CreateGridView()
    {
      this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
      this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
      this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
      this.winGridViewPager1.ShowLineNumber = true;
      this.winGridViewPager1.BestFitColumnWith = false;//是否設(shè)置為自動(dòng)調(diào)整寬度,false為不設(shè)置
      this.winGridViewPager1.gridView1.DataSourceChanged += new EventHandler(gridView1_DataSourceChanged);
    }

    /// <summary>
    /// 綁定數(shù)據(jù)后,分配各列的寬度
    /// </summary>
    private void gridView1_DataSourceChanged(object sender, EventArgs e)
    {
      //對(duì)圖片路徑的列,重新使用RepositoryItemPictureEdit類型
      //然后對(duì)該列的控件的ParseEditValue和FormatEditValue函數(shù)進(jìn)行實(shí)現(xiàn),從而實(shí)現(xiàn)路徑到圖片的顯示
      var edit = this.winGridViewPager1.gridView1.Columns.ColumnByFieldName("ImageFilePath").CreatePictureEdit();      
      edit.ParseEditValue += (s, se) =>
      {
        if (se.Value != null && se.Value.GetType() == typeof(string) && se.Value.ToString() != string.Empty)
        {
          if (File.Exists(string.Concat(se.Value)))
          {
            var picture = ImageHelper.ImageFromUrl(string.Concat(se.Value));
            se.Value = picture;
            se.Handled = true;

          }
        }
      };
      edit.FormatEditValue += (s, se) =>
      {
        if (File.Exists(string.Concat(se.Value)))
        {
          var picture = ImageHelper.ImageFromUrl(string.Concat(se.Value));
          se.Value = picture;
          se.Handled = true;
        }
      };


      if (this.winGridViewPager1.gridView1.Columns.Count > 0 && this.winGridViewPager1.gridView1.RowCount > 0)
      {
        //統(tǒng)一設(shè)置100寬度
        foreach (DevExpress.XtraGrid.Columns.GridColumn column in this.winGridViewPager1.gridView1.Columns)
        {
          column.Width = 120;
        }

        //可特殊設(shè)置特別的寬度
        GridView gridView = this.winGridViewPager1.gridView1;
        if (gridView != null)
        {
          //gridView.SetGridColumWidth("Note", 200);
        }
      }
    }

而在分頁(yè)控件的數(shù)據(jù)綁定的時(shí)候,我們指定列名的中文名即可,如下代碼所示

/// <summary>
    /// 綁定列表數(shù)據(jù)
    /// </summary>
    private void BindData()
    {
      #region 添加別名解析

      this.winGridViewPager1.DisplayColumns = "Name,ImageFilePath,EmbedIcon,ImageIcon";
      this.winGridViewPager1.AddColumnAlias("ID", "編號(hào)");
      this.winGridViewPager1.AddColumnAlias("Name", "名稱");
      this.winGridViewPager1.AddColumnAlias("ImageFilePath", "圖片路徑綁定");
      this.winGridViewPager1.AddColumnAlias("EmbedIcon", "圖片字節(jié)綁定");
      this.winGridViewPager1.AddColumnAlias("ImageIcon", "圖片對(duì)象綁定");

      this.winGridViewPager1.gridView1.OptionsBehavior.Editable = true;
      this.winGridViewPager1.gridView1.OptionsBehavior.ReadOnly = false;

      #endregion

      //構(gòu)造只有一個(gè)記錄的集合
      List<MenuInfo> menuList = new List<MenuInfo>()
      {
        new MenuInfo()
      };

      this.winGridViewPager1.DataSource = menuList;
    }

以上就是基于GridControl控件上綁定圖片的幾種操作方式,方便我們?cè)陧?xiàng)目中參考使用。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談c# 泛型類的應(yīng)用

    淺談c# 泛型類的應(yīng)用

    本篇文章是對(duì)c#中泛型類的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#內(nèi)存管理CLR深入講解(上篇)

    C#內(nèi)存管理CLR深入講解(上篇)

    本文詳細(xì)講解了C#內(nèi)存管理CLR的程序集和應(yīng)用程序域,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • C#中如何利用正則表達(dá)式判斷字符

    C#中如何利用正則表達(dá)式判斷字符

    這篇文章主要介紹了C#中利用正則表達(dá)式判斷字符的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • unity使用鏈表實(shí)現(xiàn)貪吃蛇游戲

    unity使用鏈表實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了unity使用鏈表實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C# WebClient類用法實(shí)例

    C# WebClient類用法實(shí)例

    這篇文章主要介紹了C# WebClient類用法實(shí)例,本文講解使用WebClient下載文件、OpenWriter打開一個(gè)流使用指定的方法將數(shù)據(jù)寫入到uri以及上傳文件示例,需要的朋友可以參考下
    2015-07-07
  • C#公眾號(hào)開發(fā)之給用戶發(fā)紅包

    C#公眾號(hào)開發(fā)之給用戶發(fā)紅包

    這篇文章主要為大家詳細(xì)介紹了C#公眾號(hào)開發(fā)之給用戶發(fā)紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • C#生成隨機(jī)驗(yàn)證碼代碼分享

    C#生成隨機(jī)驗(yàn)證碼代碼分享

    這篇文章主要分享了C#生成隨機(jī)驗(yàn)證碼代碼,另外附上使用示例,非常的簡(jiǎn)單實(shí)用,有需要的朋友可以參考下
    2014-10-10
  • C#實(shí)現(xiàn)單例模式的幾種方法總結(jié)

    C#實(shí)現(xiàn)單例模式的幾種方法總結(jié)

    這篇文章主要介紹了C#實(shí)現(xiàn)單例模式的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • winform模擬鼠標(biāo)按鍵的具體實(shí)現(xiàn)

    winform模擬鼠標(biāo)按鍵的具體實(shí)現(xiàn)

    這篇文章介紹了winform模擬鼠標(biāo)按鍵的具體實(shí)現(xiàn),有需要的朋友可以參考一下
    2013-10-10

最新評(píng)論