简体   繁体   English

如何在 ASP.NET MVC 视图中打印出 C# 中的 GridView?

[英]How to print out a GridView in C# in ASP.NET MVC view?

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.我很努力想弄清楚如何打印出我在 ASP.NET MVC 结构中创建的网格视图。 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.我上面的逻辑显然在这一点上没有意义,但这只是我已经尝试从 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).最难的部分是将带有 HTML 标记的行和列与来自 gridview 的实际数据合并(因为我的视图使用的是 HTML)。 I appreciate any advice, thanks!我感谢任何建议,谢谢! :) :)

// 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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM