2011-10-08 46 views

Respuesta

19

controlar el evento CellFormatting del DataGridView y aplicar un estilo de negrita a la fuente si la célula pertenece a una fila seleccionada:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    var dataGridView = sender as DataGridView; 
    if (dataGridView.Rows[e.RowIndex].Selected) 
    { 
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold); 
    // edit: to change the background color: 
    e.CellStyle.SelectionBackColor = Color.Coral; 
    } 
} 
+0

gracias por la ayuda! y cómo cambiar el BackColor de la fila? – Gali

+0

¿Es posible marcar con negrita solo algunas de las palabras en la celda, no en una celda completa? Necesito esta funcionalidad para mostrar los aspectos más destacados, pero no sé cómo implementar esto? ¿Es posible? – FrenkyB

+1

@FrenkyB: vea el artículo [RichTextBox Cell en un DataGridView] (http://www.codeproject.com/Articles/31823/RichTextBox-Cell-in-a-DataGridView) en Code Project, pero parece haber problemas de rendimiento ... Tal vez sea si tus datos son de solo lectura, podría ser aceptable. –

0

Intente controlar el evento SelectionChanged de dataGridView y configure cell style.

1

Después de cargar los contenidos en Datagrid, aplique estos manejadores de eventos a RowEnter y RowLeave.

private void dg_RowEnter(object sender, DataGridViewCellEventArgs e) 
{ 
    System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle(); 
    boldStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold); 
    dg.Rows[e.RowIndex].DefaultCellStyle = boldStyle; 
} 

private void dg_RowLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    System.Windows.Forms.DataGridViewCellStyle norStyle = new System.Windows.Forms.DataGridViewCellStyle(); 
    norStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular); 
    dg.Rows[e.RowIndex].DefaultCellStyle = norStyle; 
} 

Los códigos no están probados. Pero debería funcionar bien.

Espero que ayude.

0

El código de abajo hará que la fuente en el estilo Negrita para la fila seleccionada. "Total" es el último control de la fila en mi código

protected void gvRow_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    if (e.Row.Cells[rowIndex].Text == "Total") 
    { 
    e.Row.Font.Bold = true; 
    } 
} 
} 
Cuestiones relacionadas