我是c#
和Database
链接的新手,这就是为什么无法通过搜索stackoverflow的旧帖子来获取此信息
private void bookDetails()
{
string connectionPath = @"Data Source=Data\libraryData.dat;Version=3;New=False;Compress=True";
using (SQLiteConnection connection = new SQLiteConnection(connectionPath))
{
SQLiteCommand command = connection.CreateCommand();
connection.Open();
string query = "SELECT bookno as 'Book No.', bookCode as 'ISBN No.', title as 'Title', author as 'Author', publisher as 'Publishers', edition as 'Edition', storagehint as 'Storage Hint', description as 'Description' FROM booksDetails";
command.CommandText = query;
command.ExecuteNonQuery();
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "booksDetails");
int c = ds.Tables["booksDetails"].Rows.Count;
dataGridView1.DataSource = ds.Tables["booksDetails"];
dataGridView1.Sort(dataGridView1.Columns["ISBN No."], ListSortDirection.Ascending);
dataGridView1.ReadOnly = true;
connection.Close();
this.Totals.Text = "Total No. of Books Found : "+ Convert.ToString(c);
}
}
我用上面的方法来获取一些图书馆的书籍细节,现在我想突出显示一个特定的单元格,或者用no值改变它的颜色。书或任何其他..
示例代码首选
提前感谢...
答案 0 :(得分:3)
您需要遍历datagridview中的行,然后将颜色设置为适当的行。
假设颜色根据某些条件而变化,
private void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow myRow = dataGridView1.Rows[rowIndex];
if (myRow.Cells[colIndex].Value.ToString() == "High")
myRow.DefaultCellStyle.BackColor = Color.Red;
}
}
答案 1 :(得分:1)
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
Color c = Color.Black;
if (e.ColumnIndex == 6)
{
if (isLate(Convert.ToString(e.Value)))
c = Color.Red;
}
e.CellStyle.ForeColor = c; // or e.CellStyle.BackColor= c; whatever you can do like this
}
和事件代码是
this.dataGridView1.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView1_CellFormatting);
这将在yourform.designer.cs
中自动生成,或者您手动添加