【问题标题】:datagrid access in C#C# 中的数据网格访问
【发布时间】:2011-07-13 00:50:28
【问题描述】:

我是 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); } this 作为过期书籍的数量,“计数”值会增加 4 倍,即使我的数据库中只有两本书过期,因为如果在数据绑定时阻止访问 2 次,当再次阻止访问时再次它对我如何才能仅获得超过到期账面价值的数字进行排序

【问题讨论】:

  • 您是否尝试过在选择查询中按学生 ID 对列表进行排序?
  • 是的,我试过了,但还是同样的问题......
  • 即使我删除了排序语句,问题来了
  • 我在这里推测,但也许 dataGridView1.ReadOnly = true;触发单元格格式化事件。您是否尝试过将其注释掉或将其放在绑定到您的数据源之前?
  • @Aaron Ray 现在我删除了它,但还是同样的问题

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


【解决方案1】:

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

【讨论】:

    【解决方案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;
                    }
                }
            }
        }
    

    【讨论】:

    • 在表单加载和 gridview 排序时使用此代码以获得正确的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多