You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中DataGridView无法作为方法使用的错误修复求助

解决C#中DataGridView无法像方法调用的错误(Non-invocable member 'Form1.DataGridView1' cannot be used like a method)

错误原因

你遇到的错误核心是误用圆括号()访问DataGridView单元格,在C#中,DataGridView的单元格必须通过Rows[rowIndex].Cells[columnIndex]的索引器语法(方括号[])访问,这是属性索引而非方法调用。

修复方案

1. 修正单元格访问语法

DataGridView的行、列索引均从0开始,你的startBook_C是日期的Day值(比如1号对应1),需要减1才能匹配单元格索引,同时替换圆括号为正确的索引器写法:

原错误代码:

dataGridView1(startBook_C, id - 1).Style.BackColor = Color.Blue;
// 循环内错误代码
dataGridView1(startBook_C + i, id - 1).Style.BackColor = Color.Red;

修改后:

// 行索引为id-1,列索引为startBook_C-1(Day从1开始,单元格索引从0开始)
dataGridView1.Rows[id - 1].Cells[startBook_C - 1].Style.BackColor = Color.Blue;

// 循环内修正后的写法
dataGridView1.Rows[id - 1].Cells[startBook_C + i - 1].Style.BackColor = Color.Red;

2. 修复循环逻辑问题

你原代码中的TotalDays -= 1会导致循环提前终止(比如TotalDays为5时,循环仅执行3次),直接删除该行即可,for循环的i++已能正确遍历所有天数。

3. 优化建议(可选)

  • 使用数据库字段名而非索引读取数据,避免字段顺序变化引发错误:
    int id = dr.GetInt32("id");
    DateTime startBook = dr.GetDateTime("startBook");
    DateTime endBook = dr.GetDateTime("endBook");
    
  • 使用using语句管理数据库连接/命令/阅读器,确保资源自动释放,避免连接泄漏:
    using (MySqlConnection Con = new MySqlConnection(ConString))
    {
        Con.Open();
        using (MySqlCommand cmd = new MySqlCommand(query, Con))
        {
            // 命令执行逻辑
            using (MySqlDataReader dr = cmd.ExecuteReader())
            {
                // 数据读取逻辑
            }
        }
    }
    

完整修改后的代码

private void ShowSchedule()//show color
{
    string ConString = "server=localhost;user=root;database=carrental;password=;";
    using (MySqlConnection Con = new MySqlConnection(ConString))
    {
        Con.Open();

        string query = "Select * from rent_schedule Where startBook >= @ST AND endBook <=@ET";

        DateTime startDate = new DateTime(monthCalendar1.SelectionStart.Year, monthCalendar1.SelectionStart.Month, 1);
        DateTime EndDate = startDate.AddMonths(1);

        using (MySqlCommand cmd = new MySqlCommand(query, Con))
        {
            cmd.Parameters.AddWithValue("@ST", startDate);
            cmd.Parameters.AddWithValue("@ET", EndDate);

            using (MySqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    int id = dr.GetInt32("id");
                    DateTime startBook = dr.GetDateTime("startBook");
                    DateTime endBook = dr.GetDateTime("endBook");

                    int startBook_C = startBook.Day;
                    int endBook_C = endBook.Day;

                    int TotalDays = endBook_C - startBook_C;

                    // 标记起始日期
                    dataGridView1.Rows[id - 1].Cells[startBook_C - 1].Style.BackColor = Color.Blue;

                    // 标记中间日期
                    for (int i = 1; i <= TotalDays; i++)
                    {
                        dataGridView1.Rows[id - 1].Cells[startBook_C + i - 1].Style.BackColor = Color.Red;
                    }
                }
            }
        }
    }
}

额外注意事项

  • 确保DataGridView的行数与车辆ID数量匹配,列数至少等于当前月份的天数,避免触发IndexOutOfRangeException。
  • 若业务允许跨月租车,需调整查询逻辑,当前查询仅筛选当月内的记录。

内容的提问来源于stack exchange,提问作者user24811813

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 01:11:09