如何基于从下拉列表中选择的选定项目在图表控件上显示数据 [英] How do I display data on a chart control based on a selected item that I have selected from a dropdownlist

查看:116
本文介绍了如何基于从下拉列表中选择的选定项目在图表控件上显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的视图层上将数据从SQL数据库显示到图表控件.我想显示基于所选项目的数据,并获取相应的数据以显示在图表控件上.以下是我使用的代码,但未显示任何内容,也不会给我任何错误.

这是我在业务逻辑层中用于返回数据的方法.

I want to display data from an SQL database to a chart control on my view layer. I want to display data based on an item that I selected and get the corresponding data to display on a chart control. Below is the code that I used but nothing is being displayed and it does not give me any errors.

This is my method in the business logic layer that I use to return the data.

public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;

        }


  private void populate_reader(System.Data.IDataReader dr, DurbanVotes objp)
        {
            objp.Candidate_Name = dr.GetString(0);
            objp.Total_Votes_perc =Convert .ToString ( dr.GetInt32(1)+" %");
        }       
    }</csparameterlisttype></csparameterlisttype></durbanvotes></durbanvotes></durbanvotes>


这是我用来返回数据的表单背后的代码.


This is the code behind my form that I am using to return the data.

protected void btnsearch_Click(object sender, EventArgs e)
      {
          string year = ddyear.SelectedItem.Text .ToString ();
          string region = ddregion.SelectedValue.ToString();
          {
          Business_Logic.DurbanVotes objc = new DurbanVotes();
          Chart1.Titles.Add("Latest Results");
          Chart1 .DataSource = objc.Get_DBNVotes(year, region);
          Chart1.Series[0].XValueMember = "Candidates";
          Chart1.Series[0].YValueMembers = "Votes";
          Chart1.DataBind();
          }
      }

推荐答案

我不确定是否有错误.为什么不尝试使用try..catch块.
I''m not sure about no error. Why don''t you try with try..catch block.
public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
        try {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;
        }
        catch (Exception ex){
        }
        }


要点:该方法应该是静态的,因为它不使用任何实例信息.然后,您可以在绑定过程中删除无意义对象的创建.

乍一看看起来还可以.确保Get_DBNVotes中实际上没有错误,并且返回的是非空列表(如果您的IDE支持调试,则在"return objvotes"行上停止是实现该目标的好方法). br/>
确保您在图表中要求的列名称与对象上的属性名称相同.看起来好像不是:try
Minor point: that method should be static, as it doesn''t use any instance information. Then you could remove the creation of a pointless object in the binding process.

At first glance it looks ok. Make sure that there actually are no errors in Get_DBNVotes, and that it is returning a non-empty list (putting a stop on the ''return objvotes'' line should be a good way to achieve that if your IDE supports debugging).

Make sure that the column names you''re asking for in the chart are the same as the property names on the object. It looks like they''re not: try
Chart1.Series[0].XValueMember = "Candidate_Name";
Chart1.Series[0].YValueMembers = "Total_Votes_perc";



我希望在这种情况下会引发异常.



I''d expect that to throw an exception if that were the case, though.


我不确定是否有错误.为什么不尝试使用try..catch块.
I''m not sure about no error. Why don''t you try with try..catch block.
public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
        try {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;
        }
        catch (Exception ex){
        }
        }


这篇关于如何基于从下拉列表中选择的选定项目在图表控件上显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆