31/05/2006, 12:52
|
| Moderador | | Fecha de Ingreso: febrero-2002 Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 10 meses Puntos: 50 | |
Tema: Windows Forms Pregunta: Como poner el fondo de color de una columna determinada de mi datagrid Respuesta:
Voy a usar de ejemplo la base de datos NorthWind
Cuando llenas tu datagrid con un dataset debes asegurarte que tengas asociado un Mapping para usarlo como referencia a través del dataadaptar: c#
Código:
SqlConnection oConn = new qlConnection("....conexion");
SqlDataAdapter dAdap = new SqlDataAdapter("select * from customers",oConn);
dAdap.TableMappings.Add("Table","Customers"); //debe ser el nombre de tu tabla
DataSet ds = new DataSet();
dAdap .Fill(ds);
vb.net
Código:
Dim oConn As New SqlConnection("..conexion")
Dim dAdap As New SqlDataAdapter("select * from customers", oConn)
dAdap.TableMappings.Add("Table", "Customers") 'debe ser el nombre de tu tabla
Dim ds As New DataSet
dAdap.Fill(ds)
el segundo paso es crear una clase que herede de DataGridTextBoxColumn y sobreescribir el método Paint c#
Código:
public class ColoredTextBoxColumn : DataGridTextBoxColumn {
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum, Brush ForeColorBrush,
Brush BackColorBrush, bool AlignmentRight) {
object ObjVal;
ObjVal = this.GetColumnValueAtRow(source, rowNum);
if (ObjVal!=null) {
BackColorBrush = Brushes.Red;
ForeColorBrush = Brushes.Purple;
}
base.Paint(g, bounds, source, rowNum, BackColorBrush,
ForeColorBrush, AlignmentRight);
}
}
vb.net
Código:
Public Class ColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal bounds As Rectangle, ByVal source As _
CurrencyManager, ByVal rowNum As Integer, ByVal _
ForeColorBrush As Brush, ByVal BackColorBrush As Brush, _
ByVal AlignmentRight As Boolean)
Dim ObjVal As Object
ObjVal = Me.GetColumnValueAtRow(source, rowNum)
If Not (IsNothing(ObjVal) Or IsDBNull(ObjVal)) Then
BackColorBrush = Brushes.Red
ForeColorBrush = Brushes.Purple
End If
MyBase.Paint(g, bounds, source, rowNum, _
BackColorBrush, ForeColorBrush, AlignmentRight)
End Sub
End Class
finalmente le asociamos un DataGridTableStyle a nuestro grid ocupando nuestra clase ColoredTextBoxColumn. c#
Código:
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Customers";
ColoredTextBoxColumn col = new ColoredTextBoxColumn();
col.MappingName = "CompanyName";
tableStyle.GridColumnStyles.Add(col);
vb.net
Código:
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Customers"
Dim col As New ColoredTextBoxColumn
col.MappingName = "CompanyName"
tableStyle.GridColumnStyles.Add(col)
Donde col.MappingName = "CompanyName" es el nombre del campo (columna) a la que le queremos asignar el formato del color
El code completo sería:
c#
Código:
SqlConnection oConn = new SqlConnection("...conexion");
SqlDataAdapter dAdap = new SqlDataAdapter("select * from customers",oConn );
dAdap.TableMappings.Add("Table","Customers");
DataSet ds = new DataSet();
dAdap.Fill(ds);
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Customers";
ColoredTextBoxColumn col = new ColoredTextBoxColumn();
col.MappingName = "CompanyName";
tableStyle.GridColumnStyles.Add(col);
dataGrid1.DataSource = ds.Tables["Customers"];
dataGrid1.TableStyles.Add(tableStyle);
vb.net
Código:
Dim oConn As New SqlConnection("...conexion")
Dim dAdap As New SqlDataAdapter("select * from customers", oConn)
dAdap.TableMappings.Add("Table", "Customers") 'debe ser el nombre de tu tabla
Dim ds As New DataSet
dAdap.Fill(ds)
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Customers"
Dim col As New ColoredTextBoxColumn
col.MappingName = "CompanyName"
tableStyle.GridColumnStyles.Add(col)
DataGrid1.DataSource = ds
DataGrid1.TableStyles.Add(tableStyle)
referencias: http://msdn.microsoft.com/library/de...asicPrimer.asp http://www.akadia.com/services/dotne...able_cell.html |