Sunday, 15 April 2012

file - Formatting text output from DataGridView in vb.net -



file - Formatting text output from DataGridView in vb.net -

i'm writing values of 2 (1 column) dgvs , text box text file. text box value heading, dgv1 column of strings (such si ca s c...etc) , dgv2 column of numbers of format 11.23 or 0.01. formats how want them displayed in dgvs, formatting doesn't carry on text file these values output to. here's want textfile like:

a012345 101 1.03 si 32.13 c 1.45 ca 0.03

here's i'm getting instead:

a012345 101 1.03 si32.13015 c1.452359 ca0.032568

here's code:

seek dim da, da2 oledbdataadapter dim ds, ds2 dataset 'open connection nit2.xlsm' dim cn new oledb.oledbconnection("provider=microsoft.ace.oledb.12.0;data source=""" & textbox2.text & """;extended properties=""excel 12.0;hdr=yes;""") 'fill dataviewgrid1 element symbols, string, once' da = new oledbdataadapter("select * [sheet1$a:a" & lastrow & "]", cn) ds = new system.data.dataset da.fill(ds) datagridview1.datasource = ds.tables(0) '''''''''''''loop through each sample , write text file''''''''''''''''''''''' da2 = new oledbdataadapter("select * [sheet1$b:b" & lastrow & "]", cn) ds2 = new system.data.dataset da2.fill(ds2) datagridview2.datasource = ds2.tables(0) 'write sample heading textbox1, looped' textbox1.text = "q0" & xlwsheet1.cells(1, 2).value & " " & xlwsheet2.cells(1, 2).value 'write text file each info analysis point formatted heading, looped' dim title string = textbox1.text dim sub_title string = title.substring(0, 16) using author new system.io.streamwriter(fname, true) writer.writeline(sub_title) 'heading' dim symbol, symbolt string dim compo string cell1 integer = 0 (datagridview1.rows.count - 2) symbol = me.datagridview1(0, cell1).value 'element symbol' symbolt = symbol.trim compo = me.datagridview2(0, cell1).value 'composition' writer.writeline(symbolt & compo) next writer.close() end using cn.close()

how can format numbers dgv2 display 2 decimals?

i need set spacing between si , 32.13 (for example) decimal points in each line line up.

formatting...anyone?

something ?

dim symbol string dim compo string dim symbolspan integer = 0 ' stores maxlength of symbol dim compospan integer = 0 ' stores max integer part length of compo dim symbollist new list(of string) ' trimmed symbol dim compolist new list(of string) ' trimmed compo dim compointlengthlist new list(of integer) dim doublevalue double dim integer dim j integer = 0 (datagridview1.rows.count - 2) ' element symbol... symbol = me.datagridview1(0, i).value.tostring().trim() symbollist.add(symbol) if symbol.length > symbolspan symbolspan = symbol.length end if ' composition... if double.tryparse(me.datagridview2(0, i).value.tostring().trim(), doublevalue) compolist.add(doublevalue.tostring("r")) ' well, tostring("r") not displayed value. ' if dgv allows scientific display double ' you'll have handle here code... compo = doublevalue.tostring("f0") ' gets integer part length... compointlengthlist.add(compo.length) if compo.length > compospan compospan = compo.length end if else ' ohhhh ! cell doesn't contain valid number... :/ compo = me.datagridview2(0, i).value.tostring().trim() compolist.add(compo) if compo.contains(".") ' watch out civilization dots , commas... if compospan < compo.indexof("."c) compospan = compo.indexof("."c) end if compointlengthlist.add(compo.indexof("."c)) else if compospan < compo.length compospan = compo.length end if compointlengthlist.add(compo.length) end if end if next symbolspan = symbolspan + 1 ' space between symbol , compo. using author new system.io.streamwriter(fname, true) writer.writeline(sub_title) 'heading' = 0 symbollist.count - 1 symbol = symbollist.item(i) while symbol.length < symbolspan symbol = symbol + " " end while compo = compolist.item(i) j = compointlengthlist.item(i) while j < compospan compo = " " + compo j = j + 1 ' causing endless loop. end while writer.writeline(symbol & compo) next symbollist.clear() symbollist= nil compolist.clear() compolist = nil compointlengthlist.clear() compointlengthlist = nil writer.close() end using

i haven't tested code. just written on fly.. approach looks ugly (and agree) that's 1 way remember far. improve string.format or stringbuilder guess don't remember how ones works, sorry.

edit : missed 2 decimals part...

oh, sorry ! want 2 decimals.. replace :

compolist.add(doublevalue.tostring("r"))

by :

compolist.add(doublevalue.tostring("f2")) ' or : compolist.add(doublevalue.tostring("#.##")) ' allow 0 or 1 decimal aswell

and replace part :

' ohhhh ! cell doesn't contain valid number... :/ compo = me.datagridview2(0, i).value.tostring().trim() compolist.add(compo) if compo.contains(".") ' watch out civilization dots , commas... if compospan < compo.indexof("."c) compospan = compo.indexof("."c) end if compointlengthlist.add(compo.indexof("."c)) else if compospan < compo.length compospan = compo.length end if compointlengthlist.add(compo.length) end if

by :

' ohhhh ! cell doesn't contain valid number... :/ compo = me.datagridview2(0, i).value.tostring().trim() if compo.contains(".") ' watch out civilization dots , commas... if compospan < compo.indexof("."c) compospan = compo.indexof("."c) end if if compo.length > (compo.indexof("."c) + 3) compo = compo.substring(0, compo.indexof("."c) + 3) end if compointlengthlist.add(compo.indexof("."c)) else if compospan < compo.length compospan = compo.length end if compointlengthlist.add(compo.length) end if compolist.add(compo)

vb.net file text datagridview

No comments:

Post a Comment