SelectedIndexChanged for Gridview failing when Dropdownlist is empty

29 Views Asked by At

On my Gridview Enabled Selection is turn on. When I select an item from my dropdownlist (which is cell 5 on my gridview) and click save, the data enters into my sqltable and when I click view for the row in the gridview the data populates back into the dropdown.

If I do not select anything from the dropdownlist and click save, the data enters into my sqltable and the cell in the sqltable is empty (as it should be). But if I click on view for the new row in my gridview I get an error on the cell that has no data in it (which is cell 5 on the gridview).

The error appears on below script for DropdownList2 "DropdownList2 has a SelectedValue which is invalid because it does not exist in the list of items". But I have a blank row in the list of items for the dropdown list.

How can I select a row from my gridview when a cell is empty, in my case cell 5?

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList1.Text = GridView1.SelectedRow.Cells[1].Text;
            DropDownList3.Text = GridView1.SelectedRow.Cells[2].Text;
            TextBox1.Text = GridView1.SelectedRow.Cells[3].Text;
            DropDownList4.Text = GridView1.SelectedRow.Cells[4].Text;
            DropDownList2.Text = GridView1.SelectedRow.Cells[5].Text;
        }

The only thing I tried would be to change DropdownList2.Text to DropdownList.SelectedValue which did not resolve the issue.

0

There are 0 best solutions below