开发者

WPF Datagrid not refreshing properly with MethodParameters

I'm having masses of trouble getting an ObjectDataProvider work with method parameters. Now with the following code, I can view the data in the XAML visual studio designer, however once I try to refresh the ObjectDataProvider my data disappears. I've setup an object data provider with five parameters, although these are not passed to the dataset query, I just want to see if I can pass these parameters first. I run the filter method at the bottom to refresh the OBJDataProvider and my datagrid goes blank?

My two ObjectDataProviders

<ObjectDataProvider x:Key="LogDataProvider" ObjectType="{x:Type local:LogDataProvider}"/>

<ObjectDataProvider x:Key="siteListContains" MethodName="GetLog2" ObjectType="{x:Type local:LogDataProvider}">
   <ObjectDataProvider.MethodParameters>
      <s:String>Red</s:String>
      <s:DateTime>12:14:24</s:DateTime>
      <s:DateTime>2008-01-01</s:DateTime>
      <s:DateTime>12:12:12</s:DateTime>
      <s:DateTime>2008-01-01</s:DateTime>
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

The datagrid and it's binding to the Object Data Provider

<dg:DataGrid Name="log_datagrid" Height="230" VerticalAlignment="Stretch" 
DataContext="{Binding Source={StaticResource siteListContains}}" 
ItemsSource="{Binding}" AutoGenerateColumns="True"/>

My LogDataProvider class

public class LogDataProvider
{
   private eventlogTableAdapter adapter;
   DataSet1 dataset;

   public LogDataProvider()
   {
      dataset = new DataSet1();
      adapter = new eventlogTableAdapter();
   }

   public DataView GetLog2(string sitestr, DateTime fromtimestr, DateTime fromdatestr, DateTime totimestr, DateTime todatestr)
   {
      adapter.Fill(dataset.eventlog);
      return dataset.eventlog.DefaultView;
   }
}

Here I've ran into problems, when I pass the sitestr to MethodParameter[0], it immediately jumps to the GetLog2 method of the LogDataProvider, without taking in the rest of the parameters... Then my datagrid goes blank :-(

private void filter_btn_Click(object sender, RoutedEventArgs e)
{
   ObjectDataProvider sitelist_dataobj = this.FindResource("siteListContains") as ObjectDataProvider;
   sitelist_dataobj.MethodP开发者_如何学Pythonarameters[0] = sitestr.Text.ToString();
   sitelist_dataobj.MethodParameters[1] = from_timePicker.SelectedTime;
   sitelist_dataobj.MethodParameters[2] = from_datepicker.SelectedDate;
   sitelist_dataobj.MethodParameters[3] = to_timePicker.SelectedTime;
   sitelist_dataobj.MethodParameters[4] = to_datepicker.SelectedDate;

   sitelist_dataobj.Refresh();
}


It should call Refresh every time you update any of the parameters. If you're going to set more than one, you should use DeferRefresh to make it wait until you're done:

private void filter_btn_Click(object sender, RoutedEventArgs e)
{
    ObjectDataProvider sitelist_dataobj = this.FindResource("siteListContains") as ObjectDataProvider;
    using (sitelist_dataobj.DeferRefresh())
    {
        sitelist_dataobj.MethodParameters[0] = sitestr.Text.ToString();
        sitelist_dataobj.MethodParameters[1] = from_timePicker.SelectedTime;
        sitelist_dataobj.MethodParameters[2] = from_datepicker.SelectedDate;
        sitelist_dataobj.MethodParameters[3] = to_timePicker.SelectedTime;
        sitelist_dataobj.MethodParameters[4] = to_datepicker.SelectedDate;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜