This is a migrated thread and some comments may be shown as answers.

[Solved] how to add a dropdown list to the telerikgrid column

2 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
vinkesh
Top achievements
Rank 1
vinkesh asked on 12 Jul 2011, 07:39 AM
hello telerik

i have tried my level best  but  i'll be u nable to insert a dropdownlist to telerik grid .if you have any solution regarding this then provide me
code will be like this:
@(Html.Telerik().Grid<TelerikMvcApplication3.Models.tb_TestMvc>()
.BindTo((List<TelerikMvcApplication3.Models.tb_TestMvc>)ViewData["db_Test"])
        .Name("Grid")
            
         .DataKeys(keys =>
         {
             keys.Add(p => p.Id);
         })
                  .DataBinding(dataBinding => dataBinding.Server()
                   .Update("UpdateTest", "HOME")
                           .Insert("InsertTest", "HOME")
                                   .Delete("DeleteTest", "HOME")
                           )
              .Columns(columns =>
              {
                  columns.Bound(p => p.Id).Width(210).ReadOnly();
                  columns.Bound(p => p.ContactTitle).Width(130).Format("{0:c}");
                  columns.Bound(p => p.ContactName).Width(130).Format("{0:N}");
                  columns.Bound(p => p.Country).Width(130).Format("{0:d}" );
                  columns.Bound(p => p.Product).Width(130).Format("{0:d}");
                  columns.Bound(p => p.ProDetail).Width(130).Format("{0:d}");
                  columns.Bound(p => p.ProPrice).Width(130).Format("{0:d}");
                  columns.Bound(p => p.OrderDate).Width(130).Format("{0:d}");
                  columns.Bound(p => p.country_Id).Width(130).Format("{0:d}");
                  columns.Bound(p => p.Id)       
                   .ClientTemplate("<img width='100' height='110' alt='<#=Id #>' src='"
                    + Url.Content("../../Images/")
                     + "<#=Id #>.jpg' />")
                     .Title("Picture");


             //columns.Bound(p => p.country).ClientTemplate(
             //                       Html.Telerik().DropDownList()
             //                           .Name("ddlcnt")
             //                           .BindTo(new SelectList(UserController.GetRoles()))
             //                           .ToHtmlString()
                               

            //).Title("CountryName");


                     
           
            //columns.Bound(p => p.Country);
                 
                  columns.Command(commands =>
                      {
                          commands.Edit();
                          commands.Delete();
                      });
              })
                       
                           .ToolBar(t =>
                           {
                               t.Insert();
                           })

                 .Sortable()
                .Filterable()
                 .Pageable(paging=>paging.PageSize(5))

               )

on controller it will be like this:
  public class HomeController : Controller
    {
       public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            var tme = new db_TestEntities1();
            ViewData["db_Test"] = tme.tb_TestMvc.ToList();
            return View();
        }

           public ActionResult About()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateTest(int Id)
        {
            var tst = new test
            {
                Id = Id,
                
            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Update tb_TestMvc set Contacttitle=@Contacttitle,ContactName=@ContactName,Country=@country where Id=@Id";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
                cmd1.Parameters.AddWithValue("@Contacttitle", tst.Contacttitle);
              cmd1.Parameters.AddWithValue("@ContactName", tst.ContactName) ;
              cmd1.Parameters.AddWithValue("@Country", tst.Country);
                cmd1.ExecuteNonQuery();
                con.Close();
            }

            var tme = new db_TestEntities1 ();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
           // return View("Index");
           
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertTest(int Id, string Contacttitle)
        {
            var tst = new test
            {
                Id = Id,

            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Insert into tb_TestMvc (Id,Contacttitle,ContactName,Country) values (@Id,@Contacttitle,@ContactName,@Country)";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
                cmd1.Parameters.AddWithValue("@Contacttitle", Contacttitle);
                cmd1.Parameters.AddWithValue("@ContactName", tst.ContactName) ;
                cmd1.Parameters.AddWithValue("@Country", tst.Country);
                cmd1.ExecuteNonQuery();
                con.Close();
            }

            var tme = new db_TestEntities1();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
            // return View("Index");

        }
         [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteTest(int Id, string Contacttitle)
        {
            var tst = new test
            {
                Id = Id,

            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Delete from tb_TestMvc where Id=@Id";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
               
                cmd1.ExecuteNonQuery();
                con.Close();
            }

            var tme = new db_TestEntities1();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
            // return View("Index");

        }
        [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult _TemplatesClientSide(int Id)
         {
             var tst = new test
            {
                Id = Id,

            };
             if (TryUpdateModel(tst))
             {
                 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                 if (con.State == ConnectionState.Closed)
                 {
                     con.Open();
                 }
                 SqlCommand cmd1 = new SqlCommand();
                 cmd1.CommandText = "Select country_img from tb_Country where country_Id=@country_Id";
                 cmd1.Connection = con;
                 cmd1.Parameters.AddWithValue("@country_Id", tst.country_Id);
                 cmd1.Parameters.AddWithValue("@country_img", tst.country_img);
                 cmd1.ExecuteNonQuery();
                 con.Close();
             }
             var tme = new db_TestEntities1();
             ViewData["tb_TestMvc"] = tme.tb_Country.ToList();
             return RedirectToAction("Index", "Home");
             }
         }
   }

2 Answers, 1 is accepted

Sort by
0
Rafal
Top achievements
Rank 1
answered on 12 Jul 2011, 11:30 AM
You can make partial view with drop down list

Exemple
name of this ProductName.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.Telerik().ComboBox()
        .Name("Name")
        .AutoFill(true)
        .ClientEvents(events => events.OnChange("GetProductData"))
        .BindTo(new SelectList((IEnumerable)ViewData["productList"], "Id", "Name"))
        .AutoFill(true)
        .Filterable(filtering =>{filtering.FilterMode(AutoCompleteFilterMode.StartsWith);})
        .HighlightFirstMatch(true)
        .Value(Model)
        .HtmlAttributes(new { style = "width:80px;" })
%>

and in model add

[UIHint("ProductName")]
public int Id { get; set; }
0
vinkesh
Top achievements
Rank 1
answered on 13 Jul 2011, 06:32 AM
hello rafal

 thanks  a lot for this reply


but i want show the  dropdownlist in the column of the telerik grid.

Is it posiible or not..

if possible how can i do that..


Thanks

Tags
Grid
Asked by
vinkesh
Top achievements
Rank 1
Answers by
Rafal
Top achievements
Rank 1
vinkesh
Top achievements
Rank 1
Share this question
or