How to set selected item of a DataGrid programmatically in WPF with MVVM application? -


i have bound datatable datagrid control. how can set selected item programmatically ?

example

in view model have property of type datatable bind datagrid

 private datatable sizequantitytable;   public datatable sizequantitytable  {                 {             return sizequantitytable;         }         set         {             sizequantitytable = value;             notifypropertychanged("sizequantitytable");         }   } 

my xaml

<datagrid              itemssource="{binding sizequantitytable}"             autogeneratecolumns="true"              margin="0,0,0,120" /> 

the constructor of view model (assigning dummy values)

this.sizequantitytable = new datatable();  datacolumn sizequantitycolumn = new datacolumn(); sizequantitycolumn.columnname = "size quantity"; this.sizequantitytable.columns.add(sizequantitycolumn);  datacolumn scolumn = new datacolumn(); scolumn.columnname = "s"; this.sizequantitytable.columns.add(scolumn);  datacolumn mcolumn = new datacolumn(); mcolumn.columnname = "m"; this.sizequantitytable.columns.add(mcolumn);  datarow row1 = this.sizequantitytable.newrow(); row1[sizequantitycolumn] = "blue"; row1[scolumn] = "12"; row1[mcolumn] = "15"; this.sizequantitytable.rows.add(row1);  datarow row2 = this.sizequantitytable.newrow(); row2[sizequantitycolumn] = "red"; row2[scolumn] = "18"; row2[mcolumn] = "21"; this.sizequantitytable.rows.add(row2);  datarow row3 = this.sizequantitytable.newrow(); row3[sizequantitycolumn] = "green"; row3[scolumn] = "24"; row3[mcolumn] = "27"; this.sizequantitytable.rows.add(row3); 

ok. have created 3 columns namely sizequantitycolumn, scolumn , mcolumn , added 3 rows namely row1, row2 , row2.

so, let's wanna set selected item row2 (so in view, second row should highlighted).

how can this?

edit

i hardcoded selectedindex of datagrid 1. (so second row should selected). in design time shows selected. not in run time. can see in below snapshot.

so ultimaltely problem not highlighting row.

enter image description here

there few way select items in datagrid. depends 1 works best situation

first , basic selectedindex select row @ index in datagrid

 <datagrid selectedindex="{binding selectedindex}" />  private int _selectedindex; public int selectedindex {     { return _selectedindex; }     set { _selectedindex = value; notifypropertychanged("selectedindex"); } }  selectedindex = 2; 

selecteditem select row matches row set

<datagrid selecteditem="{binding selectedrow}" />  private datarow _selectedrow; public datarow selectedrow {     { return _selectedrow; }     set { _selectedrow = value; notifypropertychanged("selectedrow");} }  selectedrow = items.first(x => x.whatever == something); 

the common 1 selectedvalue selectedvaluepath set, in case set column want select , can select row setting corresponding value

<datagrid selectedvaluepath="size quantity" selectedvalue="{binding selectionvalue}"   private string _selectedvalue public string selectionvalue  {     { return _selectedvalue; }     set { _selectedvalue = value; notifypropertychanged("selectionvalue"); } }  selectionvalue = "blue"; 

edit:

here test , highlighting fine

code:

public partial class mainwindow : window, inotifypropertychanged {     public mainwindow()     {         initializecomponent();          this.sizequantitytable = new datatable();         datacolumn sizequantitycolumn = new datacolumn();         sizequantitycolumn.columnname = "size quantity";         ...................         ........      }      private string _selectedvalue;     public string selectionvalue      {         { return _selectedvalue; }         set { _selectedvalue = value; notifypropertychanged("selectionvalue"); }     }      private int _selectedindex;     public int selectedindex     {         { return _selectedindex; }         set { _selectedindex = value; notifypropertychanged("selectedindex"); }     }      private datatable sizequantitytable;     public datatable sizequantitytable     {         { return sizequantitytable; }         set { sizequantitytable = value; notifypropertychanged("sizequantitytable"); }     }      private void button_click_1(object sender, routedeventargs e)     {         selectedindex = 2;     }      private void button_click_2(object sender, routedeventargs e)     {         selectionvalue = "blue";     }      private void notifypropertychanged(string p)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(p));         }     } } 

xaml:

<window x:class="wpfapplication21.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="202" width="232" name="ui">      <grid datacontext="{binding elementname=ui}">         <datagrid selectedvaluepath="size quantity"                           selectedvalue="{binding selectionvalue}"                    selectedindex="{binding selectedindex}"                   itemssource="{binding sizequantitytable}"                   autogeneratecolumns="true"                    margin="0,0,0,41" />         <stackpanel orientation="horizontal" height="37" verticalalignment="bottom" >             <button content="selectedindex" height="26"  width="107" click="button_click_1"/>             <button content="selectedvalue" height="26"  width="107" click="button_click_2"/>         </stackpanel>     </grid> </window> 

result:

enter image description here


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -