如何在C#中绑定datagridview列和图表控件..? [英] How to bind datagridview columns and chart control in C#..?

查看:201
本文介绍了如何在C#中绑定datagridview列和图表控件..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要将datagridview列中的数据与图表控件绑定,任何人都可以帮我解决这个问题吗?



我的datagridview包含2列(column1和column2),有多个行值,现在我想用X&创建折线图Y轴,其中X轴指向column1值,Y轴指向column2值。



i尝试了以下代码步骤......!


               

 

chart1 数据源 = dataGridView1 ;
尝试
{
这个 chart1 系列 [ 0 ]。 积分 AddXY
Convert < span class ="pun"style ="margin:0px; padding:0px; border:0px; vertical-align:baseline; background-color:transparent; color:#000000">。 ToDouble dataGridView1 [ 1 ]),
转换 ToDouble dataGridView1 Columns [ 2 ]) );
chart1
DataBind ();
}
catch {}

然后这个,

   chart1    DataSource     =   dataGridView1  ;   
{
chart1
系列 [ 0 ]。 XValueMember = 转换 ToDouble dataGridView1 Columns [ 1 ]);
chart1
系列 [ 0 ]。 YvalueMembers = 转换 ToDouble dataGridView Columns [ 2 ]);
chart1
DataBind ();
}

这也是,

 

chart1 DataSource = dataGridView1 ;
尝试
{
这个 chart1 系列 [ 0 ]。 积分 DataBindY (( DataView dataGridView1 DataSource " Column Name" );
}
catch {}

 

但没有用..在这里我想用图表控件和datagridview列之间的数据绑定来绘制图表。比方说,代码没有通过这个任何人都可以帮我吗?如何将图表和datagridview与其他一些有用的方法绑定起来??

我试图通过单击按钮来绑定它们。我知道,我在某处做错了什么..!

提前谢谢..

解决方案

< blockquote>

您好,


请尝试以下操作。代码示例。


我创建了一个类作为我的数据源。

 public class DataCollection 
{
public string X {get; set;}

public int Y {get; set; }
}


然后在本代码中我将数据绑定到我的数据网格。

 private void PopulateGrid(object sender,EventArgs e)
{
TotalNumberAdded ++;


List< DataCollection> data = new List< ; DataCollection>
{
new DataCollection {X =" Test 1",Y = 10},
new DataCollection {X =" Test 1",Y = 10},
new DataCollection {X =" Test 2",Y = 26},
new DataCollection {X =" Test 3",Y = 5},
new DataCollection {X =" Test 4",Y = 22},
new DataCollection {X =" Test 5" ,Y = 15},
新DataCollection {X =" Test 5",Y = 17}
};

dataGridView1.DataSource = data;
PopulateChart();

}

以下是填充图表的方法。

 public void PopulateChart()
{

chart1.Series [0] = new Series();
chart1.Series [0] .XValueMember = dataGridView1.Columns [0] .DataPropertyName;
chart1.Series [0] .YValueMembers = dataGridView1.Columns [1] .DataPropertyName;
chart1.DataSource = dataGridView1.DataSource;
}

希望这能解决您的问题。


谢谢,


Sammani


http://sammanipalansuriya.blogspot .com





I need to bind data from columns of datagridview with chart control, can any one help me regarding this..?

My datagridview contains 2 columns (column1 and column2)with multiple rows values,now i like to create line chart with X & Y axis, where X axis points to column1 values and Y axis points to column2 values.

i tried these following code steps..!

               

chart1.Datasource = dataGridView1; try { this.chart1.Series[0].Points.AddXY( Convert.ToDouble(dataGridView1.Columns[1]), Convert.ToDouble(dataGridView1.Columns[2]) ); chart1.DataBind(); } catch{}

then this,

chart1.DataSource = dataGridView1;
{
   chart1.Series[0].XValueMember = Convert.ToDouble(dataGridView1.Columns[1]);
   chart1.Series[0].YvalueMembers = Convert.ToDouble(dataGridView.Columns[2]);
   chart1.DataBind();
}

and this too,

chart1.DataSource = dataGridView1; try { this.chart1.Series[0].Points.DataBindY((DataView)dataGridView1.DataSource, "Column Name"); } catch {}

but no use.. Here i like to plot chart using databinding between chart control and datagridview columns. To say, code is not passing through this functions. Can anyone help me please? how do I bind chart and datagridview with some other useful methods..?

i'm trying to bind these with single button click. I know, i'm doing something wrong somewhere..!

thanks in advance..

解决方案

Hi,

Try out the following. Code sample.

I have created a class as my data source.

  public class DataCollection
        {
            public string X { get; set; }

            public int Y { get; set; }
        }

Then in this Code I bind Data to my Data Grid.

    private void PopulateGrid(object sender, EventArgs e)
        {
            TotalNumberAdded++;

           
            List<DataCollection> data = new List<DataCollection>
                {
                    new DataCollection {X = "Test 1", Y = 10},
                    new DataCollection {X = "Test 1", Y = 10},
                    new DataCollection {X = "Test 2", Y = 26},
                    new DataCollection {X = "Test 3", Y = 5},
                    new DataCollection {X = "Test 4", Y = 22},
                    new DataCollection {X = "Test 5", Y = 15},
                    new DataCollection {X = "Test 5", Y = 17}
                };

            dataGridView1.DataSource = data;
            PopulateChart();

        }

Here is the method that populate the Chart.

    public void PopulateChart()
        {
      
            chart1.Series[0]=new Series();
            chart1.Series[0].XValueMember =dataGridView1.Columns[0].DataPropertyName;
            chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
            chart1.DataSource = dataGridView1.DataSource;
        }

Hope this will sort out your issue.

Thank you,

Sammani

http://sammanipalansuriya.blogspot.com


这篇关于如何在C#中绑定datagridview列和图表控件..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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