【问题标题】:Date format in richTextBox, when passing date values from dataGridView从 dataGridView 传递日期值时,richTextBox 中的日期格式
【发布时间】:2021-10-12 09:03:43
【问题描述】:

执行了一个简单的 SQL 查询,我的表单上的 dataGridView1 确实显示了我的数据库的日期和名称列中的值,正如预期的那样。 dataGridView1 中的日期以所需的格式显示(“MM/dd/yyyy”)。

接下来,将dataGridView1 的内容传递给richTextBox1。此处的日期值(在 RichTextBox1 中)确实以所需的格式显示(“MM/dd/yyyy”),但在所有情况下它们还包括 12:00:00 AM 的时间值(例如 2/6/2020 12 :00:00 AM)。

我想去掉richTextBox1 中日期值的这个时间分量。我知道我必须以某种方式声明richTextBox1 中列日期(dataGridView1 的第一列)的值的所需格式,但到目前为止还没有设法做到这一点。有什么建议吗?

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = SelectData();

            richTextBox1.Text = "REPORT \n\n";

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                richTextBox1.Text = richTextBox1.Text + "-";

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    richTextBox1.Text = richTextBox1.Text + "\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t";
                }
                richTextBox1.Text = richTextBox1.Text + "\n";
            }
        }
        
        private DataTable SelectData()
        {
            DataTable dtSelectData = new DataTable();

            using (MySqlConnection con = new MySqlConnection(connString))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT date, name FROM person", con))
                {
                    try
                    {
                        con.Open();
                        MySqlDataReader reader = cmd.ExecuteReader();
                        dtSelectData.Load(reader);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Database Message:\n" + ex.Message);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            return dtSelectData;
        }

【问题讨论】:

  • dataGridView1.Rows[i].Cells[j].Value 是您在 dataGridView1 中的 DateTime 值吗?
  • 每行的第一个单元格是 DateTime 值,所以我假设它是 dataGridView1.Rows[i].Cells[0].Value ?

标签: c# datagridview richtextbox


【解决方案1】:

要格式化 dataGridView1 单元格的日期,您可以将单元格的值 (object) 转换为 DateTime 类型,然后在其上使用 ToString("Some DateTime Format")

安全和简单的转换方法是结合检查,该单元格值是DateTime 类型,如果是,则将其转换为DateTime 类型。可以使用is 操作符来完成:

if (dataGridView1.Rows[i].Cells[j].Value is DateTime dt) { ... }

它调用模式匹配,您可以在MSDN 上了解它。 因此,如果条件为true(这意味着单元格值是DateTime 类型) - 您可以使用dt 变量值和ToString("SomeFormat") 来根据需要格式化值:

for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
    object cellValue = dataGridView1.Rows[i].Cells[j].Value;

    if (cellValue is DateTime dt)
        richTextBox1.Text += "\t" + dt.ToString("MM/dd/yyyy") + "\t";
    else
        richTextBox1.Text += "\t" + cellValue?.ToString() + "\t";
}

在测试中这种方式看起来不错:

【讨论】:

    【解决方案2】:

    替换

    dataGridView1.Rows[i].Cells[j].Value.ToString() 
    

    dataGridView1.Rows[i].Cells[j].Value.ToString("MM/dd/yyyy")
    

    为了防止时间。

    【讨论】:

    • dataGridView1.Rows[i].Cells[j].Valueobject 类型,其ToString() 方法有ho 重载以接受"MM/dd/yyyy" 格式作为参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多