2

尝试从网格视图获取数据键值时遇到一些问题。这是代码:

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 匹配,则复选框将被标记。但是,我没有在网格视图中显示 id 列,而是将其设置为 DataKey 值。所以我想知道如何获取网格视图中每一行的数据键。

提前致谢。

4

3 回答 3

7

假设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();
    }
}
于 2014-01-22T08:52:48.807 回答
2

你应该试试这个

GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
                    foreach (GridViewRow gr in gvForCheckBox.Rows)
                    {

int rowIndex = gr.RowIndex;
string val = (string)gvForCheckBox .DataKeys[rowIndex]["myKey"];
                        //If product name of items in prodList and SPUItemList matches
                        if (available.Where(x => x.id == val ).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();
                        }
                    }

在这里,我确定了当前行的行索引,然后在他的代码的帮助下找到该行的数据键值

int rowIndex = gr.RowIndex;
string val = (string)gvForCheckBox.DataKeys[rowIndex]["myKey"];
于 2014-01-22T08:46:37.090 回答
1

例子

头部网格视图

DataKeyNames="PrecioSinIva, PorcentajeIva"

列网格视图

<asp:BoundField HeaderText="PRECIOSINIVA" DataField="PrecioSinIva" Visible="false"/>    
<asp:BoundField HeaderText="PORCENTAJEIVA" DataField="PorcentajeIva" Visible="false"/>  

foreach (GridViewRow rowDatos in this.uiTrabajosAgregadosVales.Rows)
{
  if (rowDatos.RowType == DataControlRowType.DataRow)
  {
  string precioSinIva =iTrabajosAgregadosVales.DataKeys[rowDatos.RowIndex].Values[0].ToString();
  string porcentajeIva =iTrabajosAgregadosVales.DataKeys[rowDatos.RowIndex].Values[1].ToString();    
  }
}
于 2014-06-02T21:38:48.590 回答