-->

Wednesday, September 10, 2014

How to Bind DropDownList with Enum MVC

Earlier article was about to bind DropDownList with simple select list of type selectlistitems. These select list may by any type of list either from database or temporary for that view only. In this article we will create an enum, then create a select list through that enum and finally will bind that to drop-down list.

Create an Enum like I have, named OptionType

enum OptionTypes
{
value1,
value2,
value3,
value4
}

After creating enum, write a class that will work like a helper to create select list for that enum:

public static class EnumHelper
{
// Get the value of the description attribute if the   
// enum has one, otherwise use the value.  
public static string GetDescription<TEnum>(this TEnum value)
{
var fleid = value.GetType().GetField(value.ToString());

if (fleid != null)
{
var attributes = (DescriptionAttribute[])fleid.GetCustomAttributes(typeof(DescriptionAttribute), false);

if (attributes.Length > 0)
{
return attributes[0].Description;
}
}

return value.ToString();
}

/// <summary>
/// Build a select list for an enum
/// </summary>
public static SelectList SelectListFor<T>() where T : struct
{
Type t = typeof(T);
return !t.IsEnum ? null
: new SelectList(CreateSelectList(t), "Value", "Text");
}

/// <summary>
/// Build a select list for an enum with a particular value selected 
/// </summary>
public static SelectList SelectListFor<T>(T selected) where T : struct
{
Type t = typeof(T);
return !t.IsEnum ? null
: new SelectList(CreateSelectList(t), "Text", "Value", selected.ToString());
}

private static IEnumerable<SelectListItem> CreateSelectList(Type t)
{
return Enum.GetValues(t)
  .Cast<Enum>()
  .Select(e => new SelectListItem { Value = e.ToString(), Text = e.GetDescription()                            });
}
}

Go to your controller and write the line as I have:

ViewBag.values = EnumHelper.SelectListFor<OptionTypes>();

Open your view page and write

@Html.DropDownList("values");

Run this view in browser and check, your drop-down list has been bind. This will create a simple list named “values” as specified as parameter. If we want to create a drop-down list for a model then:

@Html.DropDownListFor(model => model.property, (IEnumerable<SelectListItem>)ViewBag.values, new { html parameters })

This will bind this drop-down list as strongly with the property of the model.

Wednesday, July 30, 2014

How to Submit data on DropDown Selection changed in MVC Razor

Submitting the form by button, programmer have to assign only the type of that button and on click this button all the data can be get on the server side. On drop-down change we have manually submit the form by either specifying "OnSelectionChanged" event or some jquery functions.

In earlier article I have created DropDownList in MVC, to submit the form we can simply use only submit function on change event of the list.

@Html.DropDownList("ddlname", "List to bind", "--default title--", new { onchange = "submit();" })

After writing this code on change it will post all the data on the form to server-side but programmer have to use same name in action method as used here “ddlname”. When we use view-model to bind the data then dropdown list’s code will be changed as:

@Html.DropDownListFor(model => model.Property , "List to bind", "--default title--", new { onchange = "submit();" })

This code will contains the value of the property of model as used here. The value will submit on change the selection of this drop-down list. Programmer can change other controls value by using this code.

Suppose we have to select country, state and city on the form in hierarchy manner. On change the country, states drop-down list should change and on change of state, city drop-down list should change. By implement this functionality we can use this submit functionality.

Friday, November 15, 2013

How to Remove Record Entry in DataGridView by Command Button: Windows Form

Binding datagridview only is not sufficient for a programmer, he/she need to be known about how to remove unnecessary records from the list. For that we have to first bind our list of item to datagridview and then add a command button with text “Remove” as discussed in earlier post.

To remove an entry, either we have to find out the record’s unique detail or the index of the record in temporary list of items. Because we are binding the datagridview with the temporary list, that’s why we can remove this record using only the index of the record.
Bind a datagridview with the list of items and then add a command button with the text property to “Remove”. Our main motive in this article here is to access the command column, we have discussed earlier. So generate the Cell_Click event of datagridview and write the following c# code:
if (e.ColumnIndex == 0)
{
stuList.RemoveAt(e.RowIndex);
dataGridView1.DataSource = null;
dataGridView1.DataSource = stuList;
}
Run the form and click on remove button of any row you want to delete. And it will remove that record from the list only.
Form having all the rows:
How to Remove Record Entry in DataGridView by Command Button: Windows Form

Form after deleting the last row, it will show only two records.

How to Remove Record Entry in DataGridView by Command Button: Windows Form

Suppose, the user don’t want to remove, and by mistake he/she has clicked on the button then what? We have to use a confirmation message, record will be deleted only when user presses “Yes” and do nothing if user clicked on “No”. Write the following code replacing above code:
if (e.ColumnIndex == 0)
{
DialogResult result = MessageBox.Show("Sure Delete!", "Confirmation", MessageBoxButtons.YesNo);
if (result==DialogResult.Yes)
{
stuList.RemoveAt(e.RowIndex);
dataGridView1.DataSource = null;
dataGridView1.DataSource = stuList;
}
}
Run the code, it will show a confirmation message box as below. If you click on “Yes” it will delete it, and if you click on “No” it will do nothing. You can change the code as per your requirements.
© Copyright 2013 Computer Programming | All Right Reserved