C# 在datagrid视图列中显示数据时出现问题
我正在windows窗体应用程序中使用datagrid视图。我有几个显示日期列的数据网格。当我显示数据时,日期显示正确,但后面总是跟“00:00:00”。如何将其更改为仅显示日期为'dd/mm'yyyy' 我按照如下方式构建数据网格:C# 在datagrid视图列中显示数据时出现问题,c#,sql,wpf,date,datagrid,C#,Sql,Wpf,Date,Datagrid,我正在windows窗体应用程序中使用datagrid视图。我有几个显示日期列的数据网格。当我显示数据时,日期显示正确,但后面总是跟“00:00:00”。如何将其更改为仅显示日期为'dd/mm'yyyy' 我按照如下方式构建数据网格: //Populate customers datagrid view private void displayInGrid_Customers(string sqlcmd) { customersDataGridView.
//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);
另外,我在另一个datagrid中遇到了另一个问题。这个有一个payment列,当我最初在access中创建表时,该列中的数据类似于“£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);
}
对于第一部分,请尝试:
customersDataGridView.Rows[arow].Cells[6].Value = ((DateTime)reader["Date_Of_Birth"]).ToShortDateString()
对于第二部分,请尝试:
customersDataGridView.Rows[arow].Cells[6].Value = ((DateTime)reader["Date_Of_Birth"]).ToShortDateString()
paymentsDataGridView.Rows[arow].Cells[1].Value=reader[“Payment”].ToString(“C”)
如果它仍然给出错误,请尝试将十进制改为双精度,尽管对于货币和货币,我会避免双精度,因为它可能会给出舍入错误。这对我的第一个问题非常有效,非常感谢!你好我得到的错误是“方法”ToString“没有重载”接受1个参数。我可能需要在((DateTime)这样的地方声明十进制吗?我在第一个示例中使用了“decimal where DateTime,但它不起作用。我的错,请立即检查。”(decimal)reader[“Current_Cost”]。ToString())这部分代码的另一个错误是((decimal)reader[“Current_Cost”]。)。错误表示无法将类型字符串转换为十进制?我感谢你的帮助!:)先生,您是位绅士!现在一切都很好!非常非常感谢。这个项目是为我的大学作业而设计的,我为此付出了很大的努力,但也遇到了一些困难。像你这样的人是上帝派来的。如果您对VS2010中的报表查看器工具感到满意,我还有一个问题吗?如果没有,不用担心,再次感谢!