Skip to main content

Microsoft Silverlight

Answered Question DataGrid refresh, update observable collectionRSS Feed

(0)

snegha
snegha

Member

Member

4 points

19 Posts

DataGrid refresh, update observable collection

 Hi,

i've a datagrid and binded by this way

where colMgmt is observable collection.

dgListMgmtRecords.ItemsSource = colMgmt;

 now i've to update a particular field from status=0 to status=2  on colMgmt on button click event(occurs in the datagrid)

 on this event : private void btnStopProcessing_Click(object sender, RoutedEventArgs e

  and this has to be refreshed in the datagrid too.

how can i do that?

how do i update colMgmt? and how do i refresh the datagrid?

Thanks in advance

xusun
xusun

Participant

Participant

991 points

165 Posts

Microsoft

Re: DataGrid refresh, update observable collection

You Item inside the ObservableCollection needs implement INotifyPropertyChanged, such that when any property got changed, the datagrid will be notified to update.

Then you can simply modify the data in the colMgmt collection, such as

colMgmt[0].Status=2

Thanks,
Xun Sun - MSFT

snegha
snegha

Member

Member

4 points

19 Posts

Re: Re: DataGrid refresh, update observable collection

 Hi even i tried on that way but its not working.

im posting my code. pls have a look on it and help me out

this is my button event placed in the grid

 

ObservableCollection<XData.Items.XQueue> colXQueue;
     


        

 private void btnStopProcessing_Click(object sender, RoutedEventArgs e)
        {
            dgListXRecords.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;

            Button btnPressed = sender as Button;
            if (btnPressed == null)
                throw new Exception("sender expected to be a button but was" + sender.GetType());
            string QueueID = btnPressed.Tag.ToString();
            //MessageBox.Show(QueueID.ToString());
            StringBuilder xmlString = new StringBuilder();
            xmlString.Append("<functions>");
            xmlString.AppendFormat("<function name=\"queueupdatestatus\" QueueID=\"{0}\" />", QueueID);
            xmlString.Append("</functions>");
            Scripting.Instance.ExecuteScript(xmlString, new EventHandler<ExecuteCompletedEventArgs>(oneScriptService_UpdateXQueueCompleted));

            var x = colXQueue.Where((XData.Items.XQueue bq) => bq.queid.ToString() == QueueID);
                 
            if (x.First().queStatus == "0")
            {
                x.First().queStatus = "5";
                foreach (XData.Items.XQueue item in x)
                {
                    item.queStatus = x.First().queStatus;
                }
            }
            else
            {
                x.First().queStatus = "0";
                foreach (XData.Items.XQueue item in x)
                {
                    item.queStatus = x.First().queStatus;

                }
             }
            dgListXRecords.ItemsSource = colXQueue;
        }

 

  private void oneScriptService_UpdateXQueueCompleted(object sender, ExecuteCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                //MessageBox.Show("The changes are being processed");
                dgListXRecords.ItemsSource = colXQueue;
            }
        }

 

and this is my Observable collection class along with notify property changed

public class XQueue : BaseId, ICreator<XQueue>,INotifyPropertyChanged
    {
        #region ICreator<XQueue> Members

        private string _questatus = string.Empty;

        public int queid { get; set; }
        public int touID { get; set; }
        public string touName { get; set; }
        public string queStatus
        {
            get { return _questatus; }
            set
            {
                if (_questatus != value)
                {
                    _questatus = value;
                    NotifyPropertyChanged("questatus");
                }
            }
        }
        public int queFailureCount { get; set; }
        public string queLastResult { get; set; }
        public string status { get; set; }


        public new void Fill(XElement x)
        {
            queid = int.Parse(x.Element("queid").Value);
            touID = int.Parse(x.Element("touID").Value);
            touName = x.Element("touName").Value;
            queStatus = x.Element("queStatus").Value;
            queFailureCount = int.Parse(x.Element("queFailureCount").Value);
            queLastResult = x.Element("queLastResult").Value;
            status = x.Element("status").Value;
        }

        public new BholdQueue Create(XElement x)
        {
            Fill(x);
            return this;
        }
        #endregion


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
       
        private void NotifyPropertyChanged(string property)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
    }
 

 

xaml code:

 <data:DataGrid x:Name="dgListXRecords"    VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
                                       SelectionMode="Extended" 
                                       HeadersVisibility="Column" 
                                       CanUserSortColumns="True"
                                       AutoGenerateColumns="false"     SelectionChanged="dgListXRecords_SelectionChanged"   RowDetailsVisibilityChanged="dgListXRecords_PreparingRowDetails"  RowDetailsVisibilityMode= "VisibleWhenSelected"  Grid.Row="0" Grid.Column="1"     >
           <data:DataGrid.RowDetailsTemplate  >
                <data:DataGrid.Columns>
                   
                <data:DataGridTextColumn Header="Description"     Width="300"   IsReadOnly="True" CanUserSort="True" Binding="{Binding touName}">
                </data:DataGridTextColumn>

                    <data:DataGridTextColumn Header="Status"      Width="300"    CanUserSort="True"  Binding="{Binding queStatus, Mode=TwoWay}">
                    </data:DataGridTextColumn>

                    <data:DataGridTemplateColumn>
                     
                    <data:DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
                                <Button x:Name="btnContinueProcessing"  Click="btnStopProcessing_Click" Tag="{Binding queid, Mode=TwoWay}"     >
                                    <Button.Content >
                                        <!--<Image Source="/styles/images/Continue.PNG"></Image>-->
                                            <Image  Name="imgContinue" Source="{Binding Path=queStatus, Mode=TwoWay, Converter={StaticResource StatusIconConverter} }"        ></Image>
                                        </Button.Content>
                                   
                                </Button>
                                </StackPanel>
                            </DataTemplate>
                     </data:DataGridTemplateColumn.CellTemplate>
                  </data:DataGridTemplateColumn>

                   
                    <data:DataGridTemplateColumn>
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="btnDeleteProcessing" Click="btnDeleteProcessing_Click" Tag="{Binding queid}">
                                    <Button.Content>
                                        <Image Source="/styles/images/Delete.PNG" ></Image>
                                    </Button.Content>
                                </Button>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                    </data:DataGridTemplateColumn>

                 </data:DataGrid.Columns>

        </data:DataGrid>    

xusun
xusun

Participant

Participant

991 points

165 Posts

Microsoft
Answered Question

Re: Re: DataGrid refresh, update observable collection

Did you directly copy/paste your code here?  Just before digging deap into the details, I noticed that there's typo in the queStatus setter, where it should be "queStatus" instead of "questatus".  (uppercase s).  Would that possible be the reason?

 

Thanks,
Xun Sun - MSFT

snegha
snegha

Member

Member

4 points

19 Posts

Re: Re: DataGrid refresh, update observable collection

 WOW..... thanks a lot....................... Yes

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities