0

Below code is to get the first column data from a datagrid, and do some string operation, but error showed,

NullReferenceException was handled Object reference not set to an instance of an object.

What is the problem ? If I want to get each data in the first column, how to do it?

private string getit(DataGrid grid)
{
    StringBuilder stringStr = new StringBuilder();

    for (int i = 0; i < grid.Items.Count; i++)
    {
        TextBlock selectTextBlockInCell = grid.Columns[0].GetCellContent(i) as TextBlock;
        string inputName = selectTextBlockInCell.Text;

        stringStr.Append(@"\pic () at (-0.5,");
        stringStr.Append(3 - i);
        stringStr.Append(inputName);
        stringStr.Append(@"}");
    }

    return stringStr.ToString();
}
Haojie
  • 5,665
  • 1
  • 15
  • 14
dsfg
  • 29
  • 1
  • 5
  • This is not the way you work with WPF datagrid. Dont follow the windows form application approach – Sajeetharan Aug 22 '14 at 01:09
  • I do not know what is the way in WPF, how it works? – dsfg Aug 22 '14 at 01:33
  • Please see this [post][1] about get cell content of WPF DataGrid. [1]: http://stackoverflow.com/questions/13204536/getting-value-of-cell-in-datagrid-in-wpf – Haojie Aug 22 '14 at 01:37

1 Answers1

1

Read MSDN documentation about DataGridColumn.GetCellContent(), especially about what parameter you should pass to the method. Then you'll know that it doesn't receive row index, but "data item that is represented by the row that contains the intended cell".

Try to operate on underlying data source of the DataGrid instead, for example :

//cast to correct type
var data = (ObservableCollection<MyClass>)grid.ItemsSource;
StringBuilder stringStr = new StringBuilder();
//loop through your data instead of DataGrid it self
for (int i = 0; i < data.Count; i++)
{
    //get the value from correct property of your class model
    string inputName = data[i].MyProperty;
    //or if you really have to get it from cell content :
    //TextBlock selectTextBlockInCell = grid.Columns[0].GetCellContent(data[i]) as TextBlock;
    //string inputName = selectTextBlockInCell.Text;

    stringStr.Append(@"\pic () at (-0.5,");
    stringStr.Append(3 - i);
    stringStr.Append(inputName);
    stringStr.Append(@"}");
}
return stringStr.ToString();

WPF meant to be used with data-binding so that we can have clear separation between UI and data (read about MVVM pattern). Application logic shouldn't care about UI, so it better off operating on UI controls. Do logic operations over model/viewmodel instead, and let data binding pass the model/viewmodel to UI/view.

*) Getting data from data.ItemsSource is just simplified way, to start from what OP has at the moment. The ultimate way is to have a property that store the data and bind ItemsSource to that property.

har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks! But if I really need to get the data from datagrid cell, not through data binding source, how to do it? – dsfg Aug 22 '14 at 02:28
  • Why can't you get it from `grid.ItemsSource`? – har07 Aug 22 '14 at 02:38
  • Because I do not know where is the source, our code doesnt follow the binding rules... – dsfg Aug 22 '14 at 02:45
  • You don't have to know where is the source. If you meant you don't know the correct type of object stored in `grid.ItemsSource`, you can simply debug and use Visual Studio's "watch" window to see the actual type.. – har07 Aug 22 '14 at 02:52