2016-07-27 107 views
0

我有一个Datagrid其中添加项目使用grid.Items.Insert(0, row); 我总是希望插入的最后一行先在网格上显示。 我也希望第一行与其他人有不同的颜色。为此我使用LoadingRow事件:将第一行数据网格设置为不同的颜色

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
dataGrid.RowBackground = new SolidColorBrush(Colors.AntiqueWhite);//set all rows to default color 
e.Row.Background = new SolidColorBrush(Colors.CornflowerBlue); //set current=>1st row row to BLue         
} 

但是添加的每一行是越来越有色CornflowerBlue没有先前添加的行改为AntiqueWhite。 我错过了明显的东西,真的很感激任何指导。

+0

可能是这个链接将帮助你出现着色问题。 http://stackoverflow.com/questions/23753299/set-background-color-only-for-the-first-row-in-a-datagrid –

回答

0

您需要遍历datagridview中的行,然后比较每行(示例列)上的第7列和第10列的值。

试试这个:

foreach (DataGridViewRow row in vendorsDataGridView.Rows) 
if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value)) 
{ 
    row.DefaultCellStyle.BackColor = Color.Red; 
} 
0

使用插入而不是添加的。这样您可以指定要添加新记录的位置。要在网格顶部添加新记录,只需使用插入(0)即可。 DataGridView1.Rows(0).Cells(n).Value =“abc”// - 这将会在顶部添加一条新记录,总是为 DataGridView1.Rows(0).Cells(n).Value =“abc”// - this will will add a top record,top 将该值设置为n列。

相关问题