c# - 我如何从 Rows.count 循环中按 ID 获取项目

标签 c# asp.net gridview sharepoint

我想做的是,我想从 gridview 更新任何选中项目的状态。

我的错误:

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

最佳答案

这一行:

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

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

Cells[1] 引用第二列:

Diryas
Soban
Attiq
test

Cells[0] 引用第一列:

3
4
5
8

关于c# - 我如何从 Rows.count 循环中按 ID 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36103501/

相关文章:

c# - 处理潜在危险的查询字符串

jquery - 我想使用 jquery 验证 asp.net 中的单选按钮列表

c# - SMTP 服务器无法从托管 c# asp.net 发送电子邮件

c# - row.Cells[4].Text 在 GridView1_SelectedIndexChanged 中什么都不返回?

asp.net - 在 asp.net 的 gridview 中显示 pdf 文件大小值

c# - 如何在 C# 中使用预处理器指令仅在 Windows 10 上执行一些代码?

c# - HandleError 和 ASP.MVC 中的强类型布局

c# - 基于64位或32位操作系统导入外部dll

asp.net - 如何在 ASP.Net 应用程序中使用 HTTPS

reactjs - 如何在React Native中排列卡片以形成 GridView ?