[英]How do i get item by ID from a loop of Rows.count
我想要做的是我想從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;
}
}
}
}
}
}
}
這一行:
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
好的,在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.