【问题标题】:Getting dataKey value for each row in grid view获取网格视图中每一行的 dataKey 值
【发布时间】:2014-02-12 05:14:10
【问题描述】:

我在尝试从网格视图中获取数据键值时遇到了一些问题。代码如下:

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

提前致谢。

【问题讨论】:

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


    【解决方案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();
                            }
                        }
    

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

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

    【讨论】:

    • 这一行有错误:string val = (string)this.gvForCheckBox.DataKeys[rowIndex]["id"];在 gvForCheckBox 附近。它说缺少参考
    • @Gwen 现在请尝试我在 gridview 名称之前删除了这个。
    【解决方案2】:

    假设x.id 是一个字符串,您可以在foreach 循环中使用gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString() 获取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();
        }
    }
    

    【讨论】:

    • 它告诉我可能是无意的比较;要获得值比较,请将权利转换为字符串绿色错误消息
    • 我仍然无法通过比较 id 来标记选中的复选框。你介意帮我在另一个线程中解决它吗?
    【解决方案3】:

    示例

    头部网格视图

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多