【问题标题】:How do i edit the data in the dataTable before it is being displayed into the dataGridView?如何在将 dataTable 中的数据显示到 dataGridView 之前对其进行编辑?
【发布时间】:2014-10-01 03:04:52
【问题描述】:

我正在做一个 C# Windows 窗体应用程序。我想让你先了解一些事情。我正在使用本地访问数据库。我一直在尝试将月份从 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++;
            }

        }
    }

【问题讨论】:

    标签: c# winforms ms-access visual-studio-2012


    【解决方案1】:

    您可以尝试像这样更改您的选择查询。

    SELECT convert(char(3), Date, 0) AS [Month], SUM(Amount) AS [Amount] FROM Statements Where Customer_name ='" + companyName + "' group By MONTH(Date)"

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

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

    【讨论】:

    • 它向我展示了表达式中的未定义函数“转换”。是不是因为 Access 数据库有自己的一套 SQL 语句和表达式要遵循。感谢您的回复。
    • 我认为给我的第二个选项是添加一个额外的列
    猜你喜欢
    • 2014-05-30
    • 2018-03-19
    • 2012-05-25
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2016-06-11
    • 2018-12-30
    相关资源
    最近更新 更多