如何通过Rows.count循环中的ID获取项目

时间:2016-03-19 15:23:28

标签: c# asp.net gridview sharepoint

我想要做的是我想从gridview更新任何已检查项目的状态。

我的错误:

  应用程序中的服务器错误。

     

输入字符串的格式不正确。

    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;
                                }
                            }
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

这一行:

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

您似乎从int获得Cells[1]值。但根据您的图片,int文字ID位于Cells[0](该行中的第一个列)。

Cells[1]请参阅第二个列:

Diryas
Soban
Attiq
test

Cells[0]请参阅第一个列:

3
4
5
8

答案 1 :(得分:1)

好的,在Mr.Ian的帮助下,我发现了我的错误,这里是获取商品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;
                    }