C# DataGridView 动态添加列和行 | 您所在的位置:网站首页 › datagridview添加控件 › C# DataGridView 动态添加列和行 |
https://blog.csdn.net/alisa525/article/details/7350471
dataGridView1.ReadOnly = true ; //禁用编辑功能 方法一:通过手动添加Datatable,再绑定dataGridView DataTable dt = new DataTable();//建立个数据表 dt.Columns.Add(new DataColumn("id", typeof(int)));//在表中添加int类型的列 dt.Columns.Add(new DataColumn("Name", typeof(string)));//在表中添加string类型的Name列 DataRow dr;//行 for (int i = 0; i < 3; i++) { dr = dt.NewRow(); dr["id"] = i; dr["Name"] = "Name" + i; dt.Rows.Add(dr);//在表的对象的行里添加此行 } dataGridView1.DataSource =dt; 如果要添加一个textbox效果的列,可做如下处理 dt.Columns.Add(new DataColumn("选中", typeof(bool)); 方法二:直接在dataGridView中插入 dataGridView1.ColumnCount = 4; dataGridView1.ColumnHeadersVisible = true; // Set the column header style. DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.BackColor = Color.Beige; columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold); dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle; // Set the column header names. dataGridView1.Columns[0].Name = "Recipe"; dataGridView1.Columns[1].Name = "Category"; dataGridView1.Columns[2].Name = "Main Ingredients"; dataGridView1.Columns[3].Name = "Rating"; // Populate the rows. string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef", "**" }; string[] row2 = new string[] { "Key Lime Pie", "Dessert", "lime juice, evaporated milk", "****" }; string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish", "pork chops, salsa, orange juice", "****" }; string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad", "black beans, brown rice", "****" }; string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert", "cream cheese", "***" }; string[] row6 = new string[] { "Black Bean Dip", "Appetizer", "black beans, sour cream", "***" }; object[] rows = new object[] { row1, row2, row3, row4, row5, row6 }; foreach (string[] rowArray in rows) { dataGridView1.Rows.Add(rowArray); } 插入DataGridViewCheckBoxColumn列 DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn(); { column.HeaderText = "选中"; column.Name = isSelected; column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; column.FlatStyle = FlatStyle.Standard; column.ThreeState = true; column.CellTemplate = new DataGridViewCheckBoxCell(); column.CellTemplate.Style.BackColor = Color.Beige; } DataGridView1.Columns.Insert(0, column);
//给已知道的列添加值 for (int i = 0; i < 3; i++) { int indexRow = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[indexRow].Cells["Number"].Value = "123"; this.dataGridView1.Rows[indexRow].Cells["text"].Value = "asdfad"; } //使用特性给DataGridView 表格列赋值 Type objType = typeof(ImportOrderSameGoodsDtoRequest); //取属性上的自定义特性 foreach (PropertyInfo propInfo in objType.GetProperties()) { object[] objAttrs = propInfo.GetCustomAttributes(typeof(OperationModel.AttributeClass.ModeProperty), true); if (objAttrs.Length > 0) { OperationModel.AttributeClass.ModeProperty attr = objAttrs[0] as OperationModel.AttributeClass.ModeProperty; if (attr != null) { if (attr.IsVerify) { continue; } DataGridViewTextBoxColumn dgvtxtc = new DataGridViewTextBoxColumn(); dgvtxtc.Name = propInfo.Name; dgvtxtc.DataPropertyName = propInfo.Name; dgvtxtc.HeaderText = attr.PropertyName; dataGridView1.Columns.Add(dgvtxtc); } } } //附上特性定义代码/// /// 字段属性 /// [AttributeUsage(AttributeTargets.Property, Inherited = true)] public class ModeProperty : Attribute { private ModeProperty() { } public string PropertyName { get; set; } public Boolean IsNull { get; set; } /// /// 验证字段 /// public Boolean IsVerify { get; set; } public Boolean IsList { get; set; } public Type Type { get; set; } public ModeProperty(string PropertyName) { this.PropertyName = PropertyName; } }
|
CopyRight 2018-2019 实验室设备网 版权所有 |