C#dataGridView属性 表格设置

1.设置行高

dataGridView1.RowTemplate.Height = 30;

2.行标题的列显示 改变颜色

 dataGridView1. EnableHeadersVisualStyles = False;
 dataGridView1.RowsaDefaultCellStyle.backColor = Gray;

3.行标题中小三角号去除

dataGridView1.RowHeadersDefaultCellStyle.Padding = new Padding(dataGridView1.RowHeadersWidth);

4.默认不选中任何单元格

 dataGridView1.ClearSelection();

5.添加一列

dataGridView1.Columns.Add("ID", "ID");
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
         dataGridView1.Rows[i].Cells["ID"].Value = i + 1;
        }

6.添加表格

            //添加表格(命名)
            DataTable dataTable = new DataTable("data");
            //添加列(命名)
            DataColumn dataColumn = new DataColumn("dataName");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("dataValue");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("danWei");
            dataTable.Columns.Add(dataColumn);
			//添加行(数组)
            dataTable.Rows.Add(new object[] { "11", "22", "33" });
            dataTable.Rows.Add(new object[] { "14", "72", "39" });
            dataGridView1.DataSource = dataTable;

7.设置添加表格的列宽

 dataGridView1.Columns[0].Width = (int)(dataGridView1.Width * 0.61);
 dataGridView1.Columns[1].Width = (int)(dataGridView1.Width * 0.33);

8.根据条件设置相应行的颜色 CellFormatting 事件中

  private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string result =(this.dataGridView1.Rows[e.RowIndex].Cells["alarmSituation"].Value).ToString();
                 dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = result == "报警"
                    ? System.Drawing.Color.FromArgb(195, 13, 35)
                    : System.Drawing.Color.FromArgb(0, 0, 0);
            }
        }

9.改变对应单元格的字体颜色 CellFormatting 事件中

 if (e.ColumnIndex == this.dataGridView.Columns["checkResult"].Index)//根据检测结果设置单元格样式
        {
            if (this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value
                && (this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).ToString() == "NG")
                //对NG设置特殊样式
            {
                this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置NG的字显示为红色
                this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
            }
        }

10.//根据条件改变行背景色 CellFormatting 事件中

if(e.RowIndex > -1)
    {
    string result = (this.dataGridView.Rows[e.RowIndex].Cells["checkResult"].Value).ToString();
    dataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = result == "ok" ? Color.White : Color.Red;
}

11.取消排序

foreach (DataGridViewColumn dataGridViewColumn in dataGridView1.Columns)
                dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;

12.设置行单元格的默认样式RowsDefaultCellStyle

13.设置滚动条ScrollBars

14.设置表格为只读模式dataGridView1.ReadOnly = true;

15单元格网格线的颜色GridColor

你可能感兴趣的:(c#)