标签:c# datagridview
1、如何默认创建一个DataGridview为默认单元格数(n行m列)
// dataGridViewG1 // this.dataGridViewG1.AllowUserToAddRows = false; this.dataGridViewG1.AllowUserToDeleteRows = false; this.dataGridViewG1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridViewG1.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.dataGridViewG1.Location = new System.Drawing.Point(12, 12); this.dataGridViewG1.Name = "dataGridViewG1"; this.dataGridViewG1.ReadOnly = true; this.dataGridViewG1.RowTemplate.Height = 27; this.dataGridViewG1.Size = new System.Drawing.Size(250, 250); this.dataGridViewG1.TabIndex = 0; this.dataGridViewG1.RowCount = 5; this.dataGridViewG1.ColumnCount = 5;
当然这个datagridview显示的单元格只是处事单元格。接下来我们还需对单元格进行设置
2、单元格行列宽度
那怎么去掉上边那个列标题行呢?
其实我们设置一下ColumnHeaderVisiable属性就可以了,让其value=false。此属性就是是否显示列标题。同理,行的话我们可以设置一下ROHeaderVisiable属性,使其为false。
则呈现如下图所示:
同时我再次设置了列宽=50,具体代码如下:
// dataGridViewTextBoxColumn1 // //this.dataGridViewTextBoxColumn1.HeaderText = ""; this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1"; this.dataGridViewTextBoxColumn1.ReadOnly = true; this.dataGridViewTextBoxColumn1.Width = 50; // // dataGridViewTextBoxColumn2 // //this.dataGridViewTextBoxColumn2.HeaderText = ""; this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2"; this.dataGridViewTextBoxColumn2.ReadOnly = true; this.dataGridViewTextBoxColumn2.Width = 50; // // dataGridViewTextBoxColumn3 // //this.dataGridViewTextBoxColumn3.HeaderText = ""; this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3"; this.dataGridViewTextBoxColumn3.ReadOnly = true; this.dataGridViewTextBoxColumn3.Width = 50; // // dataGridViewTextBoxColumn4 // //this.dataGridViewTextBoxColumn4.HeaderText = ""; this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4"; this.dataGridViewTextBoxColumn4.ReadOnly = true; this.dataGridViewTextBoxColumn4.Width = 50; // // dataGridViewTextBoxColumn5 // //this.dataGridViewTextBoxColumn5.HeaderText = ""; this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5"; this.dataGridViewTextBoxColumn5.ReadOnly = true; this.dataGridViewTextBoxColumn5.Width = 50;其实可以通过设置
或者通过设置Columns集合属性,如下图所示:
注1:要将
this.dataGridViewTextBoxColumn3.HeaderText = "";去掉,如果这句存在就可能出现问题。
注2:
this.dataGridViewG1.ColumnCount = 5; this.dataGridViewG1.RowCount = 5;这两句一旦修改了datagridview就会没有了,所以重新写一遍最后。就会重新绘制一遍datagridview表格,就会绘制成如下图所示:
也就是说,行的宽度是根据datagridview平均分配的,如果最后设置行列数的话。如果不行的话可以设置
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; dataGridView1.RowTemplate.Height = 50;
3、设置单元格颜色
首先我们要得到单元格的位置,然后再进行对其style的backcolor进行设置,具体代码如下:
this.dataGridViewG6.Rows[3].Cells[2].Style.BackColor = System.Drawing.Color.Black;
BackColor可根据属性BackColor设置的值进行对其赋值.引用的颜色来源显而易见:System.Drawing.Color类。
效果图如下所示:
标签:c# datagridview
原文地址:http://blog.csdn.net/ztzi321/article/details/40079683