question

Bikramj-4545 avatar image
0 Votes"
Bikramj-4545 asked Opendata858c91-2256 commented

Datatable cell vale change not working

Hi ,

I want to change the date format for all the rows for a speciific column in a datatable but when i am assigning the changed value to it its not reflecting.

foreach (DataRow dr in dBTable.Rows)
{
dr[4] = Convert.ToDateTime(dr[4]).ToString("dd-MMM-yy");
}

Can anyone please help?

Thanks

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Bikramj-4545,
I made a simple test and your code worked fine.
In order to see the changes of DateTime in Datatable more intuitively, I display Datatable in dataGridView.

 DataTable table = new DataTable();
 private void Form1_Load(object sender, EventArgs e)
 {
     table.Columns.Add("Dosage", typeof(int));
     table.Columns.Add("Drug", typeof(string));
     table.Columns.Add("Diagnosis", typeof(string));
     table.Columns.Add("Date", typeof(DateTime));
     table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
     table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
     table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
     table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
     dataGridView1.DataSource = table;
 }
    
 private void button1_Click(object sender, EventArgs e)
 {
     foreach (DataRow dr in table.Rows)
     {
                  
         dr[3] = Convert.ToDateTime(dr[3]).ToString("dd-MMM-yy");
         Console.WriteLine(dr[3]);
     }
 }

The result:
113802-712.gif
So what is your DataTable? And please provide more code to reproduce the situation.
Best Regards,
Daniel Zhang


0 Votes 0 ·
712.gif (65.3 KiB)

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Hello,

A DataTable is for storing data, not for changing the format in the case of a DateTime, no different from storing a DateTime in a database table.

If displaying in say a DataGridView, set the format.

113769-f1.png

Code

 public Form1()
 {
     InitializeComponent();
        
     Shown += OnShown;
     dataGridView1.AutoGenerateColumns = false;
 }
    
 private void OnShown(object sender, EventArgs e)
 {
     var table = new DataTable();
    
     table.Columns.Add(new DataColumn("SomeDate", typeof(DateTime)));
    
     table.Rows.Add(new DateTime(2021,1,1));
     table.Rows.Add(new DateTime(2021,2,1));
     table.Rows.Add(new DateTime(2021,3,1));
    
     dataGridView1.DataSource = table;
 }

Then we can do the following

 private void button1_Click(object sender, EventArgs e)
 {
     var table = (DataTable) dataGridView1.DataSource;
    
     StringBuilder sb = new StringBuilder();
     foreach (DataRow row in table.Rows)
     {
         sb.AppendLine(row.Field<DateTime>("SomeDate")
             .ToString("dd-MMM-yy"));
     }
    
     MessageBox.Show(sb.ToString());
 }


Or

 var datesFormatted = ((DataTable) dataGridView1.DataSource)
     .AsEnumerable()
     .Select(row => row.Field<DateTime>("SomeDate").ToString("dd-MMM-yy"))
     .ToArray();
    
 MessageBox.Show(string.Join("\n",datesFormatted));

113893-f2.png




f1.png (23.4 KiB)
f2.png (8.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.