我选择在GridView控件如何在C#的颜色选择在GridView的行
gridView1.SelectAll()跟踪代码的所有行;
现在我想在gridview中为选定的行着色例如蓝色。
我该怎么办?
我选择在GridView控件如何在C#的颜色选择在GridView的行
gridView1.SelectAll()跟踪代码的所有行;
现在我想在gridview中为选定的行着色例如蓝色。
我该怎么办?
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
row.DefaultCellStyle.BackColor = Color.Blue;
}
可以使用SelectedRowStyle财产
试试这个,
To change selected rows
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.SelectedRows)
{
if (null != item)
{
item.DefaultCellStyle.BackColor = Color.Blue;
}
}
}
To change all rows
foreach (DataGridViewRow item in dataGridView1.Rows)
{
}
我相信你在说的是DevExpress XtraGrid。如果是这样,那么有多种方法可以突出显示DevExpress XtraGrid中的特定行,具体取决于您的任务。 例如,要突出显示任何选择的行你可以使用下面的代码:
gridView1.Appearance.SelectedRow.BackColor = Color.Red;
为了强调通过使用自定义条件的特定行,你可以使用GridView.RowStyle事件:
void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
if((e.State & DevExpress.XtraGrid.Views.Base.GridRowCellState.Selected) != 0) {
// check some conditions
e.HighPriority = true;
e.Appearance.BackColor = Color.Blue;
}
}
请阅读更多以下帮助文章中的所有这些方法:Customizing Appearances of Individual Rows and Cells
对于gridview,请为事件CellFormatting设置以下方法
private void StocksDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
StocksDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.OrangeRed;
}
我使用的DevExpress GridView的 – Vaseph
哎呀。你没有提到。 :) – Muctadir