I have a chart that the points was added from a datagridview using C#. I would like to include a tooltip on the chart that shows the x and y values of the chart. I have adopted the code from see values of chart points when the mouse is on points.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
chartIC.Series["IC"].Points.AddXY(Convert.ToDateTime(dataGridView1[4,i].Value).ToString("yyyy-MM-dd"),
dataGridView1[1, i].Value);
}
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();
private void chartIC_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
if (prevPosition.HasValue && pos == prevPosition.Value)
return;
tooltip.RemoveAll();
prevPosition = pos;
var results = chartIC.HitTest(pos.X, pos.Y, false,
ChartElementType.PlottingArea);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.PlottingArea)
{
var xVal = chartIC.Series["IC"].Points[result.PointIndex].XValue;
var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
tooltip.Show("X Value=" + xVal + ", Y Value=" + yVal.ToString("0.0"), this.chartIC,
pos.X, pos.Y - 15);
}
}
}
However I got an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index') when I 'mouse over' the chartIC. Would appreciate any help provided.