简体   繁体   English

获取要在网格中显示的foreach项目c#asp.net

[英]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:- 如果要将DateDescription显示为单独的列,则可以使用如下标记:-

<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属性设置为DateDescription (或将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.

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