获取网格视图中每行的dataKey值

时间:2014-01-22 08:39:47

标签: c# asp.net .net gridview datakey

尝试从网格视图中获取数据键值时出现问题。这是代码:

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值。所以我想知道如何在网格视图中获取每一行的datakey。

提前致谢。

3 个答案:

答案 0 :(得分:7)

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

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();
    }
}

答案 1 :(得分:1)

你应该试试这个

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();
                        }
                    }

这里我确定当前行的rowindex并且在他的代码的帮助下找到该行的datakey值

int rowIndex = gr.RowIndex;
string val = (string)gvForCheckBox.DataKeys[rowIndex]["myKey"];

答案 2 :(得分:0)

实施例

HEAD GRIDVIEW

DataKeyNames="PrecioSinIva, PorcentajeIva"

COLUMNS GRIDVIEW

<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();    
  }
}