C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > 绑定winform中DataGrid

绑定winform中DataGrid

作者:

绑定winform中DataGrid,需要的朋友可以参考一下

复制代码 代码如下:

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

///高
dgrdStaticTank.ColumnHeadersHeight = 40;
dgrdBlendMaterialQuality.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

宽:
  dgrdBlendMaterial.AutoGenerateColumns = false;
            dgrdBlendMaterial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgrdBlendMaterial.Columns["ColumnMaterialTankName"].Width = 80;

----------------------------------------------------------

this.dataGridView1.CurrentCell.Value;  //当前处于活动状态的单元格的值

this.dataGridView1.CurrentCell.RowIndex;//当前处于活动状态的单元格所在的行

this.dataGridView1.CurrentCell.ColumnIndex;//当前处于活动状态的单元格所在的列

this.dataGridView1.Rows[5].Cells[3].Value;//位于第6行第4列的单元格的值
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);

-----某列的显示格式----
dgrdTank.Columns["ColumnTimeStamp"].DefaultCellStyle.Format = Config.AppConfig.DateTimeFormat;

---取得选中某列的值-----
strCurRoleID = dgrdRoleList.SelectedRows[0].Cells[0].FormattedValue.ToString();


您可能感兴趣的文章:
阅读全文