How to generate Auto generated Grid in MVC
My scenario is something like this that my stored procedure is returning differ column(like name of column and no of column) while I am passing parameter through drop down list i.e.. I do not have knowledge of fields in column at compile time ,do you have any solution how can I achieve this in MVC
Abhishek JainPosted Aug 30, 2013, 8:09 AM
Regards
AJ
Gaurav KapahiPosted Aug 31, 2013, 12:35 AM
Gaurav KapahiPosted Aug 31, 2013, 12:32 AM
VIEW Code:
Abhishek JainPosted Aug 30, 2013, 8:16 AM
CSHTML:
Gaurav KapahiPosted Aug 30, 2013, 6:38 AM
public ActionResult Index(string MatchTypeName)
{
foreach (MatchTypeMaster MatchType in db.MatchTypeMasters)
{
SelectListItem selectMatchTypes = new SelectListItem
{
Text = MatchType.MatchTypeName,
Value = MatchType.MatchTypeID,
};
selectMatchTypeList.Add(selectMatchTypes);
ViewBag.MatchTypeName = selectMatchTypeList;
}
if (string.IsNullOrEmpty(MatchTypeName))
{
MatchTypeName = "XYZ";
var bugedlist = GetList(MatchTypeName);
return View(bugedlist);
}
else
{
var GridFill = GetList(MatchTypeName);
return View(GridFill);
}
return View();
}
public List GetList(String MatchTypeName)
{
//var modelList = new List();
var result = new List();
using (SqlConnection conn = new SqlConnection(@"Server= server_name; Uid=xyz;pwd=pass; database=db_name"))
{
try
{
conn.Open();
SqlCommand dCmd = new SqlCommand("SP_WEBBASEDREPorTS", conn);
dCmd.CommandType = CommandType.StoredProcedure;
dCmd.Parameters.Add(new SqlParameter("@MatchType", MatchTypeName));
dCmd.Parameters.Add(new SqlParameter("@ModeFlag", "Batting Industry Average"));
SqlDataAdapter da = new SqlDataAdapter(dCmd);
DataSet ds = new DataSet();
DataTable table = new DataTable();
ds.Clear();
da.Fill(table);
conn.Close();
foreach (DataRow row in table.Rows)
{
var obj = (IDictionary
foreach (DataColumn col in table.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
}
catch
{
}
}
return result;
-----------------------------------------------------------------------
CSHTML Code is:
@using GridMvc.Html;
@model IEnumerable(LearningMVC.User)
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@*
@Html.ActionLink("Create New", "Create")
*@
@using (@Html.BeginForm("Index", "My", FormMethod.Get))
{
@Html.DropDownList("MatchTypeName", "Select All");
}
@Html.Grid(Model).AutoGenerateColumns().Sortable(true)
Improve question Permalink
Abhishek JainPosted Aug 30, 2013, 6:05 AM
If you want that whateever is in your list just gets binded to the WebGrid then, please refer this code:
If you need that some columns are static and some will be decided based on some other parameters, then please refer this code:
If you want to bind a WebGrid directly to a DataTable, it means DataTable only contains the columns which needs to be shown into the grid right, but the data source of the WebGrid is of type IEnumerable so you have to convert the DataTable into an IEnumerable collection. So you have to do something like this:
@model System.Data.DataTable
Hope it helps...
Regards
AJ
Gaurav KapahiPosted Aug 30, 2013, 4:19 AM
Abhishek JainPosted Aug 30, 2013, 3:56 AM