日期:2014-05-20  浏览次数:20853 次

更改DataGrid行的颜色
根据行的字段值来更改DataGrid行的颜色,

------解决方案--------------------
給你兩段代碼,地址不記得了
在dataGrid 中如何设置行的颜色:
namespace DataGridRowHeaderText
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Form1 : System.Windows.Forms.Form
{
private DataGridRowHeaderText.MyDataGrid dataGrid1;
private Point pointInCell00;

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.dataGrid1 = new DataGridRowHeaderText.MyDataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.dataGrid1.DataMember = " ";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 24);
this.dataGrid1.Name = "dataGrid1 ";
this.dataGrid1.Size = new System.Drawing.Size(576, 248);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Paint += new System.Windows.Forms.PaintEventHandler(this.dataGrid1_Paint);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(624, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Name = "Form1 ";
this.Text = "Form1 ";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

[STAThread]
public static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
// Set the connection and sql strings
// assumes your mdb file is in your root
string connString = "Initial Catalog=Northwind;Data Source=.;user id=sa ;password=; ";
string sqlString = "SELECT * FROM orders ";

SqlDataAdapter dataAdapter = null;
DataSet _dataSet = null;

try
{
// Connection object
SqlConnection connection = new SqlConnection(connString);

// Create data adapter object
dataAdapter = new SqlDataAdapter(sqlString, connection);

// Create a dataset object and fill with data using data adapter 's Fill method
_dataSet = new DataSet();
dataAdapter.Fill(_dataSet, "orders ");
connection.Close();