• 使用SunnyUI的datagridview常用代码(个人常用)


    1、窗体加载时初始化grid
    private void LayOut() {

    dgv.Font = new System.Drawing.Font("微软雅黑", 9F);
    dgv.AutoGenerateColumns = false;
    dgv.AllowUserToAddRows = false;
    dgv.AllowUserToResizeColumns = true;
    dgv.AllowUserToOrderColumns = true;

    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    checkBoxColumn.Width = 40;
    checkBoxColumn.Name = "Check";
    checkBoxColumn.HeaderText = "全选";
    dgv.Columns.Add(checkBoxColumn);

    CommonUtil.AddDgvColumns2(dgv, "id", "ID", 110);
    CommonUtil.AddDgvColumns2(dgv, "name", "名称", 0, false);
    CommonUtil.AddDgvColumns(dgv, "Img", "照片", 90);

    }

    2、添加公共类
    //公共类
    public class CommonUtil{
    public
    static void AddDgvColumns(DataGridView dgv, string dgvName, string dgvHeadertext, int dgvWidth = 50, bool dgvVisible = true, bool dgvReadonly = true, bool _Frozen = false) {   if (dgvName == "Img")   {     DataGridViewImageColumn column = new DataGridViewImageColumn();     column.ImageLayout = DataGridViewImageCellLayout.Zoom;     column.Name = dgvName;     column.DataPropertyName = dgvName;     column.HeaderText = dgvHeadertext;     column.Width = dgvWidth;     column.Visible = dgvVisible;     column.Frozen = _Frozen;//是否冻结     dgv.Columns.Add(column);  } else {      dgvWidth = dgvWidth < 2 ? 2 : dgvWidth;     DataGridViewColumn column = new DataGridViewTextBoxColumn();     column.Frozen = _Frozen;//是否冻结     column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;     column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;     column.ReadOnly = dgvReadonly;     column.Name = dgvName;     column.DataPropertyName = dgvName;     column.HeaderText = dgvHeadertext;     column.Width = dgvWidth;     column.Visible = dgvVisible;     column.MinimumWidth = dgvWidth; //column.SortMode = DataGridViewColumnSortMode.NotSortable;     dgv.Columns.Add(column);   } } public static void AddDgvColumns2(DataGridView dgv, string dgvName, string dgvHeadertext, int dgvWidth = 50, bool dgvVisible = true, bool dgvReadonly = true, bool _Frozen = false) { dgvWidth = dgvWidth < 2 ? 2 : dgvWidth; DataGridViewColumn column = new DataGridViewTextBoxColumn(); column.Frozen = _Frozen;//是否冻结 column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; column.ReadOnly = dgvReadonly; column.Name = dgvName; column.DataPropertyName = dgvName; column.HeaderText = dgvHeadertext; column.Width = dgvWidth; column.Visible = dgvVisible; column.MinimumWidth = dgvWidth; if (!dgvReadonly) { column.HeaderText = dgvHeadertext + "*"; } //column.SortMode = DataGridViewColumnSortMode.NotSortable; dgv.Columns.Add(column); } }

     3、数据绑定

    private void LoadList(){
      string sql="select * from user";
      DataSet ds=new DataSet();
       SqlHelper.ExecuteDataSet(ref ds, sql); 
      DataTable dt2 = ds.Tables[1];
         dt.Columns.Add("Img", typeof(Image));
          dgv.Columns["Img"].DefaultCellStyle.NullValue = null;
          dgv.DataSource = dt2;
                for (int i = 0; i < dgv.RowCount; i++)
                {
                    string imgpath = dgv.Rows[i].Cells["imgUrl"].Value.ToString();
                    dgv.Rows[i].Cells["Img"].Value = CommonUtil.GetFtpImage(imgpath);//获取照片对象
                }
    }

    分页使用别的方式

  • 相关阅读:
    深究递归和迭代的区别、联系、优缺点及实例对比
    提高Python运行效率的六个窍门
    C++设计模式——单例模式
    使用Python的turtle库实现七段数码管绘制
    Python 死循环和嵌套循环
    Python 随机数 random
    更改 pandas dataframe 中两列的位置
    Pandas中DataFrame修改列名
    MM 算法与 EM算法概述
    机器学习中的训练集、验证集和测试集
  • 原文地址:https://www.cnblogs.com/bklsj/p/15091361.html
Copyright © 2020-2023  润新知