c# datagridview 基本操作 (windows form)


//添加按键列          

            DataGridViewButtonColumn dcButton = new DataGridViewButtonColumn();
            dcButton.Name = "测试";
            dcButton.HeaderText = "测试";
            dcButton.Width = 50;
            dcButton.UseColumnTextForButtonValue = true;
            dataGridView1.Columns.Insert(0, dcButton);

//添加string 列

            DataGridViewColumn drvc1 = new DataGridViewColumn();

            drvc1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;        //设置单元格 对齐方式

            drvc1.Width = 250;

            drvc1.HeaderText="说明";
            drvc1.Name = "说明";
            drvc1.CellTemplate= new DataGridViewTextBoxCell();
            dataGridView1.Columns.Add(drvc1);

//添加行

            DataGridViewRow row1 = new DataGridViewRow();
            DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
            DataGridViewButtonCell btcell = new DataGridViewButtonCell();
            btcell.Value = "全切";
            row1.Cells.Add(btcell);
            textboxcell.Value = "立即全切,指令0x1d,0x56,0x00";
            row1.Cells.Add(textboxcell);
            dataGridView1.Rows.Add(row1);

//dataset 来设置 datagridview

            写了一个简单的方法 生成dataset,见下面
            m_DataSet1 = new DataSet();
            m_DataSet1 = CreatDataSetTable("one", 5, "说明", "测试");
            dataGridView1.DataSource = m_DataSet1.Tables[0];
            

              //生成dataset方法
        private DataSet CreatDataSetTable(string tablename,string columname1,string columname2)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = columname1;
            dc.AllowDBNull = false;
            dt.Columns.Add(dc);

            dt.Columns.Add(columname2,typeof(string));
            DataRow dr = dt.NewRow();
            dr[columname1] = "立即全切,指令0x1d,0x56,0x00";
            dt.Rows.Add(dr);
            dc.ReadOnly = true;
            ds.Tables.Add(dt);
            return ds;
        }

你可能感兴趣的:(c# datagridview 基本操作 (windows form))