Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a single series which populates my chart with records from a database when a button is clicked. Seven records are been displayed but in the sanme color.(Example days of the week)

I' trying to change each bar color but without success

Below are the code i tried but it gave me one big green bar :(

C#
 private void button1_Click(object sender, EventArgs e)
    {
        /*First empty the chart2 to fire the current data*/
        if (cbChart.SelectedItem == null)
        {
            chart.Visible = false;
            chart.Controls.Clear();
        }
        else
            //pnchart.Controls.Clear();
        chart.Visible = true;
        chart.Titles.Clear();


        /*Add a new title*/
        Title bezeichung = new Title("Finance" + Environment.NewLine + "(GWU)", Docking.Top, new Font("Yu Gothic", 8, FontStyle.Bold), Color.Black);
        chart.Titles.Add(bezeichung);          
        chart.Titles.Add(bezeichung2);



         if (cbChart.SelectedItem != null)
        {
      string S =    ConfigurationManager.ConnectionStrings[""].ConnectionString;
      SqlConnection con = new SqlConnection(S);
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = con;
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = ("[dbo].[storedprocedure]");
      cmd.Parameters.AddWithValue("@Table_Name", cbChart.SelectedValue.ToString());
      SqlDataReader myReader;  // DataReader to just read Data from the Datbase

            try
            {
                //DO SOMETHING
                con.Open();
                myReader = cmd.ExecuteReader();

                while (myReader.Read())
                {

                   //Parameters (Seriesname, x-axis data & y-axis data)
                    this.chart.Series["Series"].Points.AddXY(myReader["Finance"], myReader["GWU"]);

                    // remove grid lines
                    chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
                    chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
                    chart.ChartAreas[0].AxisX.LabelStyle.Angle = -45;

      chart.Series["series1"].Points[0].Color = Color.Green;
      chart.Series["series1"].Points[1].Color = Color.Red;
      chart.Series["series1"].Points[2].Color = Color.PowderBlue;
      chart.Series["series1"].Points[3].Color = Color.Peru;
      chart.Series["series1"].Points[4].Color = Color.Pink;
      chart.Series["series1"].Points[5].Color = Color.Purple;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

         else
         {

MessageBox.Show("Bitte ", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }

    }


That’s the error message I received after running it:

The index lies outside the valid range, index must not be negative and must be lower than the size of the list
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900