Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save a column of data from a datagrid into a list
for example these data are already entered to a datagrid

ID | Name | Number
-----------------------
1 | David | 3012525
2 | Anna | 5825926
3 | Anna | 5878926


I want to save the numbers in a list so i can use them somewhere else?
Like
Number
3012525
5825926
5878926

Thanks

What I have tried:

I searched and apparently in winform you can do this :
C#
List<double>  Number= new List<double>();
       foreach (var row in dataGridView1.row)
       {
           Number.Add(Convert.ToDouble(row.Cells[2]));
       }</double></double>
Posted
Updated 11-May-16 20:39pm
v2

Yo can read the cell value using below method and (need to do further modification)

C#
var NumberList= new List<double>();

<pre lang="C#">foreach (var row in dataGridView1.row)
{
/// call the below method here with appropriate parameter
<pre lang="C#">NumberList.Add(Convert.ToDouble(value return from method));
}

&amp;lt;pre lang=&amp;quot;C#&amp;quot;&amp;gt;public string GetCellValue(DataGrid datagrid, int row, int column)
  {
      var cellInfo = new DataGridCellInfo(
          datagrid.Items[row], dataGrid.Columns[column]);

      DataGridCell cell = null;
      var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
      if (cellContent != null)
          cell = (DataGridCell)cellContent.Parent;

      if (cell == null) return string.Empty;

      // if DataGridTextColumn / DataGridComboBoxColumn is used
      // or AutoGeneratedColumns is True
      if (cell.Content is TextBlock)
          return ((TextBlock)cell.Content).Text;
      else if (cell.Content is ComboBox)
          return ((ComboBox)cell.Content).Text;

      // if DataGridTemplateColumn is used
      // assuming cells are either TextBox, TextBlock or ComboBox. Other Types could be handled the same way.
      else
      {
          var txtPresenter = FindVisualChild&amp;amp;lt;TextBox&amp;amp;gt;((ContentPresenter)cell.Content);
          if (txtPresenter != null) return txtPresenter.Text;
          var txbPresenter = FindVisualChild&amp;amp;lt;TextBlock&amp;amp;gt;((ContentPresenter)cell.Content);
          if (txbPresenter != null) return txbPresenter.Text;
          var cmbPresenter = FindVisualChild&amp;amp;lt;ComboBox&amp;amp;gt;((ContentPresenter)cell.Content);
          if (cmbPresenter != null) return cmbPresenter.Text;
      }
      return string.Empty;
  }

  public static T FindVisualChild&amp;amp;lt;T&amp;amp;gt;(DependencyObject obj) where T : DependencyObject
  {
      for (int i = 0; i &amp;amp;lt; VisualTreeHelper.GetChildrenCount(obj); i++)
      {
          DependencyObject child = VisualTreeHelper.GetChild(obj, i);
          if (child != null &amp;amp;amp;&amp;amp;amp; child is T)
              return (T)child;
          else
          {
              T childOfChild = FindVisualChild&amp;amp;lt;T&amp;amp;gt;(child);
              if (childOfChild != null)
                  return childOfChild;
          }
      }
      return null;
  }&amp;lt;/pre&amp;gt;&lt;/pre&gt;</pre>

.
 
Share this answer
 
Comments
Member 12481067 14-May-16 5:37am    
Thanks for replying to my question
I am wondering if this code is for WPF cos dataGridView1.row doesn't get picked in WPF
Cheers
Take a look at ObservableCollection, you can use this to bind.
Changes work both ways, and it's easy to get to the values, in the example MyList:
Code:
C#
ObservableCollection<MyClass> MyList= new ObservableCollection<MyClass>();

Form:
<ListBox ItemsSource="{Binding MyList}"></ListBox>

More information can be found here: How to: Create and Bind to an ObservableCollection[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900