Now I am creating Extension Method by which we can easily generate our requuire type's column.
namespace DevExpress.Web.Mvc
{
#region Grid Columns
public static class GridColumn
{
#region String
/// <summary>
///
/// </summary>
/// <param name="settings"></param>
/// <param name="FieldName"></param>
/// <param name="DisplayName">(Optional) pass Column Caption name(If you dont want to pass caption then pass ""(blank) )</param>
/// <param name="Width">(Optional) Pass column width (If you dont want to pass width then pass 0).</param>
/// <param name="IsCentre">(Optional) If you want to create column centre then pass true otherwise false</param>
public static void GetStringColumn(GridViewSettings settings, string FieldName, string DisplayName = "", int Width = 0, bool IsCentre = false)
{
settings.Columns.Add(gridViewColumn =>
{
if (DisplayName != "")
gridViewColumn.Caption = DisplayName;
if (Width != 0)
gridViewColumn.Width = Unit.Pixel(Width);
if (IsCentre == true)
{
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
}
else
{
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
}
gridViewColumn.FieldName = FieldName;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
});
}
#region String
public static void GetHiddenColumn(GridViewSettings settings, string FieldName)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.FieldName = FieldName;
gridViewColumn.Visible = false;
});
}
#endregion
//public static void GetStringColumn(GridViewSettings settings, string FieldName, string DisplayName, int Width)
//{
// //return new MVCxGridViewColumnCollection<TModel>(Columns);
// settings.Columns.Add(gridViewColumn =>
// {
// gridViewColumn.Caption = DisplayName;
// gridViewColumn.FieldName = FieldName;
// gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
// gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
// gridViewColumn.Width = Unit.Pixel(Width);
// gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
// gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
// });
//}
#endregion
#region DateTime
public static void GetDateColumn(GridViewSettings settings, string FieldName, string DisplayName, string DisplayFormatString = "d")
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.Caption = DisplayName;
gridViewColumn.FieldName = FieldName;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.ColumnType = MVCxGridViewColumnType.DateEdit;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Equals;
gridViewColumn.PropertiesEdit.DisplayFormatString = DisplayFormatString;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
});
}
public static void GetDateColumn(GridViewSettings settings, string FieldName, string DisplayName, int Width, string DisplayFormatString = "d")
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.Caption = DisplayName;
gridViewColumn.FieldName = FieldName;
gridViewColumn.Width = Unit.Pixel(Width);
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.ColumnType = MVCxGridViewColumnType.DateEdit;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Equals;
gridViewColumn.PropertiesEdit.DisplayFormatString = DisplayFormatString;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
});
}
#endregion
#region Numeric
public static void GetNumericColumn(GridViewSettings settings, string FieldName, string DisplayName)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.Caption = DisplayName;
gridViewColumn.FieldName = FieldName;
gridViewColumn.ColumnType = MVCxGridViewColumnType.SpinEdit;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Equals;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
});
}
//"{0:c2}"
public static void GetNumericColumn(GridViewSettings settings, string FieldName, string DisplayName, string DisplayFormatString, int Width = 0)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.Caption = DisplayName;
gridViewColumn.FieldName = FieldName;
gridViewColumn.ColumnType = MVCxGridViewColumnType.SpinEdit;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Equals;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
gridViewColumn.PropertiesEdit.DisplayFormatString = DisplayFormatString;
if (Width != 0)
gridViewColumn.Width = Unit.Pixel(Width);
});
}
public static void GetNumericColumn(GridViewSettings settings, string FieldName, string DisplayName, int Width)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.Caption = DisplayName;
gridViewColumn.FieldName = FieldName;
gridViewColumn.ColumnType = MVCxGridViewColumnType.SpinEdit;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Equals;
gridViewColumn.HeaderStyle.Wrap = DefaultBoolean.True;
gridViewColumn.Width = Unit.Pixel(Width);
});
}
#endregion
#region LinkColumn
public static void GetLinkColumn(HtmlHelper htmlhelper, GridViewSettings settings, string FieldName, string DisplayName, int Width, string GridName)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.FieldName = FieldName;
gridViewColumn.Caption = DisplayName;
gridViewColumn.Width = Unit.Pixel(Width);
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.SetDataItemTemplateContent(container =>
{
htmlhelper.ViewContext.Writer.Write(string.Format("<a href = 'javascript:void(0)' style='width:{3}px;' onclick='{0}.StartEditRow({1});'>{2}</a>", GridName, (container as GridViewDataItemTemplateContainer).VisibleIndex, container.Text, Width));
});
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
});
}
public static void GetLinkColumn(HtmlHelper htmlhelper, GridViewSettings settings, string FieldName, string DisplayName, string GridName)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.FieldName = FieldName;
gridViewColumn.Caption = DisplayName;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.SetDataItemTemplateContent(container =>
{
htmlhelper.ViewContext.Writer.Write(string.Format("<a href = 'javascript:void(0)' onclick='{0}.StartEditRow({1});'>{2}</a>", GridName, (container as GridViewDataItemTemplateContainer).VisibleIndex, container.Text));
});
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
});
}
#endregion
#region Link Column To Redirect BY ID
public static void GetLinkRedirectColumn(HtmlHelper htmlhelper, GridViewSettings settings, string FieldName, string DisplayName, int Width, string URL)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.FieldName = FieldName;
gridViewColumn.Caption = DisplayName;
gridViewColumn.Width = Unit.Pixel(Width);
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.SetDataItemTemplateContent(container =>
{
htmlhelper.ViewContext.Writer.Write("<a href='" + URL + "'>" + container.Text + "</a>");
});
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
});
}
public static void GetLinkRedirectColumn(HtmlHelper htmlhelper, GridViewSettings settings, string FieldName, string DisplayName, string URL)
{
settings.Columns.Add(gridViewColumn =>
{
gridViewColumn.FieldName = FieldName;
gridViewColumn.Caption = DisplayName;
gridViewColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.CellStyle.HorizontalAlign = HorizontalAlign.Left;
gridViewColumn.SetDataItemTemplateContent(container =>
{
htmlhelper.ViewContext.Writer.Write("<a href='" + URL + "'>" + container.Text + "</a>");
});
gridViewColumn.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
});
}
#endregion
#region DeleteButton
public static void GetDeleteColumn(GridViewSettings settings, string ButtonID, string ButtonText, string ButtonURL, string fnCustomButtonClick, int width)
{
GridViewCommandColumnCustomButton deleteButton = new GridViewCommandColumnCustomButton()
{
ID = ButtonID,
Text = ButtonText
};
deleteButton.Image.Url = ButtonURL;
deleteButton.Image.AlternateText = "Delete";
deleteButton.Image.IsResourcePng = true;
settings.CommandColumn.Visible = true;
settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
settings.CommandColumn.CustomButtons.Add(deleteButton);
settings.CommandColumn.Width = System.Web.UI.WebControls.Unit.Pixel(width);
settings.CommandColumn.VisibleIndex = 30;
settings.CommandColumn.SetHeaderTemplateContent(c =>
{
});
settings.ClientSideEvents.CustomButtonClick = string.Format("function(s, e) {{ {0}(s, e); }}", fnCustomButtonClick);
}
public static void GetDeleteColumn(GridViewSettings settings, string ButtonID, string ButtonText, string ButtonURL, string fnCustomButtonClick)
{
GridViewCommandColumnCustomButton deleteButton = new GridViewCommandColumnCustomButton()
{
ID = ButtonID,
Text = ButtonText
};
deleteButton.Image.Url = ButtonURL;
deleteButton.Image.AlternateText = "Delete";
deleteButton.Image.IsResourcePng = true;
settings.CommandColumn.Visible = true;
settings.CommandColumn.ButtonType = ButtonType.Image;
settings.CommandColumn.CustomButtons.Add(deleteButton);
settings.CommandColumn.Width = Unit.Pixel(16);
settings.CommandColumn.VisibleIndex = 30;
settings.CommandColumn.SetHeaderTemplateContent(c =>
{
});
settings.ClientSideEvents.CustomButtonClick = string.Format("function(s, e) {{ {0}(s, e); }}", fnCustomButtonClick);
}
#endregion
}
}