c# - Get the cell value of the third column on button click -


i have button in first column of datagrid, , when click i'm trying cell value of 3rd column of row clicked button on. example click button on row 3 of datagrid want cell value of column 3, row 3 int. how can that?

xaml button:

<control:datagrid.columns>                   <control:datagridtemplatecolumn>         <control:datagridtemplatecolumn.celltemplate>               <datatemplate>                  <button click="showhidedetailsclick" foreground="black">+</button>               </datatemplate>         </control:datagridtemplatecolumn.celltemplate>      </control:datagridtemplatecolumn> <control:datagrid.columns> 

c# handle click:

private void showhidedetailsclick(object sender, routedeventargs e) {    //want cell value here    system.windows.controls.button expandcollapsebutton = (system.windows.controls.button)sender;    dependencyobject obj = (dependencyobject)e.originalsource;    while (!(obj extendedgrid.microsoft.windows.controls.datagridrow) && obj != null) obj = visualtreehelper.getparent(obj);         if (obj extendedgrid.microsoft.windows.controls.datagridrow)         {             if (null != expandcollapsebutton && "+" == expandcollapsebutton.content.tostring())             {                 (obj extendedgrid.microsoft.windows.controls.datagridrow).detailsvisibility = visibility.visible;                 expandcollapsebutton.content = "-";             }             else             {                 (obj extendedgrid.microsoft.windows.controls.datagridrow).detailsvisibility = visibility.collapsed;                 expandcollapsebutton.content = "+";             }         }  } 

i use datatable fill datagrid data retrieved database, see code below:

public datatable sourcetable {                 {             return _sourcetable;         }         set         {             _sourcetable = value;             onpropertychanged("sourcetable");          }  }  sourcetable = new datatable();  sourcetable.columns.addrange(new datacolumn[]{                 new datacolumn("inputid", typeof(int)),                 new datacolumn("traderid", typeof(string)),                 new datacolumn("tradedate", typeof(datetime)),                 new datacolumn("tradetime", typeof(timespan)),                 new datacolumn("clientname", typeof(string)),                 new datacolumn("curpair", typeof(string)),                 new datacolumn("amnt", typeof(int)),                 new datacolumn("action", typeof(string)),                 new datacolumn("executedrate", typeof(decimal))             });  datarow rowsource = null;  var opentradesquery = qa in connection.quickanalyzerinputs                                   qa.tradecloseddatetime == null                                   select new                                   {                                       qa.inputid,                                       qa.traderid,                                       qa.clienttradeddate,                                       qa.clienttradedtime,                                       qa.clientname,                                       qa.currencypair,                                       qa.tradedamount,                                       qa.action,                                       qa.executedrate                                   };  foreach (var rowobj in opentradesquery)                 {                     rowsource = sourcetable.newrow();                     sourcetable.rows.add(rowobj.inputid, rowobj.traderid, rowobj.clienttradeddate, rowobj.clienttradedtime, rowobj.clientname, rowobj.currencypair, rowobj.tradedamount, rowobj.action, rowobj.executedrate);                 } 

and in xaml datagrid binding sourcetable

before anything, i'll suggest create little class contains information rows of database, populate datagrid. facilities in code. implementing answer.

the class this:

   public class populatedata    {         public int       inputid;         public string    traderid;         public datetime  tradedate;         public timespan  tradetime;         public string    clientname;         public string    curpair;         public int       amnt;         public string    action;         public decimal   executedrate;          public populatedata(int iid, string tid, datetime date, timespan ttime, string cname, string curpair, int amnt, string act, decimal exrate)         {           inputid   = iid;            traderid  = tid;           tradedate = date;           tradetime  = ttime;           clientname = cname;           curpair    = curpair;           amnt       = amnt;           action     = act;           executedrate = exrate;         }    } 

so when populate sourcetable, this:

  foreach (var rowobj in opentradesquery)   {      rowsource = sourcetable.newrow();      sourcetable.rows.add(new populatedata( rowobj.inputid, rowobj.traderid, rowobj.clienttradeddate, rowobj.clienttradedtime, rowobj.clientname, rowobj.currencypair, rowobj.tradedamount, rowobj.action, rowobj.executedrate));   } 

after row corresponding button (the datagridrow) it's done: have inside row; populatedata actually.

    private void showhidedetailsclick(object sender, routedeventargs e)     {        system.windows.controls.button expandcollapsebutton =  (system.windows.controls.button)sender;       dependencyobject obj = (dependencyobject)e.originalsource;      while (!(obj extendedgrid.microsoft.windows.controls.datagridrow) && obj != null) obj = visualtreehelper.getparent(obj);      //get entire row view here        if(obj != null && obj extendedgrid.microsoft.windows.controls.datagridrow)        {           datagridrow d = obj datagridrow; //convert datagridrow           populatedata st = d.item populatedata; //get populatedata            if (st.traderid == "what wants") //or else in class           {             // stuff here           }         }             if (obj extendedgrid.microsoft.windows.controls.datagridrow)         {            if (null != expandcollapsebutton && "+" == expandcollapsebutton.content.tostring())            {              (obj extendedgrid.microsoft.windows.controls.datagridrow).detailsvisibility = visibility.visible;              expandcollapsebutton.content = "-";            }             else             {               (obj extendedgrid.microsoft.windows.controls.datagridrow).detailsvisibility = visibility.collapsed;             expandcollapsebutton.content = "+";             }         }      } 

and that's , let me know if works you.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -