Monday, 15 February 2010

c# - SelectedItem of DataGrid -



c# - SelectedItem of DataGrid -

i developing user interface host monitoring application, beingness monitored on database level. have displayed 2 datagrids on ui populate on run time.these 2 datagrids connected hostid ( hostid foreign key in logdatagrid).

the first datagrid displays list of host status(either running or stopped). display log status of respective hostid when user wants know status in detail. how accomplish when user selects host id in hostdatagrid ? have added xaml , screenshot of ui.

xaml

<datagrid datacontext="{binding path=hostdata,notifyontargetupdated=true,mode=oneway}" autogeneratecolumns="false" name="hostdatagrid" margin="171,32,235,230"> <datagrid.columns> <datagridtextcolumn header="host" width="auto" binding="{binding hostid}" /> <datagridtextcolumn header="status" width="auto" binding="{binding hoststatus}"/> </datagrid.columns> </datagrid> <datagrid datacontext="{binding path=logdata,notifyontargetupdated=true,mode=oneway}" autogeneratecolumns="false" name="logdatagrid" margin="103,108,102,145"> <datagrid.columns> <datagridtextcolumn header="host id" width="auto" binding="{binding hostid}" /> <datagridtextcolumn header="logs" width="auto" binding="{binding logid}" /> <datagridtextcolumn header="log path" width="auto" binding="{binding logpath}"/> <datagridtextcolumn header="date" width="auto" binding="{binding date}"/> <datagridtextcolumn header="last activity" width="auto" binding="{binding lastactivity}"/> </datagrid.columns>

code behind logfile model:

public logfilemodel() { } private int _hostid; public int hostid { { homecoming _hostid; } set { _hostid= value; onpropertychanged("hostid"); } } private string _logid; public string logid { { homecoming _logid; } set { _logid= value; onpropertychanged("logid"); } } private string _logpath; public string logpath { { homecoming _logpath; } set { _logpath = value; onpropertychanged("logpath"); } } private datetime _date; public datetime date; { { homecoming _date; } set { _date= value; onpropertychanged("date"); } } private bool _activity; public bool lastactivity { { homecoming _activity; } set { _activity= value; onpropertychanged("lastactivity"); } }

code behind logfile viewmodel :

logmodel _mymodel = new logmodel(); private observablecollection<logfilemodel> _logfiledata = new observablecollection<logfilemodel>(); public observablecollection<logfilemodel> logfiledata { { homecoming _logfiledata; } set { _logfiledata = value; onpropertychanged("logfiledata"); } } public logfileviewmodel() { initializeload(); timer.tick += new eventhandler(timer_tick); timer.interval = new timespan(0, 0, 3); timer.start(); } ~logfileviewmodel() { dispose(false); } protected virtual void dispose(bool disposing) { if (!disposed) { if (disposing) { timer.stop(); timer.tick -= new eventhandler(timer_tick); } disposed = true; } } private void timer_tick(object sender, eventargs e) { seek { logfiledata.clear(); initializeload(); } grab (exception ex) { timer.stop(); console.writeline(ex.message); } } private void initializeload() { seek { datatable table = _mymodel.getdata(); (int = 0; < table.rows.count; ++i) logfiledata.add(new logfilemodel { hostid= convert.toint32(table.rows[i][0]), logid = table.rows[i][1].tostring(), logpath = table.rows[i][2].tostring(), date = convert.todatetime(table.rows[i][3]), lastacivity= table.rows[i][4].tostring(), }); } grab (exception e) { console.writeline(e.message); } } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { var handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } public class logmodel { public datatable getdata() { datatable ndt = new datatable(); sqlconnection sqlcon = new sqlconnection(configurationmanager.connectionstrings["myconnectionstring"].connectionstring); sqlcon.open(); sqldataadapter da = new sqldataadapter("select * [localdb].[dbo].[logfiles]", sqlcon); da.fill(ndt); da.dispose(); sqlcon.close(); homecoming ndt; } } }

}

i have followed same pattern host model , viewmodel too.

you have have next in code viewmodel

a selecteditem hold selected item in first datagrid. collection hostdata holds of hosts. empty collection log info display logs particular host

//populate hosts , bind first datagrid private observablecollection<hostmodel> _hostdata= new observablecollection<host>(); public observablecollection<hostmodel> hostdata { { homecoming _hostdata; } set { _hostdata= value; onpropertychanged("hostdata"); } } //populate logs selected item , bind sec datagrid private observablecollection<logfilemodel> _logfiledata = new observablecollection<logfilemodel>(); public observablecollection<logfilemodel> logfiledata { { homecoming _logfiledata; } set { _logfiledata = value; onpropertychanged("logfiledata"); } } //when user selects item in first datagrid property hold value //so bind selected item property of first datagrid private host _selectedhost; //initialise avoid null issues public hostmodelselectedhost { get{ homecoming _selecteditem; } set { //call method populate sec collection _selectedhost = value; logfiledata = getlogsforselectedhost(_selectedhost); onpropertychanged("selectedhost"); { } //the method populating sec collection private observablecollection<logfilemodel> getlogsforselectedhost(_selectedhost) { observablecollection<logfilemodel> filteredlogs = new observablecollection<logfilemodel>; filteredlogs = //fill collection logs match host id of //your selected item homecoming filteredlogs ; }

i not sure code class hope code above can show way of doing this.

c# wpf wpf-controls wpfdatagrid

No comments:

Post a Comment