Click here to Skip to main content
15,666,947 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I get the error message:

An object reference is required for the non-static field, method, or property 'System.Windows.Controls.Primitives.Selector.SelectedItem.get

On the code below

DataRowView drv = (DataRowView)DataGrid.SelectedItem;

This is the full button code.

public void buyprintButton_Click(object sender, RoutedEventArgs e)
            DataGrid momentsDataGrid = new DataGrid();

            DataRowView drv = (DataRowView)DataGrid.SelectedItem;

            String myTestColumnValue = drv["ColumnName"].ToString();
         //   String myTestColumnValue = drv[0].ToString();


Could you please tell what is happening. Please dont tell me i have to put static or remove static from the name, as ive done that and none of them work.

Change this:
DataRowView drv = (DataRowView)DataGrid.SelectedItem;

to this:
DataRowView drv = (DataRowView)momentsDataGrid.SelectedItem;
Share this answer
Static members are used via the name of the declaring type. Non-static (instance members) are accessed via the instance.
For example:
string myString = "AAA ABABAB";
string onlyA = myString.Replace("B", "");

// but NOT
//string onlyA = string.Replace("B", ""); // why?
// because -- replace from what?! from what string? from what INSTANCE?!

Is it clear?

Share this answer

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