Reputation: 233
I am created a listview
I need to display selected item in textBox
on clicking edit button my code shows following error.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in PresentationFramework.dll
Additional information: Specified argument was out of the range of valid values.
<ListView x:Name="lstvUnit" HorizontalAlignment="Left" Height="149" Margin="10,192,0,0" VerticalAlignment="Top" Width="321">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="50" DisplayMemberBinding="{Binding Path=Id}" />
<GridViewColumn Header="Unit" Width="150" DisplayMemberBinding="{Binding Path=name}"/>
<GridViewColumn Header="Edit" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnEdit" Content="Edit" Width="50" CommandParameter="{Binding}" HorizontalContentAlignment="Right" Style="{StaticResource NewImg}" Cursor="Pen" Foreground="Blue" Click="btnEdit_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The cs code is given below
DataRowView item = lstvUnit.Items.GetItemAt(lstvUnit.SelectedIndex) as DataRowView;
txtUnit_Name.Text = item[0].ToString();
Upvotes: 2
Views: 2855
Reputation: 233
I got above question solution.
if (lstvUnit.SelectedIndex >= 0)
{
DataRowView item = lstvUnit.Items.GetItemAt(lstvUnit.SelectedIndex) as DataRowView;
txtUnit_Id.Text = item[0].ToString();
txtUnit_Name.Text = item[1].ToString();
}
Upvotes: -1
Reputation: 943
You are getting exception as
'System.ArgumentOutOfRangeException' occurred in PresentationFramework.dll' exception as your selectedIndex is -1.
Set selectedIndex to some value say '1' in your Listview
in xaml
and then exception is not thrown.
And also the click event will be fired for button. do not think that when you click on 'Edit' button you have selected that particular ListviewItem
. The event is first registered with child and if you want that to be triggered with parent then you need to set e.Handled=false
on your event trigger.
One more issue is that you are casting the item to DataRowView, which is wrong. You need to cast it to your Collection class type and then use the item. It returns only one item so
item[0]
is wrong.
Upvotes: 1
Reputation: 17392
A few things.
Since you are using a DataRowView
, you cannot use an index value because it is not returning a collection. Also, you cannot simply convert the entire row to a string, as the row contains columns, etc.
You can format your own string, and access the values within each column of the row using the object's property name. Additionally, you should also have error checking.
if (lstvUnit.SelectedIndex >= 0)
{
var item = lstvUnit.Items.GetItemAt(lstvUnit.SelectedIndex) as DataRowView;
if (item != null)
{
xtUnit_Name.Text = string.Format("{0} {1}", item["Id"], item["name"];
}
}
Upvotes: 2
Reputation: 1550
This is simply saying that there is no index of 0 for item. Are you sure you don't just need item.ToString()? GetItemAt() sounds like it should only return a single item, not a collection of them. If you are trying to access the subitems of your item variable, try item.subitems[0]
Upvotes: 1
Reputation: 5797
Did you check if lstvUnit.SelectedIndex>=0
in GetItemAt
, so if there is any item selected at all.
Upvotes: 1