2012-12-21 26 views
5

मेरे पास एक डाटाग्रिड है जो एक SQL सर्वर डीबी से जुड़ी एक तालिका प्रदर्शित करता है। मैं प्रत्येक 60 सेकंड के लिए टाइमर सेट करना चाहता हूं, जो किसी भी अपडेट के लिए जांच करता है और फिर नवीनतम अद्यतन डेटा प्रदर्शित करता है।डब्ल्यूपीएफ डाटाग्रिड- ऑटो रीफ्रेश

अब तक मैं datagrid के लिए एक event_handler, उस वस्तु डिस्पैचर टाइमर

private void dataGrid1_loaded(object sender, RoutedEventArgs e) 
{ 
    DispatcherTimer dispatcherTimer = new DispatcherTimer(); 
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
    dispatcherTimer.Interval = new TimeSpan(0, 0, 60); 
    dispatcherTimer.Start(); 
} 

शामिल अब मैं ईवेंट हैंडलर डेटाबेस से नए-नए अपडेट डेटा को संभालने के लिए के साथ आगे बढ़ने के लिए पता नहीं कैसे बनाया है ।

dispatcherTimer_Tick 

यहां मेरा चयन कथन है जिसका उपयोग डेटाग्रिड को भरने के लिए किया जाता है।

private void Page_Loaded(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     String selectstatement = "select top 2 ItemID, ItemName,ConsumerName, Street, DOJ from ConsumarTB order by ItemID "; 
     da = new SqlDataAdapter(selectstatement, con); 
     ds = new DataSet(); 
     da.Fill(ds); 
     dataGrid1.ItemsSource = ds.Tables[0].DefaultView; 

    } 
    catch (SqlException e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 

उत्तर

6

आपके ऊपर जो कुछ भी है, उसे सुधारने के कई तरीके हैं। लेकिन यहां मैं शुरुआत करने वालों के लिए क्या प्रयास करूंगा।

नीचे पृष्ठ लोड पर आपके डेटाग्रिड को पॉप्युलेट करेगा, प्रत्येक 60 सेकंड टिक टिकाने के लिए टाइमर सेट करें। जब टाइमर टिकता है, तो यह फिर से ग्रिड में डेटा लोड करने के लिए एक विधि को कॉल करेगा। अभी शुरुआत के लिए

//On PageLoad, populate the grid, and set a timer to repeat ever 60 seconds 
private void Page_Loaded(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     RebindData(); 
     SetTimer(); 
    } 
    catch (SqlException e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 

//Refreshes grid data on timer tick 
protected void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    RebindData(); 
} 

//Get data and bind to the grid 
private void RebindData() 
{ 
    String selectstatement = "select top 2 ItemID, ItemName,ConsumerName, Street, DOJ from ConsumarTB order by ItemID "; 
    da = new SqlDataAdapter(selectstatement, con); 
    ds = new DataSet(); 
    da.Fill(ds); 
    dataGrid1.ItemsSource = ds.Tables[0].DefaultView; 
} 

//Set and start the timer 
private void SetTimer() 
{ 
    DispatcherTimer dispatcherTimer = new DispatcherTimer(); 
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
    dispatcherTimer.Interval = new TimeSpan(0, 0, 60); 
    dispatcherTimer.Start(); 
} 
+0

अच्छा चित्रण। धन्यवाद। मुझे कोशिश करने और आप पर वापस जाने दो। – user1221765

+1

INotifyPropertyChanged को लागू करके इसे प्राप्त करने का एक अच्छा विचार होगा? – user1221765

+1

यह 'अवलोकन योग्य चयन ' के साथ-साथ 'INotifyPropertyChanged' को देखने का एक शानदार अवसर होगा। यदि आप एक कस्टम क्लास बनाते हैं और अपने ग्रिड को 'ऑब्जर्जेबल कोलेक्शन ' पर बाध्य करते हैं, तो आपको अपने अपडेट पर पुनर्विचार करने की आवश्यकता नहीं है। मैं अत्यधिक सुझाव देता हूं कि आप इसे केवल wpf शैक्षणिक लाभ के लिए करें। :) – Khan

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^