c# - Get content of GridView -
i have application c#/wpf listview containing gridview. want content create csv file values.
i have made research found solution windows form or wpf datagrid. nothing gridview.
my list:
<listview grid.row="1" x:name="mylistview" borderbrush="white" horizontalalignment="stretch" itemssource="{binding path=myitem}" selecteditem="{binding path=actualitem}"> <listview.view> <gridview x:name="mygridview"> <gridviewcolumn header="{x:static p:resources.name}" displaymemberbinding="{binding path=name,mode=oneway,updatesourcetrigger=propertychanged}" width="{staticresource doublenan}"/> <gridviewcolumn header="{x:static p:resources.label}" displaymemberbinding="{binding path=label,mode=oneway,updatesourcetrigger=propertychanged}"/> </gridview> </listview.view> </listview>
i can header no content:
system.text.stringbuilder csv = new system.text.stringbuilder(); string separator = system.globalization.cultureinfo.currentculture.textinfo.listseparator; foreach (system.windows.controls.gridviewcolumn col in mygridview.columns) { string header = col.header string; if (!string.isnullorwhitespace(header)) { csv.append(header + separator); } } csv.appendline(); system.io.file.writealltext(path, csv.tostring());
how can content make csv file ?
i found solution, maybe not best works.
the problem was:
to header gridview, because headers depend on language
to displayed columns
items in gridview tyep myitem
, columns contain properties. created loop header , property binded gridview
. second loop values objects properties in listview
system.text.stringbuilder csv = new system.text.stringbuilder(); string separator = system.globalization.cultureinfo.currentculture.textinfo.listseparator; list<string> listproperties = new list<string>(); csv.appendline("sep=" + separator); foreach (system.windows.controls.gridviewcolumn col in mygridview.columns) { // check if column displayed if (col.actualwidth > 0) { // header value string header = col.header string; //check if header not empty if (!string.isnullorwhitespace(header)) { // write in firte line csv.append(header + separator); // if columns binding property system.windows.data.binding binding = col.displaymemberbinding system.windows.data.binding; if (binding != null) { // name of property listproperties.add(binding.path.path); } } } } // write first line csv.appendline(); foreach (myitem item in mylistview.items) { foreach (string property in listproperties) { // , write value property object value = getpropvalue(item, property); csv.append(value + separator); } csv.appendline(); } system.io.file.writealltext(path, csv.tostring()); public static object getpropvalue(object src, string propname) { return src.gettype().getproperty(propname).getvalue(src, null); }
Comments
Post a Comment