c# - How do I get the row of a selected cell in a DataGrid? -
this question has answer here:
- get selected row item in datagrid wpf 9 answers
i have wpf application working on. using datagrid control display fields list various information. cannot find datgridview control in visual studio community toolbox. majority of google searches bring info datagridview, frustrating. use datagridview if find, digress. want able capture current row when user selects cell, can value adjacent cell. having no luck finding out how this. here how create datagrid:
private void displayfieldlengths(string strflfilename, int32 inttotalrowsize, int[] intfieldlengths) { int intdisplaycnt, colcnt = 0; string strdata = ""; lblflinfo.content = "file: " + strflfilename; datatable dt = new datatable(); dt.columns.add("field", typeof(string)); dt.columns.add("size", typeof(string)); dt.columns.add(" new size", typeof(string)); dt.columns[0].readonly = true; dt.columns[1].readonly = true; (intdisplaycnt = 0; intdisplaycnt < intfieldlengths.length; intdisplaycnt++) { strdata = "field" + (intdisplaycnt + 1) + "|" + intfieldlengths[intdisplaycnt].tostring() + "|1"; dt.rows.add(strdata.split('|')); } dtgrid.itemssource = dt.defaultview; lblrowsize.content = "total row length: " + inttotalrowsize; }
here xaml datagrid.
<datagrid isreadonly="false" name="dtgrid" loaded="gridloaded" currentcellchanged="newfieldsizechanged" horizontalalignment="left" verticalalignment="top" height="365" margin="48,53,0,0" width="272" borderthickness="1" borderbrush="black"> <datagrid.resources> <style targettype="{x:type datagridcell}"> <style.triggers> <trigger property="datagridcell.isselected" value="true"> <setter property="background" value="#ff9df3d6" /> <setter property="foreground" value="#000000" /> </trigger> </style.triggers> </style> </datagrid.resources> </datagrid>
it looks you're using events , code-behind implement want achieve. suggest embrace mvvm paradigm , implement data binding. code no longer in events , code-behind (ie not in view) in view model. example, track row changes bind selecteditem property property in view model. responding row changes becomes trivial putting code in view model property's setter. topic long put answer. best of luck.
Comments
Post a Comment