我有一个显示表格的数据 GridView 。当我单击数据 GridView /表中的一个值时,它将显示在标签中。在表中,日期格式为 10/18/2015
。我有工作代码,但我想要日期标签中显示的格式将类似于 May 1, 2014
而不是 10/18/2015 12:00:00 AM
date
和return_date
列的数据类型是datetime
。
这是我的工作代码:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
lblfname.Text = row.Cells["First_Name"].Value.ToString();
lbllname.Text = row.Cells["Last_Name"].Value.ToString();
lblbook.Text = row.Cells["Title"].Value.ToString();
lbldate.Text = row.Cells["Return_Date"].Value.ToString();
lblbor.Text = row.Cells["Date"].Value.ToString();
else
{
//do something
}
}
编辑:这是我用表格过滤 datagridview 的代码
DataTable dt;
public void refreshdata()
{
string loadstring = @"server=localhost;database=librarys;userid=root;password=1234;";
MySqlConnection conDataBase = new MySqlConnection(loadstring);
MySqlCommand cmdDataBase = new MySqlCommand("CALL `proc_returnborrowed`();", conDataBase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
最佳答案
您需要将日期转换为 custom date format.
lblbor.Text = row.Cells["Date"].Value.ToString("MMMM dd, yyyy",
CultureInfo.CreateSpecificCulture("en-US"))
或者只是
lblbor.Text = row.Cells["Date"].Value.ToString("MMMM dd, yyyy")
编辑:
lblbor.Text = DateTime.ParseExact(row.Cells["Date"].Value, "MMMM dd, yyyy",
CultureInfo.InvariantCulture);
关于c# - 如何将数据类型为 datetime 的列的值显示为 2014 年 5 月 1 日的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016925/