Получение значения 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 в моем виде сетки, но я установил его как значение DataKey. Поэтому мне интересно, как получить ключ данных для каждой строки в виде сетки.
Заранее спасибо.
3 ответа
Если предположить, x.id
is a string, you can get the DataKey value using gvForCheckBox.DataKeys[gr.RowIndex].Value.ToString()
внутри 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();
}
}
Вы должны попробовать это
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"];
ПРИМЕР
ГОЛОВНАЯ СЕТКА
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();
}
}