c# - Getting selected Item in DataGrid in WPF -
i have datagrid , textboxes. when select row in datagrid want textboxes filled related data of datagrid column. here picture.,
i have written code in mainwindow.xaml.cs :
private void personelentitydatagrid_loaded(object sender, routedeventargs e) { personelentity pers = (personelentity)personelentitydatagrid.selecteditem; namebox.text = pers.name; // exception here agebox.text = pers.age.tostring(); phonenumberbox.text = pers.phonenumber; addresbox.text = pers.address; }
when run code null reference exception in line wrote. here exception:
{"object reference not set instance of object."}
i guess object pers null. can tell me how make work?
here xaml file :
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:personel" mc:ignorable="d" x:class="personel.mainwindow" title="mainwindow" height="500" width="725" loaded="window_loaded"> <window.resources> <collectionviewsource x:key="personelentityviewsource" d:designsource="{d:designinstance {x:type local:personelentity}, createlist=true}"/> </window.resources> <grid datacontext="{staticresource personelentityviewsource}" > <label content="personnel " horizontalalignment="left" margin="55,47,0,0" verticalalignment="top"/> <button content="delete" horizontalalignment="left" margin="344,47,0,0" verticalalignment="top" width="75" click="delete_button_click"/> <button content="add" horizontalalignment="left" margin="477,47,0,0" verticalalignment="top" width="75" click="add_button_click"/> <button content="update" horizontalalignment="left" margin="624,47,0,0" verticalalignment="top" width="75" click="update_button_click"/> <label content="name" horizontalalignment="left" margin="55,91,0,0" verticalalignment="top"/> <label content="age" horizontalalignment="left" margin="55,132,0,0" verticalalignment="top"/> <label content="phone number" horizontalalignment="left" margin="55,178,0,0" verticalalignment="top"/> <label content="address" horizontalalignment="left" margin="55,218,0,0" verticalalignment="top"/> <textbox horizontalalignment="left" height="23" margin="309,95,0,0" textwrapping="wrap" text="" verticalalignment="top" width="367" name="namebox"/> <textbox horizontalalignment="left" height="23" margin="309,136,0,0" textwrapping="wrap" text="" verticalalignment="top" width="57" name="agebox"/> <textbox horizontalalignment="left" height="23" margin="309,178,0,0" textwrapping="wrap" text="" verticalalignment="top" width="120" name="phonenumberbox"/> <textbox horizontalalignment="left" height="23" margin="309,222,0,0" textwrapping="wrap" text="" verticalalignment="top" width="367" name="addresbox"/> <datagrid x:name="personelentitydatagrid" autogeneratecolumns="false" selectionchanged="personelentitydatagrid_selectionchanged" enablerowvirtualization="true" itemssource="{binding path=.}" margin="10,291,0,-22" rowdetailsvisibilitymode="visiblewhenselected" loaded="personelentitydatagrid_loaded"> <datagrid.columns> <datagridtextcolumn x:name="addresscolumn" binding="{binding address}" header="address" width="sizetoheader"/> <datagridtextcolumn x:name="agecolumn" binding="{binding age}" header="age" width="sizetoheader"/> <datagridtextcolumn x:name="idcolumn" binding="{binding id}" header="id" width="sizetoheader"/> <datagridtextcolumn x:name="namecolumn" binding="{binding name}" header="name" width="sizetoheader"/> <datagridtextcolumn x:name="phonenumbercolumn" binding="{binding phonenumber}" header="phone number" width="sizetoheader"/> </datagrid.columns> </datagrid> </grid> </window>
don't check in loaded event, instead check in selectionchanged event, add null check pers object
private void personelentitydatagrid_selectionchanged(object sender, routedeventargs e) { personelentity pers = (personelentity)personelentitydatagrid.selecteditem; if(pers != null) { namebox.text = pers.name; // exception here agebox.text = pers.age.tostring(); phonenumberbox.text = pers.phonenumber; addresbox.text = pers.address; } }
Comments
Post a Comment