其他分享
首页 > 其他分享> > 使用SunnyUI的datagridview常用代码(个人常用)

使用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);//获取照片对象
            }
}

 

分页使用别的方式

标签:常用,SunnyUI,dgvName,column,datagridview,Frozen,dgv,dgvWidth,string
来源: https://www.cnblogs.com/bklsj/p/15091361.html