How do i get item by ID from a loop of Rows.count

What I am trying to do is that I want to update the status of any checked item from gridview.

My Error:

Server Error in '/' Application.

Input string was not in a correct format.

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            DropDownList drpDwnStat = (DropDownList)GridView1.Rows[i].FindControl("drpDwnStatus");
            CheckBox chkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            

            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("List");
                    if (list != null)
                    {
                        if (chkBox.Checked == true)
                            {
                                GridViewRow row = (GridViewRow)chkBox.NamingContainer;
                                int index = row.RowIndex;

                                SPListItem newStat = list.Items.GetItemById(int.Parse(GridView1.Rows[index].Cells[1].Text));
                                {
                                    web.AllowUnsafeUpdates = true;
                                    newStat["Status"] = drpDwnStat.SelectedItem.Text;

                                    newStat.Update();
                                    web.AllowUnsafeUpdates = false;
                                }
                            }
                    }
                }
            }
        }
    }

Upvotes: 1

Views: 797

Answers (2)

Okay with the help of Mr.Ian, I found my error and here is the correct code to get item ID.

//NOTE: Rows[i] this i is from the loop (int i = 0; i < GridView1.Rows.Count; i++)
int justtocheck = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);

SPListItem newStat = list.Items.GetItemById(justtocheck);
                    {
                        web.AllowUnsafeUpdates = true;
                        newStat["Status"] = drpDwnStat.SelectedItem.Text;

                        newStat.Update();
                        web.AllowUnsafeUpdates = false;
                    }

Upvotes: 1

Ian
Ian

Reputation: 30813

This line:

SPListItem newStat = list.Items.GetItemById(int.Parse(GridView1.Rows[index].Cells[1].Text))

You seem to get the int value from Cells[1]. But according to your picture, the int text ID is in Cells[0] (which is the first column in the row).

Cells[1] refer to the second column:

Diryas
Soban
Attiq
test

Cells[0] refer to the first column:

3
4
5
8

Upvotes: 2

Related Questions