0
votes

I am really struggling in a situation from past 4 days and now pulling my hairs out. Please Help me out guys. here is the requirement :

I have a datagrid which currently bound with a datacollection and working perfectly. Now in one of the column I need to display a datetime values, but in such a way that in each 10th row the value must be in DateTime value while other rows means 0 to 9th row, need to display only time value.

The above scenario I have achieved using converter and it is working fine. Now here is the problem : "If I scroll down it is getting proper data but Whenever I scroll up it is calling the converter again and because of this every thing goes wrong (it shows DateTime values randomly instead of each 10th row)"

I have tried making converter null but then also it doesn't work.

The below code is just for reference (I have removed unnecessary columns) :

I have a datagrid as given below:

  <c1:C1DataGrid  DockPanel.Dock="Top" ScrollMode="Deferred" 
        VirtualizingStackPanel.VirtualizationMode="Standard" 
        VirtualizingStackPanel.IsVirtualizing="True" 
        Name="TabulerDataGrid" AlternatingRowBackground="#A4CEDCF6"
        MouseOverBrush="#9546E400" SelectedBackground="#9AA46D00"
        Visibility="Hidden" >
  </c1:C1DataGrid>

I am binding the data collection to the grid like below:

    private void BindDataGrid(object sender, EventArgs e)
    {
        foreach (DataColumn col in dataCollection.Columns)
            {
                if (col.ColumnName == "Time")
                {
                    C1.WPF.DataGrid.DataGridDateTimeColumn column = 
                        new C1.WPF.DataGrid.DataGridDateTimeColumn();

                    column.Format = GlobalDataDisplay.DateFormat;  //TTP 6321
                    column.FilterMemberPath = "OnlyDate";
                    DataGridContentFilter f = new DataGridContentFilter();
                    DataGridDateTimeFilter d = new DataGridDateTimeFilter();
                    d.EditMode = C1DateTimePickerEditMode.DateTime;
                    f.Content = d;
                    column.Filter = f;
                    column.CanUserFilter = true;
                    column.Header = col.ColumnName;

                   column.Binding = new System.Windows.Data.Binding {
                             Path = new PropertyPath(col.ColumnName), 
                        Converter = new ConvertDateChange()};

                    TabulerDataGrid.Columns.Add(column);
                }
          }   
            TabulerDataGrid.ItemsSource = dataCollection.DefaultView; 

    }

Here is my Converter class :

public class ConvertDateChange : IValueConverter
{
    int i = 0;
    public object Convert(object value, Type targettype, object parameter, System.Globalization.CultureInfo cluture)
    {

        if (i == 10)
        {
            i = 0;
        }
        else
        {
            i++;

        }
        string s = value.ToString();
        if (string.IsNullOrEmpty(s))
        {
            i = 0;

            return null;
        }
        else
        {
            string[] str = s.Split(' ');
            s = str[1];
            s = s.Substring(s.IndexOf(" ") + 1);
            if (i == 1)
            {
                return value;
            }
            else
            {
                return s;
            }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
1

1 Answers

0
votes

This is taking a bit of a shot, so if this doesn't help, I apologize. But having done this myself, I hope it does:

Instead of binding the datagrid directly to the data collection, and modifying the behavior of how that data is displayed that way...Try creating a Table View and using that as an interim.

Just set the source of the Table View to your data collection, then make the required modifications to the information in the table view, then bind the data grid to the table view.

Here is a bit of vb.net code, if that helps (I don't know C#, sorry):

        Dim listTable As DataView = New DataView(list) 'list is the data collection
        dgvList.DataSource = listTable   'dgvList is the data grid