locked
gridview cell RRS feed

  • Question

  • Hi

    how can i access to cells and row and column of gridview that is in ListView?

    or how can i get data of a cell in gridview in code

    this grid view is in ListView?

     


    Hossein Aftabi www.internationalsell.com
    Wednesday, November 3, 2010 8:42 AM

Answers

  • Hi H.Aftabi,

    Based on your description, I think your final goal is to manupilate the data in "gridview cell". If that's true, you should manupilate binding sourse that you bind to the ListView. Because the gridview/listview doesn't actaully host the data.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Thursday, November 4, 2010 9:44 AM
  • Hi H.Aftabi,

    You can accomplish this by your model class (or its wrapper) without manupilating on the UI controls.

    Please check out the following sample,

     public partial class Window2 : Window
     {
    
      public Window2()
      {
       InitializeComponent();
    
       DataGrid dg = new DataGrid();
       this.Content = dg;
       dg.ItemsSource = new ObservableCollection<Person>()
       {
        new Person(){Name="Mary", Wage=1000},
        new Person(){Name="Peter", Wage=800},
        new Person(){Name="Steven", Wage=1300}
       };
      }
    
    
     }
    
     public class Person : INotifyPropertyChanged
     {
      protected void OnNotifyPropertyChanged(string p)
      {
       if (PropertyChanged != null)
       {
        PropertyChanged(this, new PropertyChangedEventArgs(p));
       }
      }
      public event PropertyChangedEventHandler PropertyChanged;
    
      string _Name;
      public string Name
      {
       get { return _Name; }
       set
       {
        if (_Name != value)
        {
         _Name = value;
         OnNotifyPropertyChanged("Name");
        }
       }
      }
    
      decimal _Wage;
      public decimal Wage
      {
       get { return _Wage; }
       set
       {
        if (_Wage != value)
        {
         _Wage = value;
         _Salary = value * 4;
         OnNotifyPropertyChanged("Wage");
         OnNotifyPropertyChanged("Salary");
        }
       }
      }
    
      decimal _Salary;
      public decimal Salary
      {
       get { return _Salary; }
       set
       {
        if (_Salary != value)
        {
         _Salary = value;
         _Wage = value / 4;
         OnNotifyPropertyChanged("Salary");
         OnNotifyPropertyChanged("Wage");
        }
       }
      }
     }
    

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Tuesday, November 9, 2010 7:48 AM
  • Hi and thanks

    i do it fron this code

     DataGrid dgDemo = (DataGrid)sender;
    
          if (e.Column ==dgDemo.Columns[4])
          {
            TextBox ntes = (TextBox)e.EditingElement;
            string nt = ntes.Text;
          }
    

    Hossein Aftabi www.internationalsell.com
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Wednesday, November 10, 2010 10:07 AM

All replies

  • Hello

    Regarding to how to get the cell data, I think this is depends on the cell content, simple textblock or CellTemplate or binding.

    Anyway, please take a look about this.

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b069b9bc-2067-46a5-9fc5-d84ac8c0a929

    If it's possible you can give more info about your scenario.

     

    Hope this helps.


    Keep improving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)
    • Proposed as answer by Min Zhu Thursday, November 4, 2010 9:41 AM
    Thursday, November 4, 2010 5:08 AM
  • Hi H.Aftabi,

    Based on your description, I think your final goal is to manupilate the data in "gridview cell". If that's true, you should manupilate binding sourse that you bind to the ListView. Because the gridview/listview doesn't actaully host the data.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Thursday, November 4, 2010 9:44 AM
  • thanks for answer

    i write like this

    <ListView Name="listView2" VerticalAlignment="Stretch" Margin="10,10,234,16" 
               IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding Source={StaticResource DetailView}}" Style="{StaticResource ListViewStyle}" >
            <ListView.View>
              <GridView x:Name="GV2">
                <GridViewColumn Header="ID" Width="50">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <Label Content="{Binding Path=ID}" Width="50" Margin="-6,0,-6,0"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="DeptPay" Width="150">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <TextBox Text="{Binding Path=DebtPay}" Width="150" Margin="-6,0,-6,0"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Date" Width="100">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=PersianDate}" Width="150" Margin="-6,0,-6,0"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Status" Width="100">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <ComboBox Width="100" Margin="-6,0,-6,0" IsEditable="False"
                           IsSynchronizedWithCurrentItem="False"
                           ItemsSource="{Binding Source={StaticResource StatusLookup}}"
                           SelectedValue="{Binding Path=tblStatu}"
                           DisplayMemberPath="StatusText"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Wage" Width="100">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <TextBox Text="{Binding Path=Wage}" Width="150" Margin="-6,0,-6,0"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Description" Width="165">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                      <TextBox Text="{Binding Path=Description}" Width="215" Margin="-6,0,-6,0"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
              </GridView>
            </ListView.View>
          </ListView>

    ok

    now how i can get data for a cell?/

     


    Hossein Aftabi www.internationalsell.com
    Saturday, November 6, 2010 9:50 AM
  • Hi H.Aftabi,

    Look into the data source of the binding. For example, if you want to get the first cell of the second column, get it from the ID property of the second item in the resource "DetailView".

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, November 8, 2010 1:55 AM
  • <p>Thanks i changed listview to datagrid and do that but if i write like this</p>
    
    DataGrid dgDemo = (DataGrid)sender;
          if (e.Column ==dgDemo.Columns[4])
          {
            decimal mamberWage = ((tblDebtPay)DG2.SelectedItem).Wage;
            int memberId = ((tblDebtPay)DG2.SelectedItem).MemberID;
          }
    

    i want: when user insert text in to Column 4 change column5 to (column 4 * 20)

    but if i do with source return 0 how can i read text of column 4 when user insert text in to this column notfrom source;

    however i cheked this eventes

    {CellEditEnding & SelectedCellChanged & SelectionChanged}

     


    Hossein Aftabi www.internationalsell.com
    Tuesday, November 9, 2010 5:25 AM
  • Hi H.Aftabi,

    You can accomplish this by your model class (or its wrapper) without manupilating on the UI controls.

    Please check out the following sample,

     public partial class Window2 : Window
     {
    
      public Window2()
      {
       InitializeComponent();
    
       DataGrid dg = new DataGrid();
       this.Content = dg;
       dg.ItemsSource = new ObservableCollection<Person>()
       {
        new Person(){Name="Mary", Wage=1000},
        new Person(){Name="Peter", Wage=800},
        new Person(){Name="Steven", Wage=1300}
       };
      }
    
    
     }
    
     public class Person : INotifyPropertyChanged
     {
      protected void OnNotifyPropertyChanged(string p)
      {
       if (PropertyChanged != null)
       {
        PropertyChanged(this, new PropertyChangedEventArgs(p));
       }
      }
      public event PropertyChangedEventHandler PropertyChanged;
    
      string _Name;
      public string Name
      {
       get { return _Name; }
       set
       {
        if (_Name != value)
        {
         _Name = value;
         OnNotifyPropertyChanged("Name");
        }
       }
      }
    
      decimal _Wage;
      public decimal Wage
      {
       get { return _Wage; }
       set
       {
        if (_Wage != value)
        {
         _Wage = value;
         _Salary = value * 4;
         OnNotifyPropertyChanged("Wage");
         OnNotifyPropertyChanged("Salary");
        }
       }
      }
    
      decimal _Salary;
      public decimal Salary
      {
       get { return _Salary; }
       set
       {
        if (_Salary != value)
        {
         _Salary = value;
         _Wage = value / 4;
         OnNotifyPropertyChanged("Salary");
         OnNotifyPropertyChanged("Wage");
        }
       }
      }
     }
    

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Tuesday, November 9, 2010 7:48 AM
  • Hi and thanks

    i do it fron this code

     DataGrid dgDemo = (DataGrid)sender;
    
          if (e.Column ==dgDemo.Columns[4])
          {
            TextBox ntes = (TextBox)e.EditingElement;
            string nt = ntes.Text;
          }
    

    Hossein Aftabi www.internationalsell.com
    • Marked as answer by Min Zhu Monday, November 15, 2010 1:28 AM
    Wednesday, November 10, 2010 10:07 AM