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 JainRank #1,062 · 1.8K reputationPosted Aug 30, 2013, 4:09 AM EDT
Regards
AJ
Abhishek JainRank #1,062 · 1.8K reputationPosted Aug 29, 2013, 11:56 PM EDT
Gaurav Kapahi132 reputationPosted Aug 30, 2013, 12:19 AM EDT
Abhishek JainRank #1,062 · 1.8K reputationPosted Aug 30, 2013, 2:05 AM EDT
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 Kapahi132 reputationPosted Aug 30, 2013, 2:38 AM EDT
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 JainRank #1,062 · 1.8K reputationPosted Aug 30, 2013, 4:16 AM EDT
CSHTML:
Gaurav Kapahi132 reputationPosted Aug 30, 2013, 8:32 PM EDT
VIEW Code:
Gaurav Kapahi132 reputationPosted Aug 30, 2013, 8:35 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts