简体   繁体   中英

Why i can't get the cell value of my Datagrid?

First of all i'm a programming newbie so please be indulgent.

I'm having trouble to get the values from a Datagrid. I've created one in MainWindow.XAML :

<DataGrid x:Name="LoadMapGrid"  HorizontalAlignment="Left" Height="264" Margin="0,0,-2,-12" VerticalAlignment="Top" Width="226" AlternatingRowBackground="#FF009999" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="xLoad" Width="100" Header ="Time,s" Binding="{Binding X_coord, Mode=TwoWay}"/>
        <DataGridTextColumn x:Name="yLoad" Width="100" Header ="Load, MW" Binding="{Binding Y_coord, Mode=TwoWay}"/>
    </DataGrid.Columns>
   </DataGrid>

And I'm trying to show the value of a cell in a messagebox when pressing a button :

private void SaveButton_Click(object sender, RoutedEventArgs e)
{              
    string str = xLoad.GetCellContent(0).ToString();

    ShowMessage(str);
}

I am getting the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in SiemensSP.exe Additional information: The object reference is not defined to an object instance.

If I get it right I should create an object using "new", which I don't understand because my object DataGridTextColumn is defined in my XAML file...

Once again I'm still not sure to understand how the OOP work so pls be forgiving :P.

Thanks

Baptiste

Ok I didn't manage to resolve the error, but at least I've found another way to do it. I'm posting the solution in case someone want to access data in a DataGrid and not in a DataGridView, which is way more complex.

To do so, i used this link : https://techiethings.blogspot.ch/2010/05/get-wpf-datagrid-row-and-cell.html

I've created a class, where i copied the methods shown in the link :

public static class Class1
{
    public static T GetVisualChild<T>(Visual parent) where T : Visual

    public static DataGridRow GetSelectedRow(this DataGrid grid)

    public static DataGridRow GetRow(this DataGrid grid, int index)

    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)

    public static DataGridCell GetCell(this DataGrid grid, int row, int column)
}

Then I used the following code in the Main :

private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.DataGridCell dataGridCell = null;

        dataGridCell = LoadMapGrid.GetCell(1,1);

        string A =dataGridCell.ToString(); //Return : System.Windows.Controls.DataGridCell: "MyValue"
        Char delimiter = ' ';
        string[] str = new string[1];
        str = A.Split(delimiter);


        System.Windows.MessageBox.Show(str[1]);

    }

As you can see I wasn't able to get "MyValue" only, so I had to cut the string. I know it's a bit barbaric but at least it's working. I'm open to any improvement proposal.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM