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

C#
DataRowView drv = (DataRowView)DataGrid.SelectedItem;


This is the full button code.

C#
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();

            MessageBox.Show(myTestColumnValue);
        }



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.
Posted

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

to this:
C#
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:
C#
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?

—SA
 
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