Friday, 24 May 2013

How to add multiple lines series dynamically in kendo UI chart

JavaScript
  

HTML
............. .............
Annully Monthly Weekly Daily
Below code snippet is only for reference to create Dummy data in MVC.

Controller
  
public class HomeController : Controller
{
    [AllowAnonymous]
    [HttpGet]
    public JsonResult GetDummydata(int Duration)
    {
        List>TestModels> models = GetDummyData(Duration);

        return Json(models, JsonRequestBehavior.AllowGet);
    }

    public List<TestModels> GetDummyData(int Duration)
    {
        List<TestModels> models = new List<TestModels>();

        switch (Duration)
        {
            case (int)DurationType.Annully:

                TestModels testModel = new TestModels();
                testModel.Field1 = "Annully";
                testModel.Field2 = 20;
                testModel.Field3 = 50;
                testModel.Field4 = 70;
                models.Add(testModel);

                break;
            case (int)DurationType.Monthly:

                TestModels testModel2 = new TestModels();
                testModel2.Field1 = "Monthly";
                testModel2.Field2 = 50;
                testModel2.Field3 = 50;
                testModel2.Field4 = 50;
                models.Add(testModel2);

                break;
            case (int)DurationType.Weekly:
                TestModels testModel3 = new TestModels();
                testModel3.Field1 = "Weekly";
                testModel3.Field2 = 70;
                testModel3.Field3 = 10;
                testModel3.Field4 = 90;
                models.Add(testModel3);
                break;
            case (int)DurationType.Daily:
                TestModels testModel4 = new TestModels();
                testModel4.Field1 = "Daily";
                testModel4.Field2 = 11;
                testModel4.Field3 = 111;
                testModel4.Field4 = 1111;
                models.Add(testModel4);
                break;

        }

        return models;
    }
}
Model
  
 [Serializable]
    public class TestModels
    {   
        public string Field1 { get; set; }
        public int Field2 { get; set; }
        public int Field3 { get; set; }
        public int Field4 { get; set; }
    }

Saturday, 4 May 2013

How to add a new worksheet after all of the others in Excel Interop

Worksheet xlNewSheet = null;

// Add new worksheet in workbook
xlNewSheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(missing, missing, missing, missing);

// Add new worksheet at the end of all sheets in workbook
xlNewSheet = (Excel.Worksheet)xlWorkbook.Sheets.Add(After: xlWorkbook.Sheets[xlWorkbook.Sheets.Count], Count: 1, Type: Excel.XlSheetType.xlWorksheet);

// Give worksheet name
xlNewSheet.Name = worksheetName;

Thursday, 31 January 2013

Download Files From Grid in Kendo UI

View
 
@(Html.Kendo().Grid<MvcApplication1.Models.TestModels>() .Name("Grid") .Columns(columns => { columns.Bound(p => p.ID).Groupable(false); columns.Bound(p => p.Name); columns.Template(@).ClientTemplate("Download file").Title("Download1"); columns.Template(@).ClientTemplate("" + Html.ActionLink("Download me", "DocumentDownload2", "Home", new { id = "#=ID#" }, null) + "").Title("Download2"); }) .DataSource(dataSource => dataSource .Ajax() .Read(read => read.Action("Grid_Read", "Home")) ) )

Controller
public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request)
{
    List<TestModels> models = new List<TestModels>();

    for (int i = 1; i < 6; i++)
    {
        TestModels model = new TestModels();
        model.ID = i;
        model.Name = "Name" + i;
        models.Add(model);
    }

    return Json(models.ToDataSourceResult(request));
}


public ActionResult DocumentDownload1()
{
    string contentType = "application/xlsx";
    string filePath = Server.MapPath("~/Files/YourFileName.xlsx");
    return File(filePath, contentType, "YourFileName.xlsx");
}

public ActionResult DocumentDownload2(int id)
{
    string contentType = "application/xlsx";
    string filePath = Server.MapPath("~/Files/YourFileName.xlsx");
    return File(filePath, contentType, "YourFileName_" + id.ToString() + ".xlsx");
}

Monday, 21 January 2013

How to use special characters in enums in asp.net

.aspx


        
            
                
                    
                    
                    
                    
                    
                        
                            
                        
                        
                            
                            
                        
                    
                    
                    
                
            

.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
      Class1.BindDropDownListWithEnum(ref DropDownList1, typeof(Enums.ShipperRating));
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Shipper", typeof(int));
        dt.Columns.Add("ShipperName", typeof(string));
        dt.Columns.Add("Rating", typeof(int));
        dt.Rows.Add("1", "ShipperName1", "1");
        dt.Rows.Add("2", "ShipperName2", "2");
        dt.Rows.Add("3", "ShipperName3", "2");

        RadGrid1.DataSource = dt;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode && e.Item is GridEditableItem)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            RadComboBox RadComboBox1 = item.FindControl("RadComboBox1") as RadComboBox;
            int Rating = Convert.ToInt32(item.GetDataKeyValue("Rating"));
            Class1.BindRadComboBoxWithEnum(ref RadComboBox1, typeof(Enums.ShipperRating));

            if (RadComboBox1.Items.FindItemByValue(Convert.ToString(Rating)) != null)
            {
                RadComboBox1.Items.FindItemByValue(Convert.ToString(Rating)).Selected = true;
            }
        }
    }
Class1.cs
public class DisplayValue : Attribute
{
    private string _value;

   
    public DisplayValue(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}

public class EnumListItem
{
    private int _value;
    public int Value
    {
        get { return this._value; }
        set { this._value = value; }
    }

    private string _displayName;
    public string DisplayName
    {
        get { return _displayName; }
        set { _displayName = value; }
    }

    private string _name;
    public string Name
    {
        get { return _name.Replace("_", " "); }
        set { _name = value.Replace("_", " "); }
    }

    public EnumListItem(int Value, string Name, string DisplayName)
    {
        _value = Value;
        _displayName = string.IsNullOrEmpty(DisplayName) ? Name : DisplayName;
        _name = Name;
    }
}


public class Enums
{

    public enum ShipperRating
    {
        [DisplayValue("1-3 (Poor)")]
        Poor = 1,
        [DisplayValue("4-5 (Fair/Average)")]
        Average = 2,
        [DisplayValue("6-7 (Good)")]
        Good = 3

    }
}

public static class Class1
{
    public static void BindDropDownListWithEnum(ref DropDownList ddl, Type enumType)
    {
        List<enumlistitem> el = GetEnumValues(enumType, false);
        ddl.DataSource = el;
        ddl.DataTextField = "DisplayName";
        ddl.DataValueField = "Value";
        ddl.DataBind();
    }

    public static void BindRadComboBoxWithEnum(ref RadComboBox radcmb, Type enumType)
    {
        List<enumlistitem> el = GetEnumValues(enumType, true);
        radcmb.DataSource = el;
        radcmb.DataTextField = "DisplayName";
        radcmb.DataValueField = "Value";
        radcmb.DataBind();
    }
    public static List<enumlistitem> GetEnumValues(Type type, bool isSelectRequired)
    {
        List<enumlistitem> el = new List<enumlistitem>();
        EnumListItem ei;
        foreach (int item in Enum.GetValues(type))
        {
            ei = GetEnumItem(type, item);
            el.Add(ei);
        }
        if (isSelectRequired)
        {
            el.Insert(0, new EnumListItem(0, "--Select--", "--Select--"));
        }
        return el;
    }
    public static EnumListItem GetEnumItem(Type type, int item)
    {
        string name = Enum.GetName(type, item);
        string displayName = string.Empty;
        object[] displayAttributes = type.GetField(Enum.GetName(type, item)).GetCustomAttributes(typeof(DisplayValue), false);
        if (displayAttributes.Length > 0)
            displayName = ((DisplayValue)displayAttributes[0]).Value;
        else
            displayName = name.Replace("_", " ");

        
        return new EnumListItem(item, name, displayName);
    }
}

Wednesday, 16 January 2013

Set column editable mode based on another column value changes in kendo UI

View
  
@(Html.Kendo().Grid<MvcApplication1.Models.TestModels>() .Name("Grid") .Columns(columns => { columns.Bound(p => p.ID); columns.Bound(p => p.Name); columns.Bound(p => p.IsActive); columns.ForeignKey(p => p.FID, (System.Collections.IEnumerable)ViewData["TestList"], "Value", "Text"); }) .ToolBar(toolBar => toolBar.Save()) .Editable(editable => editable.Mode(GridEditMode.InCell)) .Pageable() .Sortable() .Scrollable() .Filterable() .Events(e => e.Edit("onGridEdit")) .DataSource(dataSource => dataSource .Ajax() .Batch(true) .ServerOperation(false) .Events(events => events.Error("errorHandler")) .Model(model => { model.Id(p => p.ID); model.Field(p => p.ID).Editable(false); }) .Read(read => read.Action("ForeignKeyColumn_Read", "Home")) .Update(update => update.Action("ForeignKeyColumn_Update", "Home")) ) )
 


Controller
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            List<SelectListItem> items = new List<SelectListItem>();

            for (int i = 1; i < 6; i++)
            {
                SelectListItem item = new SelectListItem();
                item.Text = "text" + i.ToString();
                item.Value = i.ToString();
                items.Add(item);
            }

            ViewData["TestList"] = items;

            return View();
        }

        public ActionResult ForeignKeyColumn_Read([DataSourceRequest] DataSourceRequest request)
        {
            List<TestModels> models = new List<TestModels>();

            for (int i = 1; i < 6; i++)
            {
                TestModels model = new TestModels();
                model.ID = i;
                model.Name = "Name" + i;
                
                if (i % 2 == 0)
                {
                    model.IsActive = true;
                    model.FID = i;
                }

                models.Add(model);
            }

            return Json(models.ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ForeignKeyColumn_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<TestModels> tests)
        {
            if (tests != null && ModelState.IsValid)
            {
                // Save/Update logic comes here
            }

            return Json(ModelState.ToDataSourceResult());
        }
    }
} 

Model
 
namespace MvcApplication1.Models
{
    public class TestModels
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsActive { get; set; }
        public int FID { get; set; }
    }
}
DOWNLOAD DEMO