I would like to retrieve the value of whats stored in a currenlty selected row in my datagrid and store it in a string.

1. Is this possible?
2. If so how might I go about doing it

myDataGrid.Rows.Cell[0].etc.etc doesn't seem to work and it is all I could find on the internet

Dani AI

Generated

The Silverlight DataGrid does not expose a Rows collection like WinForms or ASP.NET grids do; it holds data in an ItemsSource and exposes the selected data object via SelectedItem / SelectedItems. That explains why saw "no Rows" and why calling ToString() on the selected item returned the class path (default Object.ToString). was on the right track about iterating selected items, but each selected item must be cast to the actual data type before reading a property.

// when the grid is bound to ObservableCollection<MyItem> or List<MyItem>
var model = myDataGrid.SelectedItem as MyItem;
if (model != null)
{
    string value = model.Name;   // read the property from the underlying object
}

If the compile-time type is unknown (anonymous types, different bindings), reflection can be used to fetch a named property:

var item = myDataGrid.SelectedItem;
if (item != null)
{
    var p = item.GetType().GetProperty("Name");
    var value = p != null ? p.GetValue(item, null)?.ToString() : null;
}

Additional notes and troubleshooting:

  • Overriding ToString() on the data class will make the simple ToString() call return a useful value, but direct property access is preferred.
  • Ensure SelectedIndex != -1 or SelectedItem != null before casting.
  • If the grid uses DataGridTemplateColumn (templates inside a cell), reading the bound object’s property is still the reliable approach; reading visual elements requires visual-tree traversal and is fragile.
  • If the environment is actually WinForms or ASP.NET rather than Silverlight, different APIs (Rows/Cells or GridView APIs) apply — confirm which DataGrid type is in use before applying code.

Recommended Answers

All 9 Replies

>Is this possible?

Yes. Use row index (int value) with myDataGrid.Rows[index].Cell[0].etc

I tried string

myString = myDataGrid.Rows[0].Cells[0].etc

and I get DataGrid has no method for Rows or something to that extent

Can I know which asp.net version are you using? Please post complete source code here.

Can I know which asp.net version are you using? Please post complete source code here.

string DataGridValue;
MainDataGrid.SelectedIndex = 0;
DataGridValue = ThisDataGrid.Rows[0].Cell[0].ToString();

ASP.NET Version# 3.0.50106.0

DataGridValue = ThisDataGrid.Rows[0].Cell[0].Text;
DataGridValue = ThisDataGrid.Rows[0].Cell[0].Text;

I get datagrid does not contain a definition for Rows

I think you should have to post source code.

See this code,

foreach (YourDataType item in ThisDataGrid.SelectedItems)
    {
          ....
    }

I think you should have to post source code.

See this code,

foreach (YourDataType item in ThisDataGrid.SelectedItems)
    {
          ....
    }

I would post the source code, but there is only so much I can post at work. Im a Mac guy most of the time so work is the only time I get access to c#

foreach (Object thisRow in MyDataGrid.SelectedItems)
                {
                    
                    StringTestTextBox.Text = thisRow.ToString();

                }
            }

returns the path to the class that stores the data

i.e MyProjectSolution.MyClass

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.