Tuesday, 26 May 2015

Edit Gridview in entity framework

       //btnEdit_Command    

 btnsubmt.Text = "UPDATE";
           int currid = Convert.ToInt32(e.CommandArgument.ToString());
           Session["ID"] = currid;         
           DataTable dt = objcls.GetApplicationDetails1(currid);
           if (dt.Rows.Count > 0)
           {
               // = dt.Rows[0]["vaApplicationNo"];
               txtname.Text = dt.Rows[0]["varname"].ToString();
               txtage.Text = dt.Rows[0]["varage"].ToString();
               txtaddress.Text = dt.Rows[0]["varaddress"].ToString();
               ddlinstitution.DataSource = dt.Rows[0]["institutionname"].ToString();
               ddlinstitution.DataBind();
          

   //DL



        public DataTable GetApplicationDetails1(int intAppID)
        {
            using (var context = new pharmacyEntities3())
            {
                var q = (from pd in context.registrations
                        
                         where pd.intid == intAppID
                         select new
                         {
                             pd.varname,
                             pd.varage,
                             pd.varaddress,
                            pd.institutionname

                         }).ToList();

                if (q.Count > 0)
                {
                    return ToDataTable(q);
                }

            }
            return null;

        }

        DataTable ToDataTable<T>(IEnumerable<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            var table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }


No comments:

Post a Comment