简体   繁体   中英

System.Byte in datagrid column

I am using the following query:

string query = @"SELECT r.id, user_name, user_phone, date_create, REPLACE( date_payment,  '0000-00-00 00:00:00',  'Не оплачено' ) as 
                date_payment, payment_method, amount, rs.name_ru
                FROM request AS r, request_status AS rs
                WHERE r.status = rs.id";

And i am binding datatemplate in the following way:

DataTemplate>
    <TextBlock VerticalAlignment="Center" Text="{Binding date_payment}" Width="135" />
</DataTemplate>

Its throwing correct output except the " date_payment " Its output value comes " System.Byte[] ". please help!!!

Thank from MBen

class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output = "";
                output = System.Text.Encoding.UTF8.GetString(listOfBytes);
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }

date_payment is an Array, and you didn't provide any way for WPF for displaying it, so it calls ToString . You can provide a data converter for it.

Add a resource to your Window or page :

   <Window.Resources>
        <local:ByteArrayToString x:Key="ByteArrayConverter" />
    </Window.Resources>

Use it in your TextBlock as such :

<TextBlock VerticalAlignment="Center" Text="{Binding date_payement, Converter={StaticResource ByteArrayConverter}}" Width="135" />

Now you need to add a new class that does the conversion :

    class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output ="";
                output = listOfBytes.Aggregate(output, (current, elemt) => current + elemt.ToString());
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }

This thing happened to me to. It seems this is due to a bug in the connector. Try to cast the date column to CHAR. It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM