【发布时间】: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