gpt4 book ai didi

c# - 获取 GridView 中每一行的 dataKey 值

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:38 25 4
gpt4 key购买 nike

我在尝试从 GridView 中获取数据键值时遇到了一些问题。这是代码:

GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
foreach (GridViewRow gr in gvForCheckBox.Rows)
{
//If product name of items in prodList and SPUItemList matches
if (available.Where(x => x.id == gr.Cells[0].Text).Any())
{
//Mark the checkBox checked
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
cb.Checked = true;

//Get the product packaging quantity by productName
string name = gr.Cells[1].Text;
int productQuantity = packBLL.getProductQuantityByName(name);
TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
tb.Text = productQuantity.ToString();
}
}

if (available.Where(x => x.id == gr.Cells[0].Text).Any()) 处,它应该与第 0 列的值进行比较逐行,如果 id 匹配,则复选框将被标记。但是,我没有在 GridView 中显示 id 列,而是将其设置为 DataKey 值。所以我想知道如何获取 GridView 中每一行的数据键。

提前致谢。

最佳答案

假设 x.id 是一个字符串,您可以在 中使用 gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString() 获取 DataKey 值>foreach 循环:

foreach (GridViewRow gr in gvForCheckBox.Rows)
{
//If product name of items in prodList and SPUItemList matches
if (available.Where(x => x.id == gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString()).Any())
{
//Mark the checkBox checked
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
cb.Checked = true;

//Get the product packaging quantity by productName
string name = gr.Cells[1].Text;
int productQuantity = packBLL.getProductQuantityByName(name);
TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
tb.Text = productQuantity.ToString();
}
}

关于c# - 获取 GridView 中每一行的 dataKey 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21277969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com