2010-02-15 65 views

Trả lời

11
dataGridView1.Columns[0].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss"; 

Để định dạng cụ thể, hãy xem các liên kết sau:

+0

Đây là cách chính xác để định dạng ngày giờ nếu bạn không cần bất kỳ trường hợp đặc biệt nào để hiển thị nội dung khác hoàn toàn. –

+0

@Justin: Tôi không chắc bạn đang nói gì. Bạn có thể cho tôi biết thêm thông tin không? –

+2

Công ty chúng tôi có trường hợp nếu DateTime là một giá trị nhất định, nó cần hiển thị dưới dạng một chuỗi rỗng. Để thực hiện điều đó, bạn sẽ sử dụng sự kiện CellFormatting trên DataGridView –

1

Một cách là để xử lý các CellFormattingEvent event và sau đó bạn có thể hiển thị các dữ liệu làm thế nào bạn muốn.

+0

cảm ơn liên kết – fishhead

1

Bạn có thể thử để chỉnh sửa RowDefaultCellStyle-> định dạng trong thiết kế và chọn định dạng chấp nhận được, hoặc sử dụng này:

DataGridViewCellStyle customStyle = new DataGridViewCellStyle(); 
customStyle.Format = "T"; 
customStyle.NullValue = null; 
customGridView.RowsDefaultCellStyle = customStyle; 
1

Override sự kiện OnRowDataBound và đọc thêm một chút về cultures and formatting in .Net

protected override void OnRowDataBound(GridViewRowEventArgs e) 
{ 

    base.OnRowDataBound(e); 


    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 

        TableCell cell = e.Row.Cells[i]; 
        cell.Text = DateTime.Parse (cell.Text, culture); 
     } 
    } 
    } 
8

Dưới đây là ví dụ về 3 cách khác nhau:

  • định dạng chuỗi trên mẫu di động
  • tế bào dạng sự kiện
  • kiểu tùy chỉnh chuyển đổi

Bất kỳ việc sử dụng nào?

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 
public class MyData 
{ 
    public DateTime A { get; set; } 
    public DateTime B { get; set; } 
    public DateTime C { get; set; } 
    [TypeConverter(typeof(CustomDateTimeConverter))] 
    public DateTime D { get; set; } 
} 
class CustomDateTimeConverter : DateTimeConverter 
{ 
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
    { 
     if (destinationType == typeof(string)) 
     { 
      return ((DateTime)value).ToString("dd-MM-yyyy HH:mm:ss"); 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 
} 
static class Program { 
    [STAThread] 
    static void Main() 
    { 
     DateTime when = DateTime.Now; 
     var data = new BindingList<MyData> 
     { 
      new MyData { A = when, B = when, C = when } 
     }; 
     using (var form = new Form()) 
     using (var grid = new DataGridView { 
      AutoGenerateColumns = false, 
      DataSource = data, Dock = DockStyle.Fill }) 
     { 
      form.Controls.Add(grid); 
      grid.Columns.Add(
       new DataGridViewTextBoxColumn { 
        HeaderText = "Vanilla", 
        AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells, 
        DataPropertyName = "A", 
       }); 
      grid.Columns.Add(
       new DataGridViewTextBoxColumn { 
        HeaderText = "Format", 
        AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells, 
        DataPropertyName = "B", 
        DefaultCellStyle = { Format = "dd/MM/yyyy HH:mm:ss" } 
       }); 
      grid.Columns.Add(
       new DataGridViewTextBoxColumn { 
        HeaderText = "CellFormatting", 
        AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells, 
        DataPropertyName = "C", 
       }); 
      grid.CellFormatting += (sender, args) => 
      { 
       if (args.Value != null && args.ColumnIndex == 2 
        && args.DesiredType == typeof(string)) 
       { 
        args.Value = ((DateTime)args.Value).ToString("dd MM yyyy HH:mm:ss"); 
       } 
      }; 
      grid.Columns.Add(
       new DataGridViewTextBoxColumn { 
        HeaderText = "TypeConverter", 
        AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells, 
        DataPropertyName = "D", 
       }); 
      Application.Run(form); 
     } 
    } 

} 
+1

+1 để có câu trả lời hoàn chỉnh nhất, chọn giải pháp đơn giản nhất phù hợp với nhu cầu của bạn. –

+0

Câu trả lời này khiến tôi gắn thẻ câu hỏi là yêu thích ۞ – awe

+0

cảm ơn câu trả lời hoàn chỉnh này – fishhead

Các vấn đề liên quan