C#中的数据网格访问

时间:2011-07-13 00:50:28

标签: c# .net winforms c#-4.0 c#-3.0

我是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 = Color.Red; count++; Console.WriteLine(count); }这个数量超过到期的书籍,那么'count'值会增加4倍,即使只有两本书在我的数据库中已经过期,因为这会阻止数据绑定2次访问它排序的时间我怎么才能获得超过正当账面价值的数字

2 个答案:

答案 0 :(得分:2)

实际上,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++;
        }
    }

答案 1 :(得分:2)

答案

private void UpdateDataGridViewColor()
    {
        if (calledMethod == 2)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                int j = 6;
                DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
                CellStyle.ForeColor = Color.Red;

                if (isLate(dataGridView1[j, i].Value.ToString()))
                {
                    dataGridView1[j, i].Style = CellStyle;
                }
            }
        }
    }