Bind DataGrid in winform

  • 2020-05-09 19:11:43
  • OfStack


this.dgTankView.Columns["ID"].DataPropertyName = "ID";
            this.dgTankView.Columns["TankName"].DataPropertyName = "Name";
            this.dgTankView.Columns["Area"].DataPropertyName = "Area";
            this.dgTankView.Columns["Type"].DataPropertyName = "Type";
            this.dgTankView.Columns["Status"].DataPropertyName = "Status";
            this.dgTankView.Columns["Material"].DataPropertyName = "Material";
            this.dgTankView.Columns["MaterialCategory"].DataPropertyName = "MaterialCategory";
            this.dgTankView.Columns["MaxLevel"].DataPropertyName = "MaxLevel";
            this.dgTankView.Columns["MinLevel"].DataPropertyName = "MinLevel";
            this.dgTankView.Columns["MaxCapacity"].DataPropertyName = "MaxCapacity";
            this.dgTankView.Columns["MinCapacity"].DataPropertyName = "MinCapacity";
            this.dgTankView.Columns["Code"].DataPropertyName = "Code";
            ////set columns width
            //this.dgTankView.Columns["MaterialCategory"].Width = 80;
            //this.dgTankView.Columns["Material"].Width = 240;
            //this.dgTankView.Columns["TankName"].Width = 100;
            //this.dgTankView.Columns["Area"].Width = 80;
            //this.dgTankView.Columns["MaxLevel"].Width = 100;
            //this.dgTankView.Columns["MinLevel"].Width = 100;
            //this.dgTankView.Columns["MaxCapacity"].Width = 100;
            //this.dgTankView.Columns["MinCapacity"].Width = 100;
            //this.dgTankView.Columns["Type"].Width = 60;
            //this.dgTankView.Columns["Status"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  this.dgTankView.AutoGenerateColumns = false;
            this.dgTankView.DataSource = tankviews;
            int rowCount = dgTankView.RowCount;
            if (SelectedIndex < rowCount || SelectedIndex >= rowCount)
            {
                this.dgTankView.Rows[0].Selected = true;
            }
            else
            {
                this.dgTankView.Rows[this.SelectedIndex].Selected = true;
            }

  for (int i = 0; i < dgrdSelectedMaterial.RowCount; i++)
            {
                if (decimal.Parse(dgrdSelectedMaterial.Rows[i].Cells["ColumnSelectedMinRatio"].Value.ToString()) > decimal.Parse(dgrdSelectedMaterial.Rows[i].Cells["ColumnSelectedMaxRatio"].Value.ToString()))
                {
                    MessageUtil.Info(string.Format(Config.MessageManager[MessageGroupHelper.FormatToName((int)MessageGroup.Client), "1959"].ToString(), dgrdSelectedMaterial.Rows[i].Cells["ColumnSelectedName"].Value.ToString()));
                    return false;
                }
            }

RowHeardersVisible=false
Selection Mode=Full row Select
ColumnHeaderDefault=alignCenter
/// high 
dgrdStaticTank.ColumnHeadersHeight = 40;
dgrdBlendMaterialQuality.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
 Width: 
  dgrdBlendMaterial.AutoGenerateColumns = false;
            dgrdBlendMaterial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgrdBlendMaterial.Columns["ColumnMaterialTankName"].Width = 80;
----------------------------------------------------------
this.dataGridView1.CurrentCell.Value;  // The value of the currently active cell 
this.dataGridView1.CurrentCell.RowIndex;// The row in which the cell is currently active 
this.dataGridView1.CurrentCell.ColumnIndex;// The column in which the currently active cell resides 
this.dataGridView1.Rows[5].Cells[3].Value;// In the first 6 Line first 4 The value of the cell in the column 
DataGridView[col,row].Value=obj;
   ControlUtil.SetDataGridViewStyleForMasterData(dgrdBlendMaterial);
            dgrdBlendMaterial.AutoGenerateColumns = false;
            dgrdBlendMaterial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgrdBlendMaterial.Columns["ColumnMaterialName"].Width = 200;
      ucPagingTank.DataBindings.Add("PageIndex", tankListModel, "PageIndex", true, DataSourceUpdateMode.Never);
----- The format in which a column is displayed ----
dgrdTank.Columns["ColumnTimeStamp"].DefaultCellStyle.Format = Config.AppConfig.DateTimeFormat;
--- Gets the value of the selected column -----
strCurRoleID = dgrdRoleList.SelectedRows[0].Cells[0].FormattedValue.ToString();


Related articles: