c# - I need to format a string as a phone number in a radgrid winform cell -


sounds silly simply, right?

i can format input, have database returning value string. no matter if stored in database 1234567890 1234567890 back. in database stored strictly string numeric data, meaning there no letters or special characters, varchar(10) field.

i have created gridviewmaskbox column , set numeric, in formatting event changed data numeric this:

gridviewmaskboxcolumn maskboxcolumn = new gridviewmaskboxcolumn();         maskboxcolumn.name = "phone";         maskboxcolumn.fieldname = "cellphone_number";         maskboxcolumn.headertext = "phone";         maskboxcolumn.masktype = masktype.numeric;         maskboxcolumn.mask = "(000) 000-0000";         maskboxcolumn.textalignment = contentalignment.middlecenter;         maskboxcolumn.readonly = false;         radgridview1.mastertemplate.columns.add(maskboxcolumn); 

then in cellformatting event:

  if (e.column.name=="cellphone")             {                 long cellinfo =convert.toint64(e.cellelement.value);                  e.cellelement.value = cellinfo;             } 

i tried testing purpose:

      if (e.column.name=="cellphone")             {                 long cellinfo =convert.toint64(e.cellelement.value);                 long number = 1234567890;                  e.cellelement.value = number;             } 

and cell still shows 123456789 instead of (123) 456-7890

i have tried creating standard text column , using every variation of: radgridview1.columns["cellphone"].formatstring = "{0:(###) ###-####}"; again in formatting event changing string int64. have verified in debugger indeed hitting event , changing data type correctly.

i ready make small animal sacrifices fear not , may hinder karma have built up. please help. have spent hours on should have been childs play.

joe

update: can below seems work, replaces text in grid "()" in numbers screws database update on edit:

 if (e.cellelement.value.tostring().length==10)                 {                     biginteger bi = new biginteger();                        bool worked = biginteger.tryparse(e.cellelement.value.tostring(),out bi);                        string numberastext = bi.tostring("###-###-####");                        e.cellelement.value = numberastext.tostring();} 

i think chasing tail...

update: have added new (non databound) column called cellmasked , have following single codeline in cellformating event databound cellphone column:

 e.row.cells["cellmasked"].value = convert.touint64(e.cellelement.value).tostring("###-###-####"); 

still working on feels step in right direction.

hold! don't kill no animals :).

first, want start explaining cellformatting event used appearance modifications grid uses ui virtualization cells. so, setting value not should there, however, setting cellelement.text might want. i.e can change text want , should in case.

in regards columns' mask , masktype properties, responsible editor of cells (when open edit).

here small example put you:

 protected override void onload(eventargs e)  {      base.onload(e);       addgrid();       datatable table = new datatable();      table.columns.add("cellphone_number");      table.rows.add("1234567890");      table.rows.add("3214567890");      table.rows.add("9874567890");       radgridview1.autogeneratecolumns = false;      radgridview1.autosizecolumnsmode = gridviewautosizecolumnsmode.fill;      radgridview1.datasource = table;       gridviewmaskboxcolumn maskboxcolumn = new gridviewmaskboxcolumn();      maskboxcolumn.name = "phone";      maskboxcolumn.fieldname = "cellphone_number";      maskboxcolumn.headertext = "phone";      maskboxcolumn.masktype = masktype.standard;      maskboxcolumn.mask = "(999) 000-0000";      radgridview1.mastertemplate.columns.add(maskboxcolumn);      radgridview1.cellformatting += radgridview1_cellformatting;  }   void radgridview1_cellformatting(object sender, telerik.wincontrols.ui.cellformattingeventargs e)  {      if (e.column.name == "phone")      {          e.cellelement.text = convert.touint64(e.cellelement.value).tostring("(###)-###-####");      }  } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -