2

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.

ekad
  • 14,436
  • 26
  • 44
  • 46

3 Answers3

8

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();
    }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • It told me possible unintended comparison; to get a value comparison, cast the right to string green error message –  Jan 22 '14 at 08:55
  • I am still having problem to mark the check box checked by comparing the id. Would you mind to help me solve it in another thread? –  Jan 22 '14 at 09:01
2

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"];
Pawan
  • 1,065
  • 5
  • 10
  • There is an error at this line: string val = (string)this.gvForCheckBox.DataKeys[rowIndex]["id"]; near gvForCheckBox. It said missing reference –  Jan 22 '14 at 08:53
  • @Gwen now please try i have removed this before gridview name. – Pawan Jan 22 '14 at 08:56
1

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