dictionary - How to expand abbreviations in datatable row by row c# -


i'm using datatable has 2 columns of text data, want abbreviations expanding each row, used dictionary , here sample of code:

   private void expandwords()     {             datatable dt = new datatable();         datarow row;         dt.columns.add(new system.data.datacolumn("name", typeof(string)));         dt.columns.add(new system.data.datacolumn("label", typeof(string)));          dictionary<string, string> dict = new dictionary<string, string>();         dict.add("emp", "employee");         dict.add("dep", "department");          (int = 0; < datagridview6.rows.count; i++)         {                row = dt.newrow();             foreach(keyvaluepair<string,string> rep in dict)             {               row[0] = datagridview6.rows[i].cells[0].value.tostring().replace    (rep.key, rep.value);               row[1] = datagridview6.rows[i].cells[1].value.tostring().replace  (rep.key, rep.value);             }         dt.rows.add(row);         datagridview7.datasource = dt;      } 

it can run without exceptions doesn't work

why enumerate dictionary in loop of grid-rows? lookup key.

you have add each row in loop, not outside, otherwise add last row:

for (int = 0; < datagridview6.rows.count; i++) {        row = dt.newrow();     string abbrcell1 = datagridview6.rows[i].cells[0].value.tostring();     string abbrcell2 = datagridview6.rows[i].cells[1].value.tostring();     row[0] = dict[abbrcell1];     row[1] = dict[abbrcell2];     dt.rows.add(row); } 

edit if want replace abbreviated words in string of datagrid cells not-abbreviated word in dictionary can use code:

// in loop row = dt.newrow(); string abbrcell1 = datagridview6.rows[i].cells[0].value.tostring(); string abbrcell2 = datagridview6.rows[i].cells[1].value.tostring(); ienumerable<string> unabbrwords1 = abbrcell1.split()     .select(w => dict.containskey(w) ? dict[w] : w); ienumerable<string> unabbrwords2 = abbrcell2.split()     .select(w => dict.containskey(w) ? dict[w] : w); row[0] = string.join(" ", unabbrwords1); row[1] = string.join(" ", unabbrwords2); dt.rows.add(row); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -