none
Converting Data Types in a IEnumerable<DataRow> or During Linq Query ?

    Question

  • Hello All,

    I'm having a issues displaying a Datatable column with the Type of DateTime Inside of a WPF DataGrid binding. I'm trying to do some conversion on the datetime object to a string. It doesnt want to play nice with me. Here below is my working Linq statement with my non working foreach statement attempting to parse the Type.

     

    var dataTable = CaseLogs.CaseLogsDataTable.LogData;
     IEnumerable<DataRow> list;
    
                try
                {
                       list  = (from r in dataTable.AsEnumerable()
                                where r[columnName].ToString() == queryValue
                                select  r);
    
                       foreach (DataRow dr in list)
                       {
                           for (int i = 0; i < 7; i++)
                           {
                               if (dr.ItemArray[i] is DateTime) 
                               {
                                  dr.ItemArray[i] = dr.ItemArray[i].ToString();
                               }
                           }
                       }
                        return list.CopyToDataTable<DataRow>();      
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: GenericQuery " + ex);
                }
                return null;
    
    Friday, September 30, 2011 8:44 PM

All replies

  • Hi rreeves84;

    You state, "I'm trying to do some conversion on the datetime object to a string. It doesnt want to play nice with me. ". Are you getting an error? If so what is the error?

    I suspect that the problem happens on this line in the code:

    dr.ItemArray[i] = dr.ItemArray[i].ToString();

    What is the data type of dr.ItemArray[i] when you try to over write it with a String data type?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, October 01, 2011 2:52 AM
  • I think problem is in

    dr.ItemArray[i] = dr.ItemArray[i].ToString();
    

    Here both are dr.ItemArray[i], so why u need to set it string?

    what is the datatype of dr.ItemArray[i]

    If it is date time then you can check with .ToString("dd/MM/yyyyy") or your date time format.

    Need more details about your problem.

     


    Hasibul Haque,MCC,MCPD blog.e-rains.com
    Saturday, October 01, 2011 3:14 AM
  • Hi rreeves84;

    You state, "I'm trying to do some conversion on the datetime object to a string. It doesnt want to play nice with me. ". Are you getting an error? If so what is the error?

    I suspect that the problem happens on this line in the code:

    dr.ItemArray[i] = dr.ItemArray[i].ToString();

    What is the data type of dr.ItemArray[i] when you try to over write it with a String data type?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    I wrote in the post that the column type is date time, I also indicated that I need to do this because the Date Time wont bind to the Data Grid natively. I tried your suggestion but I didn't have any luck.

    dr.ItemArray[i] = dr.ItemArray[i].ToString("yyyy-MM-dd hh:mm:ss");

    and

    dr.ItemArray[i] = String.Format("{0}", dr.ItemArray[i] );


     

    Also, I do not receive an error.


    • Edited by rreeves84 Monday, October 03, 2011 6:35 PM
    Monday, October 03, 2011 6:34 PM
  • Hi rreeves84;

    You state that the data type of the column is of DateTime but you are trying to write to that position a object of type string and you can't do that. To format the DateTime like you want first assign the data to the dataGridView.DataSource the value of the source data and then after doing that then tell the DataGridView how you want it formatted like the following.

    dataGridView.Columns[ "Entry Date/Time" ].DefaultCellStyle.Format = "yyyy-MM-dd hh:mm:ss";

    Then all should be well.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, October 03, 2011 7:17 PM
  • > I'm having a issues displaying a Datatable column with the Type of DateTime Inside of a WPF DataGrid binding. I'm trying to do some conversion on the datetime object to a string  [...] the Date Time wont bind to the Data Grid natively


    you can try using DateTime format pattern in StringFormat of the binding.
    below is an example.

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid x:Name="grd">     
            <DataGrid ItemsSource="{Binding row}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Date" Binding="{Binding date, StringFormat={}{0:dddd MMMM dd}}" />
                    <DataGridTextColumn Header="Id" Binding="{Binding id}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    
    using System;
    using System.Data;
    using System.Windows;
    
    namespace WpfApplication2
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                grd.DataContext = Load();
            }
    
            DataSet Load()   // for test purpose 
            {
                var ds = new DataSet();
                var tbl = ds.Tables.Add("row");
                tbl.Columns.Add("id", typeof(int));
                tbl.Columns.Add("date", typeof(DateTime));
                for(var i=0; i < 5; i++) 
                    tbl.Rows.Add(i, DateTime.Now.AddDays(-i));
                return ds;
            }
        }
    }
    



    reference DateTime format patterns and their behavior

     

    • Proposed as answer by Malobukv Tuesday, October 04, 2011 9:20 PM
    Tuesday, October 04, 2011 9:20 PM