Getting dataKey value for each row in grid view

50.1k Views Asked by At

I am having some problem when trying to get the data keys value from grid view. Here is the code:

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

At if (available.Where(x => x.id == gr.Cells[0].Text).Any()), it's supposed to compare with the value of column 0 row by row, if id matched, then the check box will be marked. However, I did not display an id column in my grid view but I set it as DataKey value. So I wonder how to get the datakey for each row in a grid view.

Thanks in advance.

3

There are 3 best solutions below

2
On BEST ANSWER

Assuming x.id is a string, you can get the DataKey value using gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString() inside the foreach loop:

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

You should try this

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

here i have determime rowindex of current row and than find datakey value for this row with help of his code

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

EXAMPLE

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