我想做的是,我想从 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/