gpt4 book ai didi

c# - C# 中的数据网格访问

转载 作者:行者123 更新时间:2023-11-30 15:43:37 25 4
gpt4 key购买 nike

我是 c#Database 链接的新手,这就是为什么无法通过搜索 stackoverflow 的旧帖子来获得它的原因

代码如下

private void issueDetails()
{
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.',studentId as 'Student ID', title as 'Title', author as 'Author', description as 'Description', issuedDate as 'Issued Date', dueDate as 'Due Date' FROM issuedBooks";
command.CommandText = query;
command.ExecuteNonQuery();

SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "issuedBooks");
dataGridView1.DataSource = ds.Tables["issuedBooks"];
dataGridView1.Sort(dataGridView1.Columns["Student ID"], ListSortDirection.Ascending);
dataGridView1.ReadOnly = true;
connection.Close();
}
}

我像上面那样使用来获取一些图书馆书籍的详细信息,例如 issuedDate 和 dueDate,现在我想突出显示超过 dueDate 的特定单元格。

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;
count++;
Console.WriteLine(count);
}

}
e.CellStyle.ForeColor = c;
}

public string toInd(string date)
{
DateTimeFormatInfo fmt = new CultureInfo("fr-fr").DateTimeFormat;
string dateString;
DateTimeOffset offsetDate, dateig;
string ret = DateTime.Now.ToShortDateString();
dateString = date;
bool ws = DateTimeOffset.TryParse(dateString, fmt, DateTimeStyles.None, out dateig);
if (ws)
{
offsetDate = DateTimeOffset.Parse(dateString, fmt);
ret = offsetDate.Date.ToShortDateString();
return ret;
}
return ret;
}

private bool isLate(string nowS)
{

DateTime dueDate = Convert.ToDateTime(toInd(nowS));
DateTime now;
string present = DateTime.Now.ToShortDateString();
now = Convert.ToDateTime(present);
//Console.WriteLine(toInd(nowS));
int a = dueDate.CompareTo(now);
if (a >= 0)
return false;
else return true;
}

但如果使用 if (isLate(Convert.ToString(e.Value)))
{
c = 颜色.红色;
计数++;
控制台.WriteLine(计数);
}
这对于逾期图书的数量,即使我的数据库中只有两本书逾期,'count' 值也会增加 4 次,因为如果在数据绑定(bind)时阻止访问 2 次,并且在排序时再次阻止访问两次怎么能我只得到超过到期账面值(value)的数字

最佳答案

实际上,dataGridView1_CellFormatting 会在每个单元格事件上触发,包括您是否排序,或者即使您将鼠标悬停在单元格上......在性能方面非常昂贵。

相反,您可以在 dgv 上使用 RowsAdded 事件,它只会在添加时触发一次:

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
if (isLate(dataGridView1[6, e.RowIndex].Value.ToString()))
{
dataGridView1[0, e.RowIndex].Style.ForeColor = Color.Red;
count++;
}
}

关于c# - C# 中的数据网格访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com