这是我的代码:
var model = FDBContext.oFDBEntity.tMasters.Where(t => t.trGroup_c == "POSMOD").ToList();
dgModuleView.DataSource = model;
dgModuleView.SelectionMode = DataGridViewSelectionMode.CellSelect;
dgModuleView.Columns[0].HeaderText = "Code";
dgModuleView.Columns[0].Width = 0;
dgModuleView.Columns[1].HeaderText = "Description";
dgModuleView.Columns[1].Width = 400;
dgModuleView.Columns[2].Visible = false;
dgModuleView.Columns[3].Visible = false;
dgModuleView.Columns[4].Visible = false;
dgModuleView.Columns[5].Visible = false;
dgModuleView.Columns[6].Visible = false;
dgModuleView.Columns[7].Visible = false;
dgModuleView.Columns[8].Visible = false;
dgModuleView.Columns[9].Visible = false;
var button = new DataGridViewButtonColumn
{
FlatStyle = FlatStyle.Flat,
Width = 50
};
var button1 = new DataGridViewButtonColumn
{
FlatStyle = FlatStyle.Flat,
Width = 50
};
dgModuleView.Columns.Add(button);
dgModuleView.Columns.Add(button1);
答案 0 :(得分:1)
//declare a DataGridViewCellStyle
DataGridViewCellStyle style = new DataGridViewCellStyle();
//backcolor when the button is not selected
style.BackColor = Color.Green;
//backcolor when the button is selected
style.SelectionBackColor = Color.Yellow;
var button = new DataGridViewButtonColumn
{
FlatStyle = FlatStyle.Popup,
//assign the style property to DefaultCellStyle of DataGridViewButtonColumn
DefaultCellStyle = style,
Width = 50
};
//finally, add the button column to your DataGridView control
dgModuleView.Columns.Add(button);
可以找到DataGridViewButtonColumn
(包括DefaultCellStyle
)公开的所有属性here。
可以找到DataGridView
控件中有关单元格样式的信息here。
答案 1 :(得分:0)
从formLoad()
调用此方法yourGrid.DefaultCellSyle.BackColor = Color.Green;
这会将所有单元格更改为您想要的颜色。
yourGrid.DefaultCellStyle.SelectionBackColor = Color.Yellow;
这意味着当选择行时颜色为黄色。
答案 2 :(得分:-1)
它不起作用。 DefaultCellStyle上没有BackColor
这是正确的。
var button = new DataGridViewButtonColumn
{
FlatStyle = FlatStyle.Popup,
DefaultCellStyle = ?
Width = 50
};