如何在Sqlite数据库中通过c#中cell的值来改变特定的数据网格单元格属性
本文关键字:改变 数据 数据网 属性 单元格 网格 数据库 Sqlite cell | 更新日期: 2023-09-27 18:01:56
我是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改变它的颜色。书或任何其他…
首选示例代码
您需要遍历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;
}
}
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
中自动生成,否则您可以手动添加