//窗体加载事件
//内容居中
dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//列名居中
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//第一列禁止修改
dataGridView1.Columns[].ReadOnly = true;
//单元格第一列第一行 背景色
dataGridView1.Rows[].Cells[].Style.BackColor = Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
去掉最左侧
RowHeadVisible属性设置为false
//添加行
for (int i=;i<=;i++)
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[].Value = i; }
////第一行第一列 添加数据
//dataGridView1.Rows[0].Cells[0].Value = "1";
//dataGridView1.Rows[1].Cells[0].Value = "2";
//dataGridView1.Rows[2].Cells[0].Value = "3";
//dataGridView1.Rows[3].Cells[0].Value = "4";
//dataGridView1.Rows[4].Cells[0].Value = "5";
//循环读取DataGridView
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
object cell0 = row.Cells[].Value; //
object cell1 = row.Cells[].Value; //
object cell2 = row.Cells[].Value; //3
object cell3 = row.Cells[].Value; //4
object cell4 = row.Cells[].Value; //5
object cell5 = row.Cells[].Value; //
object cell6 = row.Cells[].Value; //
}
原来在dataGridView的编辑列中有个属性DataPropertyName,在这里设置字段名称即可显示,查询结果的字段,全部添加上列后,就不会自动产生列了 //【没有测试】
//窗体样式
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;// 居中
this.WindowState = System.Windows.Forms.FormWindowState.Maximized; //窗体最大化
/// <summary>
/// 显示行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + );
}
public void SetDataGrieViewStyle()
{
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
dataGridView1.AutoGenerateColumns = false; //是否自动创建列
//dataGridView1.Columns[0].ReadOnly = true; //第一列禁止修改
this.dataGridView1.TopLeftHeaderCell.Value ="号"; //设置左上角标题
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; //禁止调整左侧列 this.dataGridView1.AllowUserToAddRows = false; //去除最后一个空白行
dataGridView1.ReadOnly = true;//只读属性
dataGridView1.AllowUserToResizeColumns = false; //禁止调整列宽
dataGridView1.AllowUserToResizeRows = false;//禁止调整行宽 this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选中整行不选中单元格
this.dataGridView1.MultiSelect = false;//不能选中多行,只能选中一行
}
/// <summary>
/// 添加自定义数据
/// </summary>
public void AddDataGridViewContent()
{
//添加列
this.dataGridView1.Columns.Add("", "");
this.dataGridView1.Columns.Add("", "");
this.dataGridView1.Columns.Add("", "");
//添加值
this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值");
this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值"); for (int i=;i<;i++)
{
this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值");
this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值");
}
}
//设置某列颜色
var color = ColorTranslator.FromHtml("#51C83D");
//var font = new Font("", Convert.ToInt32("")); for (int i=;i<dataGridView1.Rows.Count;i++)
{
if (i % == )
{
//dataGridView1.SelectedCells[i].Style.Font = font;
dataGridView1.Rows[i].Cells[].Style.ForeColor = color;
//var ss = dataGridView1.Rows[i].Cells[0].Value; }
}
在编辑列中, HeaderText 姓名 是显示的字段
Data DataPropertyName 姓名 -- 这是对应的数据库查询出数据的列
Design (Name) dgv姓名 - -这是 获取编辑框的内容
//隐藏某列
dgv.Columns["产品ID"].Visible = false;
dgv.Columns["别名"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//设置某列 自动列宽 dgv.Columns["别名"].SortMode = DataGridViewColumnSortMode.Programmatic; //自定义排序 dgv.Sort(dgv.Columns[nColumnIndex], System.ComponentModel.ListSortDirection.Descending); //设置某列排序 设置显示格式 百分比显示
//dgv.Columns["A"].DefaultCellStyle.Format = "P"; //以百分号显示 "0\\%";
dgv.Columns["A"].DefaultCellStyle.Format = "0.00\\%"; //以百分号显示 "0\\%";