-3
int i = 0;
string[] AbDates = new string[5];

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    string dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    if (string.IsNullOrEmpty(dt))
    { 
    }
    else
    {
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Red)
        {
            if (MessageBox.Show("Do you  want to UnMark this ?", "UnMark", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
            }
            else
            {
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
                dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                AbDates = AbDates.Where(val => val != dt).ToArray();
                i--;
            }
        }
        else
        {
            if (MessageBox.Show("Do you  want to Mark this As Absent?", "Absent", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
            }
            else
            {
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
                dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                //MessageBox.Show(i.ToString());
                AbDates[i] = dt;
                i++;
            }
        }
    }
}

here is my code.. I have an array with size[5]. I added 5 values into this array and then removed the values. Now my array have only one value. If I add another value to this array it shows the error Index was outside the bounds of the array.

Yurii
  • 4,811
  • 7
  • 32
  • 41
Sijiya
  • 1
  • 1
  • 3

2 Answers2

0

My Code is not exact correct but this helps you a little to try using this from list

Try This

int i = 0;
List<string> AbDates = new List<string>();
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (string.IsNullOrEmpty(dt))
{ 
}
else
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Red)
{
if (MessageBox.Show("Do you  want to UnMark this ?", "UnMark", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
{
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
AbDates.AddRange(AbDates.Where(val => val != dt).ToList());
i--;
}
}
else
{
if (MessageBox.Show("Do you  want to Mark this As Absent?", "Absent", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
{
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
dt = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//  MessageBox.Show(i.ToString());
AbDates.Add(dt);
i++;
}
}
}
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
0
AbDates = AbDates.Where(val => val != dt).ToArray();

You are creating a new array here, which is smaller than your original one (provided the value of dt was in the old one).

Like Co. Aden said, why not use a List?

oerkelens
  • 5,053
  • 1
  • 22
  • 29