c# - fetch selected row from DataGrid -
i'm using wpf grid. first column checkbox column, , there 1 save button on page. when button clicked, i'd checkboxes row set checked, insert in datatable.
how can done?
would help?
it uses decorator pattern wrap class (that's come out of orm linq2sql) enable databind "selected" value. can upon saving selected items in list of decorated items , save those.
(simple example, not mvvm compliant)
xaml:
<window x:class="checktest.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <datagrid autogeneratecolumns="false" itemssource="{binding path=foos}" name="gridfoos" margin="0,0,0,34"> <datagrid.columns> <datagridcheckboxcolumn header="selected" binding="{binding path=selected}"/> <datagridtextcolumn header="name" binding="{binding path=value.name}" /> <datagridtextcolumn header="age" binding="{binding path=value.age}"/> </datagrid.columns> </datagrid> <button content="save" height="23" horizontalalignment="left" margin="423,283,0,0" name="button1" verticalalignment="top" width="75" click="btnsave_click" /> </grid> </window>
code behind:
namespace checktest { public partial class mainwindow : window { public observablecollection<selecteddecorator<foo>> foos { get; set; } public mainwindow() { var bars = new list<foo>() { new foo() { name = "bob", age = 29 }, new foo() { name = "anne", age = 49 } }; var selectablebars = bars.select(_ => new selecteddecorator<foo>(_)).tolist(); foos = new observablecollection<selecteddecorator<foo>>(selectablebars); initializecomponent(); datacontext = this; } private void btnsave_click(object sender, routedeventargs e) { var savethese = foos.tolist().where(_ => _.selected).select(_=> _.value); //db.bulkupdate(savethese); ... or sutch; } } public class foo { public string name{get;set;} public int age { get; set; } } public class selecteddecorator<t> { public t value { get; set; } public bool selected { get; set; } public selecteddecorator(t value) { selected = false; this.value = value; } } }
Comments
Post a Comment