Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在dataTable中的数据显示到dataGridView中之前,如何编辑数据表中的数据?_C#_Winforms_Ms Access_Visual Studio 2012 - Fatal编程技术网

C# 在dataTable中的数据显示到dataGridView中之前,如何编辑数据表中的数据?

C# 在dataTable中的数据显示到dataGridView中之前,如何编辑数据表中的数据?,c#,winforms,ms-access,visual-studio-2012,C#,Winforms,Ms Access,Visual Studio 2012,我正在做一个C窗口窗体应用程序。我想让你先了解一些事情。我正在使用本地access数据库。我一直在尝试从AccessDB将月份转换为MonthName。我试过使用DATENAME函数,但它不起作用。所以我想如果我能在数据表被放入datGridView显示之前对其进行编辑。请帮帮我。请看一下编码 private void button3_Click(object sender, EventArgs e) // display amount havent cleared the month yet

我正在做一个C窗口窗体应用程序。我想让你先了解一些事情。我正在使用本地access数据库。我一直在尝试从AccessDB将月份转换为MonthName。我试过使用DATENAME函数,但它不起作用。所以我想如果我能在数据表被放入datGridView显示之前对其进行编辑。请帮帮我。请看一下编码

private void button3_Click(object sender, EventArgs e) // display amount havent cleared the month yet
    {
        sum.Visible = true;
        // 1
        // Open connection\
        string companyName = comboBox1.Text;
        string connectionString = null;
        OleDbConnection conn = null;
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DPE2.mdb;";
        conn = new OleDbConnection(connectionString);
        {
            conn.Open();
            // 
            // Create new DataAdapter
            using (OleDbDataAdapter a = new OleDbDataAdapter(
                "SELECT MONTH(Date) AS [Month], SUM(Amount) AS [Amount] FROM Statements Where Customer_name ='" + companyName + "' group By MONTH(Date)", conn))
            {
                // 3
                // Use DataAdapter to fill DataTable
                DataTable t = new DataTable();
                a.Fill(t);
                // 4
                // Render data onto the screen
                dataGridView3.DataSource = t; 
            }

            int z = 0;
            while (z < dataGridView3.Rows.Count)
            {
                string data = dataGridView3.Rows[z].Cells[0].ToString();

                switch (data)
                {
                    case "1":
                        dataGridView3.Rows[z].Cells[0].Equals("Jan");
                        break;
                    case "2":
                        dataGridView3.Rows[z].Cells[0].Value = "Feb";
                        break;
                    case "3":
                        dataGridView3.Rows[z].Cells[0].Value = "Mar";
                        break;
                    case "4":
                        dataGridView3.Rows[z].Cells[0].Value = "April";
                        break;
                    case "5":
                        dataGridView3.Rows[z].Cells[0].Value = "May";
                        break;
                    case "6":
                        dataGridView3.Rows[z].Cells[0].Value = "June";
                        break;
                    case "7":
                        dataGridView3.Rows[z].Cells[0].Value = "July";
                        break;
                    case "8":
                        dataGridView3.Rows[z].Cells[0].Value = "August";
                        break;
                    case "9":
                        dataGridView3.Rows[z].Cells[0].Value = "September";
                        break;
                    case "10":
                        dataGridView3.Rows[z].Cells[0].Value = "October";
                        break;
                    case "11":
                        dataGridView3.Rows[z].Cells[0].Value = "Novemeber";
                        break;
                    case "12":
                        dataGridView3.Rows[z].Cells[0].Value = "December";
                        break;
                }
                z++;
            }

        }
    }

您可以尝试更改您选择的查询,如下所示

从Customer_name='+companyName+'按MONTHDate分组的语句中,选择convertchar3,Date,0为[Month],SUMAmount为[Amount]

这将直接以3个字母表示月份,因此需要实现额外的逻辑来处理datagridView单元格文本,以便将月份设置为3个字母


或者,如果您不想更改您的查询,那么实现这样的逻辑,即只在dataTable中添加一个额外的列,如MonthText,为期3个月。然后将这个新表与DataGridView绑定。

它显示了表达式中未定义的函数“convert”。这是因为Access数据库有自己的SQL语句和表达式集要遵循。谢谢你的回复。我想这给我留下了第二个选择,那就是增加一列