[英]Get foreach items to show in a grid c# asp.net
I am using Weather WebService and I have a gridview with the id forecast
. 我正在使用Weather WebService,并且具有ID
forecast
的gridview。 How could I display foreach items in the gridview? 如何在gridview中显示foreach项目? I have tried using
我尝试使用
forecast += item.Date.ToShortDateString() + " " + item.Desciption + "|";
But I get the error 但是我得到了错误
'System.Web.UI.WebControls.GridView' does not contain a definition for 'forecast' and no extension method 'forecast' accepting a first argument of type 'System.Web.UI.WebControls.GridView' could be found (are you missing a using directive or an assembly reference? )
'System.Web.UI.WebControls.GridView'不包含'forecast'的定义,也找不到扩展方法'forecast'接受类型为'System.Web.UI.WebControls.GridView'的第一个参数(您是缺少using指令或程序集引用吗? )
WeatherService.WeatherSoapClient weather = new WeatherService.WeatherSoapClient("WeatherSoap");
WeatherService.ForecastReturn for = weather.GetCityForecastByZIP(Zip.Text);
if (for.Success)
{
response.Text = for.ResponseText;
city.Text = for.City;
State.Text = for.State;
WeatherStationCity.Text = for.WeatherStationCity;
foreach (var item in for.ForecastResult)
{
forecast += item.Date.ToShortDateString() + " " + item.Desciption + "|";
}
If you have set the AutoGenerateColumns
property to true
or not set at all(by default it is true) then no need to loop through your data and bind the gridview. 如果您已将
AutoGenerateColumns
属性设置为true
或根本没有设置(默认情况下为true),则无需遍历数据并绑定gridview。 If you want to show the Date
& Description
as separate columns then you can use a markup like this:- 如果要将
Date
和Description
显示为单独的列,则可以使用如下标记:-
<asp:GridView ID="forecast" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Date" DataField="Date" />
<asp:BoundField HeaderText="Desciption " DataField="Desciption " />
</Columns>
</asp:GridView>
This will display your data without formatting, but if you want some formatting to be done then either do it before assigning the datasource (by storing it in a DataTable or Custom List) or in the RowDataBound
event of gridview. 这将显示您的数据而无需格式化,但是如果您希望完成某种格式化,则可以在分配数据源之前进行此操作(通过将其存储在DataTable或Custom List中)或在gridview的
RowDataBound
事件中进行。
Use the following code. 使用以下代码。
ForecastItem
class should be defined in a separate file. ForecastItem
类应在单独的文件中定义。 And add two columns to your gridview, setting DataField
property of them to Date
and Description
(or set AutoGenerateColumns
to true) 并将两列添加到您的gridview中,将它们的
DataField
属性设置为Date
和Description
(或将AutoGenerateColumns
设置为true)
public class ForecastItem
{
public string Date {get;set;}
public string Description {get;set;}
}
.
.
.
WeatherService.WeatherSoapClient weather = new WeatherService.WeatherSoapClient("WeatherSoap");
WeatherService.ForecastReturn for = weather.GetCityForecastByZIP(Zip.Text);
if (for.Success)
{
response.Text = for.ResponseText;
city.Text = for.City;
State.Text = for.State;
WeatherStationCity.Text = for.WeatherStationCity;
List<ForecastItem> forecastItems = new List<ForecastItem>();
foreach (var item in for.ForecastResult)
{
forecastItems.Add(new ForcastItem() {
Date = item.Date.ToShortDateString(),
Description = item.Desciption
});
}
forecast.DataSource = forecastItems;
forecast.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.