在网格视图中获取选中行的文本框值

4

我在使用gridview时遇到了一些问题,需要获取被勾选的行中文本框中的文本。当点击按钮时,它应该获取选中行的ProdID和数量(即文本框中的文本):

protected void lbnConfirm_Click(object sender, EventArgs e)
    {
        string quantity = "" , prodID = "";
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                Panel pnl = item.FindControl("pBody1") as Panel;
                GridView gv = pnl.FindControl("gvProduct") as GridView;
                foreach (GridViewRow gr in gv.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                    if (cb.Checked)
                    {
                        //Get the productID which set as DataKeyNames for selected row index
                        prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                        var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                        if (tbQuantity != null)
                        {
                            quantity = tbQuantity.Text;
                        }
                        tempList.Add(prodID);
                    }  
                }
            }
        }
        for (int i = 0; i < tempList.Count; i++)
        {    
            //Testing
            lblTest.Text += tempList[i] + " " + quantity;
        }
    }

假设我有50个prodID为1的产品、77个prodID为2的产品以及90个prodID为3的产品。当我遍历tempList时,这应该是我应该得到的结果:

1 50units, 2 77units, 390units

然而,这些代码并不能独立地从每个产品的文本框中获取数量。下面是我得到的结果:

1 90units, 2 90units, 3 90units

这仅仅是获取列表中最后一个产品的数量。我想知道是否有任何方法可以解决这个问题?提前感谢。

编辑部分:

 foreach (string key in tempList.Keys)
        {
            packagesNeeded = 1;
            unitQty = prodPackBLL.getUnitQtySPU(tempList[key]);
            lblTest.Text += key + " " + tempList[key];
            if (Convert.ToInt32(quantity) < (packagesNeeded * unitQty))
            {
                //Pop up message
                Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"Insufficient storage\");", true);
            }
        } 

你是否选择多行中的复选框? - Adarsh Shah
3个回答

1
你可以做类似这样的事情:
        foreach (DataGridViewRow item in GV.Rows)
        {
            if (Convert.ToBoolean(item.Cells[0].Value) == true)
                //here you get the rowcell value :
                string val = item.Cells[1].Value.ToString();
                //If you want to convert to a textbox :
                TextBox textBox = (TextBox)item.Cells[1].Value;
        }

GV为GridView的Id,checkbox为第0列,您可能想获取的值为第1列


我成功获取了选中行的索引,但是没有得到它所对应的文本框的值。你所说的Cells[1]是指文本框所在的列吗? - user2531590
我应该添加另一个列表来存储数量,就像我为prodID所做的那样吗? - user2531590
cells[i] 是当前行中第 i 列的单元格 - gwt
但它不是TextBox。应该如何将其转换为TextBox? - Mohamed Salemyan
我已编辑我的答案,向您展示如何将单元格转换为文本框。 - gwt

1
由于你在每一行都覆盖了变量"quantity",所以才会出现这种情况。如果你想要存储多个值,你需要使用 List<string> 来存储选中复选框的每一行的数量。
类似以下代码。注意:我没有测试过这段代码。
protected void lbnConfirm_Click(object sender, EventArgs e)
{
    List<string> quantity = new List<string>(); 
    prodID = "";
    foreach (RepeaterItem item in Repeater1.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Panel pnl = item.FindControl("pBody1") as Panel;
            GridView gv = pnl.FindControl("gvProduct") as GridView;
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                if (cb.Checked)
                {
                    //Get the productID which set as DataKeyNames for selected row index
                    prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                    var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                    if (tbQuantity != null)
                    {
                        quantity.Add(tbQuantity.Text);
                    }
                    tempList.Add(prodID);
                }  
            }
        }
    }
    for (int i = 0; i < tempList.Count; i++)
    {    
        //Testing
        lblTest.Text += tempList[i] + " " + quantity[i];
    }
}

那么这意味着我需要一个嵌套的for循环来获取prodID和quantity吗?你能给我展示一些例子吗? - user2531590
哦,我不应该放置嵌套循环,因为它会给我一个索引超出范围异常的错误消息。谢谢,现在它可以工作了! - user2531590
很高兴它能正常工作...你也可以像@ekad提到的那样使用字典...这是一个更好的解决方案... - Adarsh Shah
然而,当我尝试比较数量值时,它告诉我输入的字符串格式不正确,尽管我已经尝试过这个:Convert.ToInt32(quantity)。你能帮我检查编辑部分并帮我指出哪里出了问题吗? - user2531590
你是在使用@ekad提到的字典还是我的解决方案? - Adarsh Shah
字典类型的问题。是因为有时从文本框获取文本并进行一些转换时,转换可能不正确,因为我已经找到了一些关于 int.TryParse() 的解决方案,但我不知道如何将其适配到我的代码中。 - user2531590

1
你可以使用字典将每个prodID与其对应的quantity配对。尝试这段代码:
protected void lbnConfirm_Click(object sender, EventArgs e)
{
    Dictionary<string, string> tempList = new Dictionary<string, string>();
    string quantity = "", prodID = "";
    foreach (RepeaterItem item in Repeater1.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Panel pnl = item.FindControl("pBody1") as Panel;
            GridView gv = pnl.FindControl("gvProduct") as GridView;
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                if (cb.Checked)
                {
                    //Get the productID which set as DataKeyNames for selected row index
                    prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                    var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                    if (tbQuantity != null)
                    {
                        quantity = tbQuantity.Text;
                    }
                    tempList.Add(prodID, quantity);
                }
            }
        }
    }

    foreach (string key in tempList.Keys)
    {
        packagesNeeded = 1;
        unitQty = prodPackBLL.getUnitQtySPU(key);
        lblTest.Text += key + " " + tempList[key];
        if (Convert.ToInt32(tempList[key]) < (packagesNeeded * unitQty))
        {
            //Pop up message
            Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"Insufficient storage\");", true);
        }
    } 
}

根据您上面的示例,tempList["1"]将生成"50"tempList["2"]将生成"77"tempList["3"]将生成"90"

你能帮我检查一下已编辑部分吗?当我尝试访问数量时,它会给出“输入字符串格式不正确”的错误消息。为什么会这样? - user2531590
我已经编辑了我的答案,你需要将tempList[key]转换而不是quantity变量。你还需要验证并确保文本框只包含整数值。 - ekad
我以为tempList[key]包含prodID和数量?程序怎么知道要与哪一个进行比较呢?嗯,它还是不起作用。错误信息仍然是相同的。 - user2531590
刚刚又编辑了我的答案并添加了一些解释,希望能有所帮助。 - ekad
哦,我在检索时连接了我的数量。对不起我的错误。 - user2531590

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接