【问题标题】:Populating a chart inside a GridView in ASP.NET在 ASP.NET 中的 GridView 中填充图表
【发布时间】:2011-06-13 08:48:26
【问题描述】:

我有一个GridView 控件,我需要在其中显示一些关于我的一些重要变量的信息。这些变量随时间变化,我想使用Chart 控件在表格中将它们的趋势绘制为最后一列。

这是代码的一般视图:

<asp:GridView runat="server" ID="Variables_GridView" CssClass="grid" AutoGenerateColumns="false" Width="100%">
   <FooterStyle BackColor="White" ForeColor="#000066" />
   <HeaderStyle CssClass="gridheader" />
   <RowStyle ForeColor="#000066" />
   <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
   <SortedAscendingCellStyle BackColor="#F1F1F1" />
   <SortedAscendingHeaderStyle BackColor="#007DBB" />
   <SortedDescendingCellStyle BackColor="#CAC9C9" />
   <SortedDescendingHeaderStyle BackColor="#00547E" />
   <Columns>
      <asp:BoundField DataField="VariableName" HeaderText="Name" />
      <asp:BoundField DataField="VariableType" HeaderText="Type" />
      <asp:BoundField DataField="VariableDescription" HeaderText="Description" />
      <asp:TemplateField HeaderText="Latest values">
         <ItemTemplate>
            <asp:Chart ID="Values_Chart" runat="server" Width="150" Height="50">
               <Series>
                  <asp:Series Name="Values_Series" XAxisType="Primary" XValueType="Int32" ChartType="Line"></asp:Series>
               </Series>
               <ChartAreas>
                  <asp:ChartArea Name="Values_ChartArea"></asp:ChartArea>
               </ChartAreas>
            </asp:Chart>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

在我的代码隐藏中,您可以找到这种类型,用于映射属性(我指定的那些):

public class VariableRepresentation {

        private string variableName;

        private string variableType;

        private string variableDescription;

        private float[] variableValues;

        public string VariableName {
            get { return this.variableName; }
            set { this.variableName = value; }
        }

        public string VariableType {
            get { return this.variableType; }
            set { this.variableType = value; }
        }

        public string VariableDescription {
            get { return this.variableDescription; }
            set { this.variableDescription = value; }
        }

        public float[] VariableValues {
            get { return this.variableValues; }
            set { this.variableValues = value; }
        }

        public VariableRepresentation(string name, string type, string description) {
            this.variableName = name;
            this.variableType = type;
            this.variableDescription = description;
        }

        public VariableRepresentation(string name, string type, string description, float[] initialValues) : this(name, type, description) {
            this.variableValues = initialValues;
        }

    }

好的!

在这个页面的加载方法中我有以下内容:

protected void Page_Load(object sender, EventArgs e) {
   VariableRepresentation[] vars = new VariableRepresentation[6];
   vars[0] = new VariableRepresentation("Variable 1", 
      "Type 1", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   vars[1] = new VariableRepresentation("Variable 2", 
      "Type 2", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   vars[2] = new VariableRepresentation("Variable 3", 
      "Type 3", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   vars[3] = new VariableRepresentation("Variable 4", 
      "Type 1", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   vars[4] = new VariableRepresentation("Variable 5", 
      "Type 2", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   vars[5] = new VariableRepresentation("Variable 6", 
      "Type 3", "A generic variable", new float[] { 11, 12, 13, 14, 11, 20 });
   this.Variables_GridView.DataSource = vars;
   this.Variables_GridView.DataBind();
}

嗯,你可以想象,输出是一个表格,但我的图表是空的。我如何在 .aspx 文件、xhtml 标记、我展示的第一个代码中采取行动,让这种情况发生?

我想我应该在模板字段中放置一些数据绑定表达式,但我不知道该放置什么......

你能帮帮我吗?

谢谢

PS: 请注意,这里我没有提供解决方案,问题是我不知道如何将数据连接到图表,实际上图表(每表行一个)什么都不显示是正确的,因为我没有指定aspx 页面中的任何绑定表达式,也不是代码隐藏中的一段正确代码。

【问题讨论】:

  • 如果您将图表放在 gridview 之外,它会显示您的数据吗?
  • @Nima:实际上它没有显示任何内容并且它是正确的,我没有插入任何条件或代码以在图表的最后一列中显示数据点。如果您看到代码,则没有绑定表达式和其他说明告诉字段要在图表中显示什么。如果我将图表放在外面,我不会为每一行显示图表,这不是我想要的,无论如何也不会改变......

标签: c# asp.net data-binding gridview asp.net-charts


【解决方案1】:

不知道有没有一种方便的方法可以直接在XHTML中将数据绑定到图表系列中?但也许您可以在 GridView.RowDataBound 上创建一个事件,在那里找到图表控件,然后将数据附加到图表系列?

【讨论】:

  • 请注意,您应该将图表控件视为一些变量。然后在其中找到 YourChart["seriesName"] 作为系列的系列;那么向系列添加点的最简单方法可能是通过 YourSeries.Points.AddXY()。可能最后你应该在图表控件上做 BindData()。
  • @ub1k:不简单,,,你看,如果我使用事件,我应该在gridview的datasource属性中指定一些东西,事件是DataBinding或者DataBound,表示需要一些数据要在某个地方检索,否则我无法管理这个......
  • this.Variables_GridView.DataSource = vars; this.Variables_GridView.RowDataBound += yourEvent;
  • @ub1k: 啊,你的意思是,将它应用到网格...而不是图表...我会尝试
  • @ub1k: 回调被调用,但现在我需要在 DataBound 回调方法中获取从事件参数到最后一列的引用,以便我可以引用图表和插入数据罐...怎么做????
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 2012-08-11
  • 2020-09-10
相关资源
最近更新 更多