c# - Get count of records in excel by groupings with count? -
i have excel following data:
week---parts
32--------part1
32--------part1
33--------part2
33--------part2
33--------part3
34--------part1
34--------part4
34--------part5
from this, somethin glike in datagridview
week---part1---part2---part3----part4---part5
32-----2------0--------0-------0--------0
33-----0------2--------1-------0--------0
34-----1------0--------0-------1--------1
this program importing excel files data , not connected database.
using vb.net code, how can make desired results?
tried (from hari prasad) no output. new in vb.net
dim filepath string = openfiledialog1.filename dim extension string = path.getextension(filepath) constr = string.empty select case extension case ".xls" 'excel 97-03 constr = string.format(excel03constring, filepath, "yes") exit select case ".xlsx" 'excel 07 constr = string.format(excel07constring, filepath, "yes") exit select end select 'get name of first sheet. using con new oledbconnection(constr) using cmd new oledbcommand() cmd.connection = con con.open() dim dtexcelschema datatable = con.getoledbschematable(oledbschemaguid.tables, nothing) sheetname = dtexcelschema.rows(0)("table_name").tostring() con.close() end using end using 'read data first sheet. using con new oledbconnection(constr) dim cmd oledbcommand = new oledbcommand((convert.tostring("select parts, week [") & sheetname) + "] ", con) dim adapter new oledbdataadapter(cmd) dim ds new dataset() adapter.fill(ds) dim contents = ds.tables(0).asenumerable().select(function(x) new { _ key .week = x.field(of double)("week"), _ key .parts = x.field(of string)("parts") _ }) dim result = contents.groupby(function(g) g.week).[select](function(g) new { _ key .week = g.key, _ key .groups = g.groupby(function(s) s.parts) _ }).[select](function(n) new { _ n.week, _ key .partandcount = n.groups.[select](function(s) new { _ key .part = s.key, _ key .count = s.count() _ }) _ }) dim gview = new datagridview() gview.columns.add("week", "week") each c string in contents.[select](function(c) c.parts).distinct() gview.columns.add(c, c) next each r string in result 'error: cannot converted string dim rowindex integer = gview.rows.add() dim row datagridviewrow = gview.rows(rowindex) each s string in r.partandcount 'error: not member of string row.cells(s.part).value = s.count next next end using
the datagridview need have data source (e.g. datatable) rows , columns following; 1. each row have unique week column order week ascending 2. each row have columns each of contains number of part appearing in data (as per data in question above)
what data source used depends on preference , requirements. example, if source data coming database, grid view using query populate grid, or either hard coded or cannot converted collection use datatable , populate rows/columns
Comments
Post a Comment