получение значения из ячейки gridview в полях Dynami c с использованием события обновления строки - PullRequest
0 голосов
/ 12 февраля 2020

Я пытаюсь получить значение из gridview, но получаю пустую строку.

это пользовательский интерфейс:

    <asp:GridView runat="server" ID="GroceryGrid"
    ItemType="DAL.Grocery" DataKeyNames="GroceryId"
    SelectMethod="GroceryGrid_GetData"
    AutoGenerateColumns="false" CssClass="table table-responsive" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True" OnRowDeleting="GroceryGrid_RowDeleting" 
    OnRowEditing="GroceryGrid_RowEditing" OnRowUpdating="GroceryGrid_RowUpdating">
    <Columns>
        <asp:DynamicField DataField="GroceryName" HeaderText="GroceryName"/>
        <asp:DynamicField DataField="Cup" HeaderText="Cup" />
        <asp:DynamicField DataField="Spoon" HeaderText="Spoon" />
        <asp:DynamicField DataField="Gram" HeaderText="Gram"/>
        <asp:DynamicField DataField="Piece" HeaderText="Piece"/>
    </Columns>
    </asp:GridView>

и получение данных правильно

     protected void GroceryGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GroceryGrid.Rows[e.RowIndex];
        int groceryId = Convert.ToInt32(GroceryGrid.DataKeys[e.RowIndex].Values[0]);
        string imeNamirnice = row.Cells[0].Text;
        double salica = Convert.ToInt32((row.Cells[2].Text));
        double zlica = Convert.ToInt32((row.Cells[3].Text));
        double gram = Convert.ToInt32((row.Cells[4].Text));
        double komad = Convert.ToInt32((row.Cells[5].Text));
        using (RwaContext update = new RwaContext())

это часть кода, которая пытается получить значение из ячеек, но я получаю пустые значения, такие как

string imeNamirnice = row.Cells[0].Text;

, она возвращает: "" - пустая строка

1 Ответ

0 голосов
/ 12 февраля 2020

Вот пример, вы рядом. Показано несколько разных способов получения значения DataKey в зависимости от того, обновляетесь ли вы или редактируете.

Front-End

<form id="form1" runat="server">
        <div>

            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" DataKeyNames="id" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="true">
                <Columns>
                    <asp:CommandField ButtonType="Button" CausesValidation="false" ShowEditButton="true" EditText="Edit Me" UpdateText="Update Me"/>
                </Columns>
            </asp:GridView>

        </div>
    </form>

Code-Behind

    public partial class MyProduct
    {
        public string id { get; set; }
        public string name { get; set; }
        public string sku { get; set; }
    }

public partial class _testForPw : BaseStorePage
{

    protected void Page_Load(Object sender, EventArgs e)
    {
        Collection<MyProduct> products = new Collection<MyProduct>();
        products.Add(new MyProduct() { id = "123", name = "Test123", sku = "Sku1234" });
        GridView1.DataSource = products;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = (string)GridView1.DataKeys[e.NewEditIndex].Value;

        if (!string.IsNullOrWhiteSpace(id))
        {
            // convert to integer and then execute code
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = (string)GridView1.DataKeys[e.RowIndex].Value;

        if (!string.IsNullOrWhiteSpace(id))
        {
            // convert to integer and then execute code
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...