Save data from telerik radgrid to database

 ̄綄美尐妖づ 提交于 2021-02-11 16:47:17

问题


Hi I'm new using telerik rad controls an asp development and I need some help. I have a radGrid wich is populated with data when a button is pressed, I found a solution here in the forum for selecting a row of the radGrid and taking the values of each cell, but now I want to know how to save this values into my database using entity framework. What I've been trying is to put the values into strings and then in my saving method call the strings, but when I execute the program I add two breakpoints, one in the method that selects the row and take the values and other in the saving method, whe see the results of the breakpoints in the first method which takes the value of the selected row each string contains the data corresponding to the cell in the radGrid, but in my saving method the strings appears empty, How can I solve this problem? Hope you could help me. Here's my two methods for selecting and saving

protected void RgSolic_SelectedIndexChanged(object sender, EventArgs e) 
   { 
       foreach (GridDataItem selectedItem in rgSolic.SelectedItems) 
       { 
           strNom = selectedItem["NombreCompleto"].Text; 
           strPuesto = selectedItem["nom_pue"].Text; 
           strCveAdscripcion = selectedItem["cve_adscripcion"].Text; 
           strArea = selectedItem["nom_area"].Text; 

       }  
   } 
   protected void btnRegistra_Click(object sender, EventArgs e) 
   { 
       //BD_SSEGUAEntities sseguaRegistro = new BD_SSEGUAEntities(); 
       //sseguaRegistro.spRegistraSolicitud1(); 
       //Valida Datos del Menor 
       using (var dbContext = new BD_SSEGUAEntities()) 
       { 
           var ResultSet = new Menor(); 
           var ResultSol = new Solicitud(); 
           var ResulBitacora = new Bitacora(); 
           if (chkMasc.Checked) 
           { 
               sexo = "M"; 
           } 
           if (chkFem.Checked) 
           { 
               sexo = "F"; 
           } 
           if (rdTxtAPatMen.Text != "" && rdTxtAMatMen.Text != "" && rdTxtNomMen.Text != "" && RadDatePicker1.SelectedDate != null && sexo != "" && rdCmbEdificio.SelectedValue != "" && rdTxtDomicilio.Text != "" && rdTxtHEnt.Text != "" && rdTxtHSal.Text != "" && rdTxtTelOfic.Text != "" && rdTxtExt.Text != "" && rdTxtPart.Text != "" && rdTxtCorreoE.Text != "" && rdTxtDomPart.Text != "" && rdTxtNHijos.Text != "" && rdCmbTEmbzo.SelectedValue != "0") 
           { 
               try 
               { 

                   System.DateTime.Now.Year.ToString(); 
                   //ResultSol.fcCvePuesto = rgSolic.Columns.FindByUniqueName("nom_pue").ToString(); 
                   //ResultSol.fcCveAdsc = rgSolic.Columns.FindByDataField("cve_adscripcion").ToString(); 
                   ResultSol.fcNomEmpleado = strNom; 

                            ResultSol.fcCveAdsc = strCveAdscripcion;

                   ResultSol.fcCvePuesto = strPuesto; 
                   ResultSol.fcDomLabora = rdTxtDomicilio.Text; 
                   ResultSol.fiHorEntLab = (Byte)rdTxtHEnt.Value; 
                   ResultSol.fiHorSalLab = (Byte)rdTxtHSal.Value; 
                   ResultSol.fiTelOfna = rdTxtTelOfic.Text; 
                   ResultSol.fiExtTel = rdTxtExt.Text; 
                   ResultSol.fiTelPart = rdTxtPart.Text; 
                   ResultSol.fcCorreoE = rdTxtCorreoE.Text; 
                   ResultSol.fcDomPart = rdTxtDomPart.Text; 
                   ResultSol.fiNumHijos = (Byte)rdTxtNHijos.Value; 

                   foreach (var menor in listaMenores) 
                   { 
                       ResultSol.Menor.Add(menor); 
                   } 
                   dbContext.AddToSolicitud(ResultSol); 
                   dbContext.SaveChanges(); 
                   //ResultSet.fcPatMenor = rdTxtAPatMen.Text; 
                   //ResultSet.fcMatMenor = rdTxtAMatMen.Text; 
                   //ResultSet.fcNomMenor = rdTxtNomMen.Text; 
                   //ResultSet.fdFchNacMenor = (DateTime)RadDatePicker1.SelectedDate; 
                   //ResultSet.fiAnosMenor = (Byte)rdTxtAnio.Value; 
                   //ResultSet.fiMesesMenor = (Byte)rdTxtMeses.Value; 
                   //ResultSet.fiSexoMenor = sexo; 
                   //ResultSet.fdFchRegMenor = DateTime.Today; 
                   //dbContext.Menores.AddObject(ResultSet); 
                   //dbContext.SaveChanges(); 
                   //RgMenor.DataSource = ResultSet; 
                   //RgMenor.Rebind(); 
               } 
               catch (Exception ex) 
               { 
                   Alerta(ex.Message); 
               } 
           } 
           else 
           { 
               Alerta("Captura todos lo datos del Menor"); 
           } 
       } 
   }

回答1:


The code does not include the part about how your reference the textboxes (rdTxtXXXXX) inside the grid items. There are two options depending on whether these are textboxes inside edited rows or item templates of template columns (respectively):

//inside the button click handler, textboxes in edited rows
//have in mind the detail about the edited items from [this doc][1]
foreach(GridEditableItem editedItem in rgSolic.EditItems)
{
  TextBox txtBox = editedItem.FindControl("rdTxtDomicilio") as TextBox;
  //retrieve the text here
  ...............................................
}

//inside the button click handler, textboxes in template columns' item templates
foreach(GridDataItem dataItem in rgSolic.MasterTableView.Items)
{
  TextBox txtBox = dataItem.FindControl("rdTxtDomicilio") as TextBox;
  //retrieve the text here
  ...............................................
}


来源:https://stackoverflow.com/questions/6155312/save-data-from-telerik-radgrid-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!