在datagridview列中显示数据的问题

时间:2014-02-11 15:52:56

标签: c# sql wpf date datagrid

我在Windows窗体应用程序中使用datagrid视图。我有几个显示日期日期列的数据网格。当我显示我的数据时,日期显示正确,但始终后跟'00:00:00'。如何将其更改为仅将日期显示为“dd / mm'yyyy”。

我按如下方式构建我的数据网格:

     //Populate customers datagrid view
    private void displayInGrid_Customers(string sqlcmd)
    {
        customersDataGridView.Rows.Clear();

        connect.Open();

        command.Connection = connect;
        command.CommandText = sqlcmd;

        reader = command.ExecuteReader();


        while (reader.Read())
        {
            // add a row ( get index )
            int arow = customersDataGridView.Rows.Add();

            // datagridname.row[index].cells.value = reader[table].tostring()
            customersDataGridView.Rows[arow].Cells[0].Value = reader["Customer_ID"].ToString();
            customersDataGridView.Rows[arow].Cells[1].Value = reader["Forename"].ToString();
            customersDataGridView.Rows[arow].Cells[2].Value = reader["Surname"].ToString();
            customersDataGridView.Rows[arow].Cells[3].Value = reader["Address"].ToString();
            customersDataGridView.Rows[arow].Cells[4].Value = reader["Town"].ToString();
            customersDataGridView.Rows[arow].Cells[5].Value = reader["Postcode"].ToString();
            customersDataGridView.Rows[arow].Cells[6].Value = reader["Date_Of_Birth"].ToString();
            customersDataGridView.Rows[arow].Cells[7].Value = reader["Phone_Number"].ToString();
            customersDataGridView.Rows[arow].Cells[8].Value = reader["Email"].ToString();
            customersDataGridView.Rows[arow].Cells[9].Value = reader["Current_Rental"].ToString(); 
            customersDataGridView.Sort(Surname, ListSortDirection.Ascending);
        }
        reader.Close();
        connect.Close();
    }

    //Display all customers button
    private void button_view_all_customers_Click(object sender, EventArgs e)
    {
        command.CommandText = "SELECT CUSTOMERS.Customer_ID, CUSTOMERS.Forename, CUSTOMERS.Surname, CUSTOMERS.Address, "
        + "CUSTOMERS.Town, CUSTOMERS.Postcode, CUSTOMERS.Date_Of_Birth, CUSTOMERS.Phone_Number, CUSTOMERS.Email, CUSTOMERS.Current_Rental "
        + "from CUSTOMERS LEFT JOIN STOCK ON CUSTOMERS.Current_Rental = STOCK.Product_ID";
        string cmd = command.CommandText;
        displayInGrid_Customers(cmd);

另外,我在另一个数据网格中有另一个问题。这个有一个支付列,当我最初在访问时创建表时,列中的数据就像'£4.99'并且按预期右对齐但是当我显示它时,没有'£'符号并且它是左对齐的

该数据网格的代码是:

    //Populate payments datagrid view
    private void displayInGrid_Payments(string sqlcmd)
    {
        paymentsDataGridView.Rows.Clear();

        connect.Open();

        command.Connection = connect;
        command.CommandText = sqlcmd;

        reader = command.ExecuteReader();

        while (reader.Read())
        {
            // add a row ( get index )
            int arow = paymentsDataGridView.Rows.Add();

            paymentsDataGridView.Rows[arow].Cells[0].Value = reader["Customer_ID"].ToString();
            paymentsDataGridView.Rows[arow].Cells[1].Value = reader["Payment"].ToString();
            paymentsDataGridView.Rows[arow].Cells[2].Value = reader["Payment_Date"].ToString();

        }
        reader.Close();
        connect.Close();
    }

    //Display all payments
      private void button_display_payments_Click(object sender, EventArgs e)
    {
        command.CommandText = "SELECT PAYMENTS.Customer_ID, PAYMENTS.Payment, PAYMENTS.Payment_Date "
        + "from PAYMENTS LEFT JOIN CUSTOMERS ON PAYMENTS.Customer_ID = CUSTOMERS.Customer_ID";
        string cmd = command.CommandText;
        displayInGrid_Payments(cmd);
    }

1 个答案:

答案 0 :(得分:1)

第一部分尝试:

customersDataGridView.Rows[arow].Cells[6].Value = ((DateTime)reader["Date_Of_Birth"]).ToShortDateString()

第二部分尝试:

paymentsDataGridView.Rows [arow] .Cells [1] .Value = reader [" Payment"]。ToString(" C");

paymentsDataGridView.Rows[arow].Cells[1].Value = String.Format("{0:C}", decimal.Parse(reader["Payment"].ToString()));

如果它仍然给出错误,请尝试将decimal更改为double,尽管对于金钱和货币我会避免加倍,因为它可能会给出舍入错误。