NemeZiz
NemeZiz

Reputation: 27

C# DataGridView get selected Row / Column value

I am trying to get the Value of the selected Row and a specific Column. I already tryed some ideas from stackoverflow, but nothing worked yet. If i use this code:

string strid = "";
DataRowView rowview = DG1.SelectedItem as DataRowView;
strid = rowview.Row["Id"].ToString();
MessageBox.Show(strid);

I get this error:

An unhandled exception of type 'System.NullReferenceException' occurred in NLauncher.exe

Additional information:

Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

My XAML:

<DataGrid IsReadOnly="True" Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionChanged="DG1_SelectionChanged_1" SelectedItem="{Binding Path=SelectedDG1, Mode=TwoWay}" >
            <DataGrid.Columns>
                <mui:DataGridTextColumn x:Name="id" Header="#ID"  Binding="{Binding id}"/>
                <mui:DataGridTextColumn Header="Clientname" Binding="{Binding name}" />               
            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu >
                    <MenuItem Header="Menu 1" Click="MenuItem_Click" />
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>

My complete C# code: http://pastebin.com/WEHV2Z6e

All I want to get is the value of the column "ID" of the selected row.

Upvotes: 0

Views: 2975

Answers (3)

Doğuhan Yang&#246;z
Doğuhan Yang&#246;z

Reputation: 31

There is an easy way to solve this problem.

Instead of using;

DataRowView rowview = DG1.SelectedItem as DataRowView;

use;

dynamic rowView = DG1.SelectedItem;

and after;

strid = rowview.Id.ToString();

Upvotes: 0

Sosian
Sosian

Reputation: 622

You are probably getting an NullReferenceException because your cast

DataRowView rowview = DG1.SelectedItem as DataRowView;

is invalid and returning null. That means that whatever type DG1.SelectedItem is, it cannot be cast to the type "DataRowView".

For accessing the ID itself, Kylo Ren's answer is probably the best way to approach it.

Edit: Because rowView is null, you are getting a NullReferenceException when trying to access the Row:

strid = rowview.Row["Id"].ToString();

Upvotes: 0

Kylo Ren
Kylo Ren

Reputation: 8803

If your SelecetdItem is bound to Property like this:

private DataGridItem selectedDG1;

    public DataGridItem SelectedDG1
    {
        get { return selectedDG1; }
        set { selectedDG1 = value; 
            UpdateProperty("SelectedDG1"); 
            }
    }

For DataItem of DataGrid:

public class DataGridItem
{
    public string name { get; set; }

    public int id { get; set; }
}

Then ID can be get as SelectedDG1.id.

And code System.Data.DataRowView rowview = DG1.SelectedItem as System.Data.DataRowView; is wrong. This will only work when ItemSource is a Datatable. If ItemSource is a collection then:

       var selctedItem = DG1.SelectedItem as DataGridItem;
        if (selctedItem != null)
        {
            int value = selctedItem.id;
        }

Anyway you can suppress the exception by putting a Null check over the line: (Also your DataGridColumn is bound to 'id' and you are trying to retrive 'Id' that also can cause null exception)

if (rowview != null)
        strid = rowview.Row["Id"].ToString();

Upvotes: 1

Related Questions