如何在dataTable中显示dataGridView之前编辑数据?

时间:2014-10-01 03:04:53

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

我正在做一个C#Windows窗体应用程序。我希望你先了解一些事情。我使用的是本地访问数据库。我一直在尝试将月份从AccessDB转换为MonthName。好吧,我尝试过使用DATENAME函数,但它无法正常工作。所以我想如果我可以在将dataTable放入datGridView for Display之前编辑它。请帮助我。请看一下编码。

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++;
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试更改此选择查询。

SELECT转换(char(3),Date,0)AS [月],SUM(金额)AS [金额] FROM客户名称=&#39;&#34; + companyName +&#34;&#39;按月(日期)&#34;

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

或者如果您不想更改查询,则实现逻辑,该逻辑将仅在dataTable中添加一个额外的列,如MonthText for 3 Letter Month。然后将这个新表与DataGridView绑定。