C# dynamic GridView/DataTable set up -


i'm working on table that's going dynamic company work for. hacked way around getting gridview work way wanted.

but changed bit...

  • i have gridview.
  • every single column , row going textbox put numbers into.
  • you start out 1 column , can add multiple ones @ push of button.
  • there's fixed amount of rows (23).

i tried adding textbox data row below, shows string of namespaces textbox in. should do? i'd avoid asp stuff, don't have slightest clue it's doing

here have done far.

datatable dt = new datatable(); datacolumn dc = new datacolumn("microns",typeof(textbox));  dt.columns.add(dc);  (int = 0; < 23; ++i) {     textbox tb = new textbox();     datarow row = dt.newrow();      row["microns"] = tb;     dt.rows.add(row); } foreach (datacolumn col in dt.columns) {     boundfield bfield = new boundfield();     bfield.datafield = col.columnname;     bfield.headertext = col.columnname;     gridview1.columns.add(bfield); }   gridview1.datasource = dt; 

ok according question , sense seems want add textbox dynamically. if create class , write below code in class:

using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.componentmodel; using system.collections; /// <summary> /// summary description dynamictemplate /// </summary> public class dynamictemplate : system.web.ui.itemplate { public string tablename { get; set; }  system.web.ui.webcontrols.listitemtype templatetype; system.collections.hashtable htcontrols = new system.collections.hashtable(); system.collections.hashtable htbindpropertiesnames = new system.collections.hashtable(); system.collections.hashtable htbindexpression = new system.collections.hashtable();  public dynamictemplate(system.web.ui.webcontrols.listitemtype type) {     templatetype = type; }  public void addcontrol(webcontrol wbcontrol, string bindpropertyname, string bindexpression) {     htcontrols.add(htcontrols.count, wbcontrol);     htbindpropertiesnames.add(htbindpropertiesnames.count, bindpropertyname);     htbindexpression.add(htbindexpression.count, bindexpression); }  public void addcontrol(htmlcontrol wbcontrol, string bindpropertyname, string bindexpression) {     htcontrols.add(htcontrols.count, wbcontrol);     htbindpropertiesnames.add(htbindpropertiesnames.count, bindpropertyname);     htbindexpression.add(htbindexpression.count, bindexpression); }  public void instantiatein(system.web.ui.control container) {     placeholder ph = new placeholder();      (int = 0; < htcontrols.count; i++)     {         //clone control          control cntrl = clonecontrol((control)htcontrols[i]);          switch (templatetype)         {             case listitemtype.header:                 break;             case listitemtype.item:                 ph.controls.add(cntrl);                 break;             case listitemtype.alternatingitem:                 ph.controls.add(cntrl);                 ph.databinding += new eventhandler(item_databinding);                 break;             case listitemtype.footer:                 break;         }     }     ph.databinding += new eventhandler(item_databinding);     container.controls.add(ph); } public void item_databinding(object sender, system.eventargs e) {     placeholder ph = (placeholder)sender;     gridviewrow ri = (gridviewrow)ph.namingcontainer;     (int = 0; < htcontrols.count; i++)     {         if (htbindpropertiesnames[i].tostring().length > 0)         {             control tmpctrl = (control)htcontrols[i];              string item1value = gettype(htbindexpression[i].tostring(), ri);              //guid value = new guid(databinder.eval(ri.dataitem, htbindexpression[i].tostring()).tostring());             //string valuestring = value.tostring();             //item1value = item1value1.tostring();              control ctrl = ph.findcontrol(tmpctrl.id);             type t = ctrl.gettype();             system.reflection.propertyinfo pi = t.getproperty(htbindpropertiesnames[i].tostring());             if (pi != null)             {                 pi.setvalue(ctrl, item1value.tostring(), null);             }         }     } }  private string gettype(string columnname, gridviewrow row) {     string result = "";     clssearch obj = new clssearch();     // have set table name static require make search working macintosh project only.     // per rakesh sir directed.     int32 type = obj.getnumberfordatatypeofcolumn("tblproperty", columnname);     switch (type)     {         case 1:             datetime dtvalue = (datetime)databinder.eval(row.dataitem, columnname);             result = dtvalue.tostring();             break;         case 2:             string svalue = (string)databinder.eval(row.dataitem, columnname);             result = svalue;             break;         case 3:              break;         case 4:             int32 ivalue = (int32)databinder.eval(row.dataitem, columnname);             result = ivalue.tostring();             break;         case 5:             guid gvalue = (guid)databinder.eval(row.dataitem, columnname);             result = gvalue.tostring();             break;     }      return result; }  private control clonecontrol(system.web.ui.control src_ctl) {     type t = src_ctl.gettype();     object obj = activator.createinstance(t);     control dst_ctl = (control)obj;     propertydescriptorcollection src_pdc = typedescriptor.getproperties(src_ctl);     propertydescriptorcollection dst_pdc = typedescriptor.getproperties(dst_ctl);      (int = 0; < src_pdc.count; i++)     {         if (src_pdc[i].attributes.contains(designerserializationvisibilityattribute.content))         {             object collection_val = src_pdc[i].getvalue(src_ctl);             if ((collection_val ilist) == true)             {                 foreach (object child in (ilist)collection_val)                 {                     control new_child = clonecontrol(child control);                     object dst_collection_val = dst_pdc[i].getvalue(dst_ctl);                     ((ilist)dst_collection_val).add(new_child);                 }             }         }         else         {             dst_pdc[src_pdc[i].name].setvalue(dst_ctl, src_pdc[i].getvalue(src_ctl));         }     }      return dst_ctl;   } } 

ok come method binding grid.before bind grid means before yo specify datasource grid use below code add textboxes according requirement. adding 1 example.

    templatefield t = new templatefield();     dynamictemplate mt = new dynamictemplate(listitemtype.item);      textbox ibtnedit = new textbox();     ibtnedit.id = "btnedit";     ibtnedit.cssclass = "editbutton";     ibtnedit.tooltip = "name";     mt.addcontrol(ibtnedit, "alternatetext", "id");     t.itemtemplate = mt;     t.headertext = "activity";     t.headerstyle.horizontalalign = horizontalalign.left;     gridview1.columns.add(t);     gridview1.datasource = datatable;     gridview1.databind(); 

that's it.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -