如何将datagridview列分隔为不同的变量

时间:2015-07-15 01:33:27

标签: c# datagridview

我需要一些帮助

我从我的db 2加载Colunmns Date是DateTime而Price是双倍

日期和价格 12/3/15 15.00

并将它们存储在数据网格中。

你能否告诉我如何将它们分成两个不同的变量并将它们转换为数字以便在R.NET中应用它们并制作一个情节?

此致

private void button1_Click(object sender, EventArgs e)
    {
        //Connect to R
        REngine.SetEnvironmentVariables();
        REngine engine = REngine.GetInstance();
        engine.Initialize();

        //connect to db
        string constring = "datasource=localhost;port=3306;username=root;password=;";//connect to server
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand("Select Date,AnnualPrice from db_og.historicprices;", conDataBase);
        //MySqlDataReader myReader;
        //ArrayList DataAnnualPrice = new ArrayList();
        try
        {
            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdDataBase;
            DataTable dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bSource = new BindingSource();

            bSource.DataSource = dbdataset;
            dataGridView1.DataSource = bSource;
            sda.Update(dbdataset);



        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);

        }

2 个答案:

答案 0 :(得分:0)

您可以向datagridview添加属性

onrowdatabound="CustomGridView_RowDataBound" 

事件处理如下。

  void CustomGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Convert date here
      //e.Row.Cells[0].Text = ;
      // Convert price here
      //e.Row.Cells[1].Text = ;
    }
  }

答案 1 :(得分:0)

尝试为date和Price创建ArrayList变量

 ArrayList varDate = new ArrayList();
 ArrayList varPrice = new ArrayList();

然后在数据集加载到datagridview循环后通过每一行并将其数据添加到上面的数组列表中。您的循环可能如下所示

foreach (DataGridViewRow dr in dataGridView1.Rows)
{

    varDate.add(dr.Cells["Date"].Value.ToString());
    varPrice.add(dr.Cells["AnnualPrice"].Value.ToString());

}

希望你能得到这个想法并帮助你