Reputation: 39
I'm struggling a lot trying to figure out how to print out a grid view that I created inside the ASP.NET MVC structure. I created a testing gridview like this:
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
DataRow dr = dt.NewRow();
dr["Column1"] = "hello";
dr["Column2"] = "hola";
dr["Column3"] = "hey";
dt.Rows.Add(dr);
model.gv.DataSource = dt;
model.gv.DataBind();
And now I'm struggling to actually display that gridview inside of my view. I've tried many variations along the lines of:
@foreach (var item in Model.gv.Rows)
{
foreach (var row in Model.gv.Columns)
{
<tr>
<td>item</td>
</tr>
}
}
This logic of what I have above obviously doesn't make sense at this point but it was just an example of the things I already tried to display something from the gridview.
Does anyone know an easier way to display the rows/columns of a gridview in a tabular format? The hardest part is incorporating the rows and columns with HTML tags with the actual data from the gridview (since my view is using HTML). I appreciate any advice, thanks! :)
Upvotes: 1
Views: 386
Reputation: 1255
// Create a model -
public class MyModel..
{
public string col1 { get; set; }
public string cal2 { get; set; }
public string cal3 { get; set; }
}
// Store multiple myModel to the myModelList..
MyModel myModel = new MyModel();
List<MyModel> myModelList = new List<MyModel>();
myModel.col1=“hello”;
myModel.col1=“hela”;
myModel.col1=“hey”;
myModelList.Add(myModel);
// Pass this myModelList to view ..
foreach(MyModel myModel in myModelList)
{
<tr><td>myModel.col1</td><td>myModel.col2</td><td>myModel.col3</td></tr>
// Iterate to display .. with single loop.
}
Upvotes: 1